Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I have started my tests by using 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.

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:

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

From my research, uint8 is just an alias for CV_8U, which is the same as CV_8UC1. Couldn't find any code sample passing a mask to any feature detection algorithm in Python.

like image 945
m3h0w Avatar asked Feb 20 '17 14:02

m3h0w


1 Answers

Thanks to Miki I've managed to find a 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). Thus it couldn't be accepted as a mask.

After checking for type and shape (has to be uint8 and [rows,cols,1]):

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

Convert the mask to gray if it's still 3-channel:

mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
like image 178
m3h0w Avatar answered Sep 18 '22 08:09

m3h0w