cv2接受二进制图像吗?

2024-09-30 18:34:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我面临一个问题,cv2方法,如cv2.detectAndCompute,cv2.HoughlinesP失败,要么深度错误,要么在输入二值图像时无类型。 例如,在下列情况下

def high_blue(B, G):
    if (B > 70 and abs(B-G) <= 15):
        return 255
    else:
        return 0

img = cv2.imread(os.path.join(dirPath,filename))
b1 = img[:,:,0] # Gives **Blue**
b2 = img[:,:,1] # Gives Green
b3 = img[:,:,2] # Gives **Red**

zlRhmn_query = np.zeros((2400, 2400), dtype=np.uint8)
zlRhmn_query[300:2100, 300:2100] = 255
zlRhmn_query[325:2075, 325:2075] = 0

cv2.imwrite('zlRhmn_query.jpg',zlRhmn_query)

zl_Rahmen_bin = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8)
zl_Rahmen_bin = np.vectorize(high_blue)
zl_Rahmen_bin = zl_Rahmen_bin(b1, b2)

cv2.imwrite('zl_Rahmen_bin.jpg',zl_Rahmen_bin)

sift = cv2.xfeatures2d.SIFT_create()

kp_query, des_query = sift.detectAndCompute(zlRhmn_query,None)
kp_train, des_train = sift.detectAndCompute(zl_Rahmen_bin,None)

只有最后一行与zl\u Rahmen\u bin一起失败,出现深度不匹配错误。奇怪的是,zlRhmn\u查询没有抛出任何错误。你知道吗

接下来,当我使用这里给出的skeletonization片段[http://opencvpython.blogspot.de/2012/05/skeletonization-using-opencv-python.html]并将骨架传递给HoughLinesP时,我得到了一个NoneType类型的lines对象。在检查时,我注意到骨架数组也是二进制的,即0或255。你知道吗

请告知。你知道吗


Tags: 类型imgbin错误npbluequerycv2
1条回答
网友
1楼 · 发布于 2024-09-30 18:34:48

在我的生活中,我已经编写了50行Python程序,所以如果我在这里搞错了,请原谅。你知道吗

zl_Rahmen_bin = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8)

您刚刚创建了一个填充有零的二维数组。你知道吗

zl_Rahmen_bin = np.vectorize(high_blue)

现在您可以立即将另一个值(函数)赋给同一个变量,这使得第一行非常过时。你知道吗

zl_Rahmen_bin = zl_Rahmen_bin(b1, b2)

据我所知,你刚刚调用了一个函数z1_Rahmen_bin,并提供了蓝色和绿色的图像作为输入。输出应该是另一个值为0或255的2d数组。你知道吗

我在想怎么做矢量化知道输出应该是哪种数据类型。因为你显然需要uint8。documentation表示类型(如果没有给定)是通过使用第一个参数调用函数来确定的。所以我猜这个例子中的默认类型是255或0。你知道吗

和z1èRahmen_bin.d类型实际上是np.uint32号机组. 你知道吗

所以我修改了这个:

zl_Rahmen_bin = np.vectorize(high_blue)

zl_Rahmen_bin = np.vectorize(high_blue, otypes=[np.uint8])

这似乎很管用。你知道吗

也许这就足够了

z1_Rahmen_bin.dtype = np.uint8

但正如我说的,我对Python一无所知。。。你知道吗

相关问题 更多 >