Ask Your Question
0

Mat destructor error with homography

asked 2014-01-07 14:35:43 -0600

unxnut gravatar image

updated 2014-01-07 15:37:17 -0600

I have some code that works correctly. However, at the end of block when it has to go into another iteration of the loop, I get a popup saying "Debug Assertion Failed!" I traced the error to ~Mat that was trying to release a Mat created by findHomography. I used this Mat in perspectiveTransform and it worked correctly. I am unable to figure out why I should have this problem in the destructor. Can someone please help?\

Adding code. It is adapted from a tutorial.

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>

const char * OBJFILE = "segmentedImage.jpg";
// const char * OBJFILE = "Dress2.tif";
const char * IMGFILE = "Taylor8.tif";

int main()
{
    try
    {
        cv::Mat obj = cv::imread ( OBJFILE );
        if ( obj.empty() )
            throw ( std::string ( "Could not open object file " ) + OBJFILE );

//      cv::namedWindow ( "Good matches" );

        char  infile[80], outfile[80];

        for ( int fn = 3; fn < 9; fn++ )
        {
            sprintf ( infile, "Taylor%d.tif", fn );

            cv::Mat frame = cv::imread ( infile );
            if ( frame.empty() )
                throw ( std::string ( "Could not open image file " ) + IMGFILE );


            //*****  Detect the keypoints using SURF detector

            int     minHessian = 400;           // Threshold for the keypoint detector
            cv::SurfFeatureDetector detector ( minHessian );
            std::vector<cv::KeyPoint> keypt_obj, keypt_frame;
            detector.detect ( obj, keypt_obj );
            detector.detect ( frame, keypt_frame );

            //*****  Calculate feature vectors

            cv::SurfDescriptorExtractor extractor;
            cv::Mat descriptor_obj, descriptor_frame;
            extractor.compute ( obj, keypt_obj, descriptor_obj );
            extractor.compute ( frame, keypt_frame, descriptor_frame );

            //***** Matching descriptor vectors using brute force matcher

            cv::BFMatcher   matcher;
            std::vector<cv::DMatch> matches;
            matcher.match ( descriptor_obj, descriptor_frame, matches );


            // Find minimum and maximum distance between keypoints

            float max_dist = 0;
            float min_dist = 50;
            for ( int i = 0; i < descriptor_obj.rows; i++ )
            {
                float& dist = matches[i].distance;
                if ( dist < min_dist )
                    min_dist = dist;
                else
                    if ( dist > max_dist )
                        max_dist = dist;
            }

            std::cout << "Info: Max dist: " << max_dist << std::endl;
            std::cout << "Info: Min dist: " << min_dist << std::endl;

            // Draw only good matches, defined by dist < 3 * min_dist

            std::vector<cv::DMatch> good_matches;
            for ( int i = 0; i < descriptor_obj.rows; i++ )
                if ( matches[i].distance < 3 * min_dist )
                    good_matches.push_back ( matches[i] );

            cv::Mat img_matches = frame;
//          cv::drawMatches (obj, keypt_obj, frame, keypt_frame, good_matches, img_matches,
//              cv::Scalar::all ( -1 ), cv::Scalar::all ( -1 ), std::vector<char>(),
//              cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

            // Localize the object

            std::vector<cv::Point2f> obj_pts, frame_pts;
            for ( unsigned int i = 0; i < good_matches.size(); i++ )
            {
                obj_pts.push_back ( keypt_obj[good_matches[i].queryIdx].pt );
                frame_pts.push_back ( keypt_frame[good_matches[i].trainIdx].pt );
            }

            cv::Mat homography = cv::findHomography ( obj_pts, frame_pts, CV_RANSAC );

            // Get the corners from object to be detected

            std::vector<cv::Point2f> obj_corners ( 4 );
            obj_corners[0] = cv::Point ( 0, 0 );
            obj_corners[1] = cv::Point ( obj.cols, 0 );
            obj_corners[2] = cv::Point ( obj.cols, obj.rows );
            obj_corners[3] = cv::Point ( 0, obj.rows);

            // Get the frame corners

            std::vector<cv::Point2f> frame_corners ( 4 );
            cv::perspectiveTransform ( obj_corners, frame_corners, homography );

            // Check if we have a closed quadrilateral

            bool good_quad = ( frame_corners[0].x < frame_corners[1].x ) &&
                             ( frame_corners[1].y < frame_corners[2].y ) &&
                             ( frame_corners[2].x > frame_corners[3].x ) &&
                             ( frame_corners[3].y > frame_corners[0 ...
(more)
edit retag flag offensive close merge delete

Comments

code, or it did not happen ..

berak gravatar imageberak ( 2014-01-07 14:58:23 -0600 )edit

@berak I posted the code.

unxnut gravatar imageunxnut ( 2014-01-07 15:43:14 -0600 )edit

can you post a link to your dataset as well

Nghia gravatar imageNghia ( 2014-01-07 17:39:36 -0600 )edit

The dataset could be any picture and a pattern extracted from that picture. You can modify the for loop to run just one iteration and get the error.

unxnut gravatar imageunxnut ( 2014-01-07 18:02:21 -0600 )edit

Except that it runs fine without asserting on mine. Hence I thought it might something with your dataset. What is the debug assert you are getting?

Nghia gravatar imageNghia ( 2014-01-07 18:18:37 -0600 )edit

Interesting. I gave the code to a colleague and he was able to run it without trouble. Must be some setting on my machine. The only difference was that has has 64-bit version of OpenCV while I compiled it as 32-bit version. We are both using OpenCV 2.4.7 and Visual Studio 2012.

unxnut gravatar imageunxnut ( 2014-01-07 19:17:04 -0600 )edit
2

Are you linking with the correct debug/release libs? I've seen issues if you try compiling in release but link with the debug libs instead, not sure about the reverse.

Nghia gravatar imageNghia ( 2014-01-07 19:45:59 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-01-07 19:53:57 -0600

unxnut gravatar image

I found out the problem. I was apparently not specifying all the library files on the Additional Dependencies. I looked into the settings used by my colleague and he had specified all the libraries that are available. I copied his settings and all is well.

This does bring an interesting point. In Unix man pages, typically the page specifies the include files and the libraries to be linked into. The OpenCV documentation does not specify those when we look at the functions. Wonder if this should be added into the documentation to save trouble. My preference is to add as few extra material in the code as possible. So, I am generally conservative with the files that I add on include and additional libraries. Don't know who should I ask for this extra material in the documentation.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-01-07 14:35:43 -0600

Seen: 713 times

Last updated: Jan 07 '14