简体   繁体   中英

I can't change my RGB colour into grayscale images in python using CV

So, I am doing this project to detect diabetic retinopathy using deep learning. I however am stuck in the preprocessing image section as the pictures that are in different folders(for diff stages of DR) wouldn't convert into grayscale nomatter how much I try.

Here is my functions that does the early preprocessing stage:

def preprocessing(conditionname,directory):
  for image in os.listdir(directory):
    label = eye_label(conditionname,image)
    path = os.path.join(directory,image)
    image = cv2.imread(path,cv2.IMREAD_COLOR) #Reading the colour images
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #Changing coloured image into black and white
    #image = cv2.addWeighted(image,10,cv2.GaussianBlur(image , (0,0) , sigma_x) ,-4 ,12) 
    image = cv2.resize(image,(image_size,image_size)) #Changing the size of each image
    return image

Try using your debugger or IDE to check everything gives you the result you expect, one step at a time.

If you load an image, print its shape:

img = cv2.imread(...)
print(image.shape)

If you convert an image to greyscale, check it has 1 channel afterwards:

img = cv2.cvtColor(...)
print(image.shape)

If you resize an image, check its size is what you expect:

img = cv2.resize(...)
print(image.shape)

If you are going to return an image from a function, check its size and type:

print(result.shape, result.dtype)
return result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM