简体   繁体   English

OpenCV Python特征检测:如何提供掩码? (筛)

[英]OpenCV Python Feature Detection: how to provide a mask? (SIFT)

I am building a simple project in Python3, using OpenCV3, trying to match jigsaw pieces to the "finished" jigsaw image. 我正在使用OpenCV3在Python3中构建一个简单的项目,试图将拼图碎片与“完成的”拼图图像进行匹配。 I have started my tests by using SIFT. 我已经开始使用SIFT进行测试了。

I can extract the contour of the jigsaw piece and crop the image but since most of the high frequencies reside, of course, around the piece (where the piece ends and the floor starts), I want to pass a mask to the SIFT detectAndCompute() method, thus forcing the algorithm to look for the keypoints only within the piece. 我可以提取拼图的轮廓并裁剪图像,但由于大部分高频位于片段周围(片段结束和地板开始),我想将一个蒙版传递给SIFT detectAndCompute( )方法,从而迫使算法仅在片内寻找关键点。

test_mask = np.ones(img1.shape, np.uint8)
kp1, des1 = sift.detectAndCompute(img1, mask = test_mask)

After passing a test mask (to make sure it's uint8), I get the following error: 传递测试掩码(以确保它是uint8)后,我收到以下错误:

kp1, des1 = sift.detectAndCompute(img1,mask = test_mask) cv2.error: /home/pyimagesearch/opencv_contrib/modules/xfeatures2d/src/sift.cpp:772: error: (-5) mask has incorrect type (!=CV_8UC1) in function detectAndCompute kp1,des1 = sift.detectAndCompute(img1,mask = test_mask)cv2.error:/home/pyimagesearch/opencv_contrib/modules/xfeatures2d/src/sift.cpp:772:错误:( - 5)掩码类型不正确(!= CV_8UC1)函数detectAndCompute

From my research, uint8 is just an alias for CV_8U, which is the same as CV_8UC1. 根据我的研究,uint8只是CV_8U的别名,与CV_8UC1相同。 Couldn't find any code sample passing a mask to any feature detection algorithm in Python. 找不到任何代码示例将掩码传递给Python中的任何特征检测算法。

Thanks to Miki I've managed to find a bug. 感谢Miki,我设法找到了一个bug。

It turned out that my original mask that I created using threshold operations, even though looked binary, was a 3-channel image ([rows], [cols], 3) . 事实证明,我使用阈值操作创建的原始蒙版,即使看起来是二进制,也是一个3通道图像([rows], [cols], 3) Thus it couldn't be accepted as a mask. 因此它不能被接受作为面具。

After checking for type and shape (has to be uint8 and [rows,cols,1]): 检查类型和形状后(必须是uint8和[rows,cols,1]):

print(mask.dtype)
print(mask.shape)

Convert the mask to gray if it's still 3-channel: 如果它仍然是3通道,则将掩码转换为灰色:

mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM