简体   繁体   English

我正在使用 cv2.imread() 读取图像,并将其转换为灰度。 但是为什么我显示它时它不是灰度的?

[英]I am reading an image with cv2.imread(), and converting it to grayscale. However why is it not in grayscale when I display it?

I went to read up the syntax of cv2.imread() method and it says that specifying the flag=0 will load the image in grayscale.我去阅读了 cv2.imread() 方法的语法,它说指定 flag=0 将加载灰度图像。

The original image is this:原图是这样的:

Original Image原图

And I executed the following code with the following libs, no errors.我使用以下库执行了以下代码,没有错误。

import cv2 import pytesseract import matplotlib import image import cv2 import pytesseract import matplotlib import image

img=cv2.imread("C:/Users/HP_Demo/Desktop/cv2/sample02.png",0)

plt.imshow(img)

plt.show()

The result is this: Result image结果是这样的:结果图像

I do not really know but try using that: cv2.imread("path", cv2.IMREAD_GRAYSCALE).我真的不知道,但尝试使用它: cv2.imread("path", cv2.IMREAD_GRAYSCALE).

PS.附注。 And it is better to use sys.path.join() instead of raw /最好使用sys.path.join()而不是 raw /

import cv2 

img=cv2.imread("colorful.png",1)
cv2.imshow("",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:结果:

丰富多彩的

import cv2 

img=cv2.imread("colorful.png",0) # same image changed the 1 to 0
cv2.imshow("",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:结果:

灰灰

Conclusion结论

As I said in a comment maybe the image you used is causing the no grayscale.正如我在评论中所说,您使用的图像可能导致无灰度。

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

相关问题 cv2.imread接收cv2.IMREAD_GRAYSCALE作为参数时数组的不同长度 - Different length of array when cv2.imread receives cv2.IMREAD_GRAYSCALE as argument 为什么当我将 imread 与灰度图像一起使用时,我得到的参数必须是 2D 数组错误? - why I got parameter must be 2D array error when I used imread with grayscale image? 我正在获取新添加的图像到目录的路径,但是 cv2.imread() 打印类型<class 'nonetype'>在 python</class> - I am getting the path of an newly added image to directory, but cv2.imread() printing type <class 'NoneType'> in python 为什么 cv2.imread output 即使使用 cv.IMREAD_ANYDEPTH 也是 32 位图像的零矩阵? - Why does cv2.imread output a matrix of zeros for a 32-bit image even when using cv.IMREAD_ANYDEPTH? 使用cv2.imread加载图像时发生意外的图像修改 - Unexpected image modification when using cv2.imread for image loading 打开cv2将彩色图像转换为灰度 - open cv2 converting color image to grayscale 将 CV2 图像转换为灰度的问题 - Problem Converting CV2 image to grayscale 为什么 cv2.imread() 不存储我的图像文件的名称? - Why is cv2.imread() not storing the name of my image files? 为什么使用 django python 时 cv2.imread 不返回任何结果? - Why cv2.imread returns none when using django python? 无法使用cv2.imread()读取图像 - Cannot read image using cv2.imread()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM