【发布时间】:2018-06-02 23:06:58
【问题描述】:
我使用以下 numpy 数组来保存具有以下形状的黑白图像
print(img.shape)
(28, 112)
当我尝试对图像进行灰度化时,通过以下步骤使用 opencv 获取轮廓
#grayscale the image
grayed = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#thredshold image
thresh = cv2.threshold(grayed, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
我收到以下错误
<ipython-input-178-7ebff17d1c18> in get_digits(img)
6
7 #grayscale the image
----> 8 grayed = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
9
10
error: C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:11073: error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor
opencv 错误中没有任何信息可以找出问题所在
【问题讨论】:
-
您的图像已经是灰度图像,即黑白形状为 (28, 112)
-
如果
img的形状为 (28, 112),则它不是彩色 BGR 图像。已经是灰度了。 (BGR 或 RGB 的形状为 (28, 112, 3)。) -
是的,它是黑白的,但是 cv2 的阈值功能要求它以某种方式进行灰度,我该如何实现呢?
-
好的,现在我将它转换回 3 通道图像,但仍然出现相同的错误
img = np.stack((img,) * 3,-1) img = img.astype(np.float64) img *= 1./255 print(img.shape)现在形状为(28, 112, 3)但仍然grayed = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)给出相同的错误color.cpp:11073: error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor -
好的,我们在这里取得了进展 :) 我将 float64 更改为 float32 并且灰度问题消失了,现在我陷入了阈值问题
img = np.stack((img,) * 3,-1) img = img.astype(np.float32) img *= 1./255 print(img.shape) plt.imshow(img) #grayscale the image grayed = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #thredshold image thresh = cv2.threshold(grayed, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]thresh.cpp:1406: 错误: ( -215) src.type() == (((0) & ((1