简体   繁体   English

灰度到RGB-Python

[英]Grayscale to RGB - Python

I can't get a grayscale image in RGB, here's my code: 我无法获得RGB的灰度图像,这是我的代码:

img=cv2.imread("clahe_2.jpg")
backtorgb = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
showImg(backtorgb,"claheCLR")

This error returns: 该错误返回:

backtorgb = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
cv2.error: /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/imgproc/src/color.cpp:9765: error: (-215) scn == 1 && (dcn == 3 || dcn == 4) in function cvtColor

Filepath is correct. 文件路径正确。 Any suggest? 有什么建议吗?

I think tat problem is that your image is not real grayscale. 我认为问题是您的图像不是真正的灰度。 It is RGB, but visible as grayscale. 它是RGB,但可见为灰度。 So need to bring one channel from image and then run the code: 因此需要从图像中获取一个通道,然后运行代码:

backtorgb = cv2.cvtColor(img[..., 0], cv2.COLOR_GRAY2RGB)

By default, imread opens the image as a 3-channel BGR image so you don't need to convert it, maybe just to RGB if that's what you're looking for. 默认情况下, imread以3通道BGR图像的imread打开图像,因此您无需将其转换,如果您要查找的话,可能只需将其转换为RGB。

Check out the documentation here 此处查看文档

The problem was on the application of the CLAHE filter and its grayscale output, actually the output kept the 3 channels but at the sight it looked like a grayscale, documentation here . 问题出在CLAHE滤波器的应用及其灰度输出上,实际上该输出保留了3个通道,但从外观上看,它看起来像是灰度的, 这里是文档。

I simply found here a method that kept the RGB form to solve the problem, thanks to everyone for the answers. 我在这里简单地找到一种方法,该方法保留了RGB形式来解决问题,这要感谢大家的回答。

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

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