I want to limit a SurfFeatureDetector to a set of regions (mask). For a test I define only a single mask:
Mat srcImage; //RGB source image
Mat mask = Mat::zeros(srcImage.size(), srcImage.type());
Mat roi(mask, cv::Rect(10,10,100,100));
roi = Scalar(255, 255, 255);
SurfFeatureDetector detector();
std::vector<KeyPoint> keypoints;
detector.detect(srcImage, keypoints, roi); // crash
//detector.detect(srcImage, keypoints); // does not crash
When I pass the "roi" as the mask I get this error:
OpenCV Error: Assertion failed (mask.empty() || (mask.type() == CV_8UC1 && mask.size() == image.size())) in detect, file /Users/ux/Downloads/OpenCV-iOS/OpenCV-iOS/../opencv-svn/modules/features2d/src/detectors.cpp, line 63
What is wrong with this? How can I correctly pass a mask to the SurfFeatureDetector's "detect" method?
Regards,
If you are looking to apply the same for irregular mask then:
Then as usual, generate SIFT/SURF/... pointer
// Create smart pointer for SIFT feature detector.
I tacked your ROI code onto some existing code I was working on, with the following changes it worked for me
Without the changes to the type and the use of
mask
instead ofroi
as your mask, I'd get a runtime error as well. This makes sense, as the detect method wants a mask -- it should be the same size as the original image, and roi isn't (it's a 100x100 rectangle). To see this visually, try displaying the mask and the roiThe type has to match also; the mask should be single channel, while your image type is likely of type 16, which maps to
CV_8UC3
, a triple channel imageTwo things about the mask.
CV_8U
. In your case the mask is of type srcImage.type(), which is a 3-channel matrixroi
to the detector but you should be passingmask
. When you are making changes toroi
, you are also changingmask
.the following should work