Following this example,
I'm trying to build an application to recognize objects in a video.
My program is composed of the following steps (see code sample of each step below):
- Read the image of the object to be recognized into a
cv::Mat
object. - Detect keypoints in the object and compute descriptors.
- Read each frame of the video,
- Detect keypoints and compute descriptors of the frame,
- Match the descriptors of the frame to the descriptors of the object,
- Draw results.
Problem: The 6'th step causes a segmentation fault (see code below).
Question: What causes it, and how can I fix it?
Thank you!
Notes:
- The program runs for several frames before the segfault. The crash occurs on frame 23, which is the first frame of the video that has any content (i.e. which is not completely black).
- By removing the line of
drawMatches(...);
there is no crash. - Running on Windows 7, OpenCV 2.4.2, MinGW.
Debuggind attempt:
Running the program through gdb yields the following message:
Program received signal SIGSEGV, Segmentation fault.
0x685585db in _fu156___ZNSs4_Rep20_S_empty_rep_storageE () from c:\opencv\build\install\bin\libopencv_features2d242.dll
Step 1 - reading object's image:
Mat object;
object = imread(OBJECT_FILE, CV_LOAD_IMAGE_GRAYSCALE);
Step 2 - Detecting keypoints in the object and computing descriptors:
SurfFeatureDetector detector(500);
SurfDescriptorExtractor extractor;
vector<KeyPoint> keypoints_object;
Mat descriptors_object;
detector.detect(object , keypoints_object);
extractor.compute(object, keypoints_object, descriptors_object);
Steps 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);
}
Screenshot just before the segfault:
Frame 22 (completely black):
Frame 23 (in which the segfault occurs):