Search code examples
pythonnumba

Much different result when using numba


I have here pure python code, except just making a NumPy array. My problem here is that the result I get is completely wrong when I use @jit, but when I remove it its good. Could anyone give me any tips on why this is?

@jit
def grayFun(image: np.array) -> np.array:
      
    gray_image = np.empty_like(image)
    
    
    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            gray = gray_image[i][j][0]*0.21 + gray_image[i][j][1]*0.72 + gray_image[i][j][2]*0.07
            gray_image[i][j] = (gray,gray,gray)
    
    gray_image = gray_image.astype("uint8")
    return gray_image

Solution

  • This will return a grayscale image with your conversion formula. USUALLY, you do not need to duplicate the columns; a grayscale image with shape (X,Y) can be used just like an image with shape (X,Y,3).

    def gray(image):
       return image[:,:,0]*0.21+image[:,:,1]*0.72 + image[:,:,2]*0.07