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 :)