按照这个示例,
我正在尝试构建一个应用程序来识别视频中的对象。
我的程序由以下步骤组成(请参阅下面每个步骤的代码示例):
- 将要识别的物体的图像读入
cv::Mat
物体。 - 检测对象中的关键点并计算描述符。
- 阅读视频的每一帧,
- 检测关键点并计算帧的描述符,
- 将帧的描述符与对象的描述符匹配,
- 绘制结果。
问题:第 6 步导致分段错误(参见下面的代码)。
问题:是什么原因造成的,我该如何解决?
谢谢!
笔记:
- 该程序在段错误之前运行了几帧。崩溃发生在第 23 帧,这是视频中包含任何内容(即不是全黑)的第一帧。
- 通过删除
drawMatches(...);
没有崩溃的行。 - 在 Windows 7、OpenCV 2.4.2、MinGW 上运行。
调试尝试:
通过 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 帧(全黑):
第 23 帧(发生段错误):