Home > Software design >  transforming img to gray error, tuple out of range
transforming img to gray error, tuple out of range

Time:05-16

I am using this function that I found on the web, its add_speckle() function for adding speckle noise to images :

def add_speckle(k,theta,img):
  
  gauss = np.random.gamma(k,theta,img.size)
  gauss = gauss.reshape(img.shape[0],img.shape[1],img.shape[2]).astype('uint8')
  noise = img   img * gauss  

also am using this method to transform my images to gray images, note: this method is helpful in keeping the altered images as their array form without giving them a format like jpg or png, the method :

def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
if __name__ == '__main__':
    file_name = os.path.join('/content/img5.jpg') 
    img = rgb2gray(plt.imread(file_name))

the issue is when I apply rgb2gray on my noised image, or shortly when I run this command :

new_img = add_speckle(1,1,img)

error happened and its message as follows :

IndexError                                Traceback (most recent call last)
<ipython-input-89-d596c78bc99d> in <module>()
      8 
      9 # img = cv2.imread('/content/img1.jpg')
---> 10 img1_g1 = add_speckle(1,1,img)
     11 img1_g2 = add_speckle(8,8,img)
     12 img1_g3 = add_speckle(22,22,img)

<ipython-input-83-df0363dc14bb> in add_speckle(k, theta, img)
      2 
      3   gauss = np.random.gamma(k,theta,img.size)
----> 4   gauss = gauss.reshape(img.shape[0],img.shape[1],img.shape[2]).astype('uint8')
      5   noise = img   img * gauss

IndexError: tuple index out of range

what to do, I really need help, because I dont want any other method for gray transforming because usually, they include images that need to be saved or deal with paths and directories, while this method in keeping me dealing with only images with array form(no format: jpg, png..etc), and to let you know the source of the problem is the same add_speckle() function, because it produces images,enter code here not in grayscale form.

thanks in advance to everybody :)

CodePudding user response:

You are converting to a grayscale image from RGB, but your noise addition assumes the images are RGB. You are getting an out-of-bounds error because the shape of the NumPy array is 2D but you are assuming it's 3D. That is not the case because of the grayscale conversion. To be channel-agnostic, don't explicitly access the shape parameter. In fact, there is no need for you to reshape the array. It's already in the size you expect. Take note that you need to use the shape attribute, not size:

def add_speckle(k,theta,img):
    gauss = np.random.gamma(k,theta,img.shape)
    noise = img   img * gauss.astype(img.dtype)
    return noise

The extra intricacy here is to convert the noisy counterpart into the same form as the input image to respect the arithmetic performed between two arrays of the same type, but care should be taken so that you are not overflowing with respect to the data type. I have not explicitly checked for this here and I leave that to you to figure this out.

  • Related