如何使用遮罩参数进行特征点检测

如何使用遮罩参数进行特征点检测

本文介绍了OpenCV:如何使用遮罩参数进行特征点检测(SURF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将SurfFeatureDetector限制为一组区域(遮罩).为了进行测试,我只定义了一个遮罩:

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

当我通过"roi"作为掩码时,出现此错误:

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

这有什么问题?如何正确将掩码传递给SurfFeatureDetector的检测"方法?

What is wrong with this? How can I correctly pass a mask to the SurfFeatureDetector's "detect" method?

此致

推荐答案

关于蒙版的两件事.

  • 掩码应为8位无符号字符的1通道矩阵,其转换为opencv类型CV_8U.在您的情况下,遮罩的类型为srcImage.type(),它是一个3通道矩阵
  • 您正在将roi传递到检测器,但是您应该通过mask.当您更改roi时,您还更改了mask.
  • the mask should be a 1-channel matrix of 8-bit unsigned chars, which translates to opencv type CV_8U. In your case the mask is of type srcImage.type(), which is a 3-channel matrix
  • you are passing roi to the detector but you should be passing mask. When you are making changes to roi, you are also changing mask.

以下应该可以工作

Mat srcImage; //RGB source image
Mat mask = Mat::zeros(srcImage.size(), CV_8U);  // type of mask is CV_8U
// roi is a sub-image of mask specified by cv::Rect object
Mat roi(mask, cv::Rect(10,10,100,100));
// we set elements in roi region of the mask to 255
roi = Scalar(255);
SurfFeatureDetector detector();
std::vector<KeyPoint> keypoints;
detector.detect(srcImage, keypoints, mask);     // passing `mask` as a parameter

这篇关于OpenCV:如何使用遮罩参数进行特征点检测(SURF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:37