[英]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:原图是这样的:
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.