简体   繁体   English

我无法使用 CV 在 python 中将我的 RGB 颜色更改为灰度图像

[英]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.然而,我被困在预处理图像部分,因为无论我尝试多少,不同文件夹中的图片(对于 DR 的不同阶段)都不会转换为灰度。

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.尝试使用您的调试器或 IDE 来检查所有内容,一次一个步骤为您提供您期望的结果。

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:如果您将图像转换为灰度,请检查它是否有 1 个通道:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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