继这个例子之后,
我正在尝试构建一个应用程序来识别视频中的对象。
我的程序由以下步骤组成(参见下面每个步骤的代码示例):
cv::Mat对象。问题:第6步会导致分段错误(参见下面的代码)。
问题:是什么原因,以及我如何解决它?
谢谢!
备注:
drawMatches(...);行,就没有崩溃。Debuggind尝试:
通过gdb运行程序将产生以下消息:
Program received signal SIGSEGV, Segmentation fault.
0x685585db in _fu156___ZNSs4_Rep20_S_empty_rep_storageE () from c:\opencv\build\install\bin\libopencv_features2d242.dll步骤1-读取对象的图像:
Mat object;
object = imread(OBJECT_FILE, CV_LOAD_IMAGE_GRAYSCALE);步骤2-检测对象中的关键点和计算描述符:
SurfFeatureDetector detector(500);
SurfDescriptorExtractor extractor;
vector<KeyPoint> keypoints_object;
Mat descriptors_object;
detector.detect(object , keypoints_object);
extractor.compute(object, keypoints_object, descriptors_object);步骤3-6:
VideoCapture capture(VIDEO_FILE);
namedWindow("Output",0);
BFMatcher matcher(NORM_L2,true);
vector<KeyPoint> keypoints_frame;
vector<DMatch> matches;
Mat frame,
output,
descriptors_frame;
while (true)
{
//step 3:
capture >> frame;
if(frame.empty())
{
break;
}
cvtColor(frame,frame,CV_RGB2GRAY);
//step 4:
detector.detect(frame, keypoints_frame);
extractor.compute(frame, keypoints_frame, descriptors_frame);
//step 5:
matcher.match(descriptors_frame, descriptors_object, matches);
//step 6:
drawMatches(object, keypoints_object, frame, keypoints_frame, matches, output);
imshow("Output", output);
waitKey(1);
}屏幕截图就在分段断层之前:

框架22 (完全黑色):

Frame 23 (其中发生分段故障):

发布于 2012-10-23 09:17:45
问题在于drawMatches中参数的顺序。
正确的顺序是:
drawMatches(frame, keypoints_frame, object, keypoints_object, matches, output);解释:
在步骤5中,我使用了match对象的matcher方法:
matcher.match(descriptors_frame, descriptors_object, matches);这种方法的签名是
void match( const Mat& queryDescriptors, const Mat& trainDescriptors,
CV_OUT vector<DMatch>& matches, const Mat& mask=Mat() ) const;这意味着matches包含从 trainDescriptors 到 queryDescriptors的匹配的。
在我的例子中,火车描述符是object的,查询描述符是frame的,所以matches包含从 object 到 frame的匹配。
签名 of drawMatches是
void drawMatches( const Mat& img1, const vector<KeyPoint>& keypoints1,
const Mat& img2, const vector<KeyPoint>& keypoints2,
const vector<DMatch>& matches1to2,
... );使用drawMatches调用时,参数的顺序不正确:
drawMatches(object, keypoints_object, frame, keypoints_frame, matches, output);该方法在不正确的图像中查找匹配的坐标,这可能导致试图访问“超出边界”的像素,从而导致分割错误。
发布于 2012-10-22 18:02:58
你试过在调试器中运行你的程序吗?
只是猜测一下,当没有匹配时,画图匹配就是分段错误?尝试在if (!matches.empty())之前添加drawMatches。顺便问一下,您确定matches在调用matcher.matches(...)之前已经清空了吗?如果不是,您应该在每次循环迭代时手动执行。
https://stackoverflow.com/questions/13001158
复制相似问题