尝试在opencv中将图像转换为灰度时,我收到以下错误消息,可在此处看到: http://i.imgur.com/Av71SYz.png
以下是代码:
import cv2
img = cv2.imread('pictures\chessBoard.png',0)
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
这些错误消息非常含糊,特别是对于初学opencv的人。谢谢你的帮助!
答案 0 :(得分:4)
imread中的0标志已经将图像强制为灰度,因此后来的转换失败。
所以要么跳过转换:
gray_image = cv2.imread('pictures\chessBoard.png',0)
cv2.imshow('image',gray_image)
cv2.waitKey(0)
...
或读取bgr图像,稍后转换
img = cv2.imread('pictures\chessBoard.png')
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('image',gray_image)
cv2.waitKey(0)
作为提醒,这里再次是imread()标志:
>>> help(cv2)
...
IMREAD_ANYCOLOR = 4
IMREAD_ANYDEPTH = 2
IMREAD_COLOR = 1
IMREAD_GRAYSCALE = 0
IMREAD_LOAD_GDAL = 8
IMREAD_UNCHANGED = -1
...