"OpenCV Error: Assertion failed" after "detectAndCompute"

asked 2016-06-10 07:59:18 -0600

Letis gravatar image

My english very bad, for this reason I'll be brief. (Может кто-то тут понимает по русски?) Code:

    #include <opencv2/opencv.hpp>

    int main(int argc, char *argv[])
    {
        if (argc < 2) return -1;
        cv::Mat image = cv::imread(argv[1]);
        if (image.data == nullptr) return -1;
        cv::VideoCapture cap(cv::CAP_ANY);
        if (!cap.isOpened()) return -2;

        cv::Ptr<cv::ORB> detector = cv::ORB::create(400);
        std::vector<cv::KeyPoint> keysImg, keysFrame;
        cv::Mat discImg, discFrame;
        detector->detectAndCompute(image, cv::Mat(), keysImg, discImg);

        cv::BFMatcher matcher;
        std::vector<cv::DMatch> matches;

        cv::Mat frame;
        cv::Mat finaly;

        while(true)
        {
            cap>>frame;

            detector->detectAndCompute(frame, cv::Mat(), keysFrame, discFrame);

//            std::cout<<discImg.cols<<" "<<discFrame.cols<<" "<<(discImg.cols == discFrame.cols)<<std::endl;

            matcher.match(discImg, discFrame, matches);

            double minDist = 100;
            for (size_t i = 0; i < discImg.rows; ++i)
                if (matches[i].distance < minDist) minDist = matches[i].distance;
            std::vector<cv::DMatch> goodMatches;
            for (size_t i = 0; i < discImg.rows; ++i)
                if (matches[i].distance < 3 * minDist) goodMatches.push_back(matches[i]);

            cv::drawMatches(image, keysImg, frame, keysFrame, goodMatches, finaly);
            cv::imshow("Matches", finaly);

            if (cv::waitKey(33) != -1) break;
        }
        return 0;
    }

About 50% it works. In other cases:

OpenCV Error: Assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)) in batchDistance, file D:\My_my_documents\opencv\sources\modules\core\src\stat.cpp, line 3749

The error occurs in line:

 matcher.match(discImg, discFrame, matches);

when discFrame.cols == 0. Why discFrame.cols == 0 sometimes after detector->detectAndCompute(frame, cv::Mat(), keysFrame, discFrame);?

edit retag flag offensive close merge delete

Comments

check discImg and discFrame size before called matcher.match

LBerger gravatar imageLBerger ( 2016-06-10 08:16:10 -0600 )edit

I know what discFrame size sometime equal 0. I ask - "Why discFrame size sometime equal 0?" :-)

Letis gravatar imageLetis ( 2016-06-10 08:48:02 -0600 )edit
1

because there is no keypoint detected in your image. You must insert if before match.

Now if you want to detect more keypoints you have to modify detector using ORB method.

LBerger gravatar imageLBerger ( 2016-06-10 09:39:04 -0600 )edit

No keypoint detected? It's really possible with default ORB settings? I transfer images from the camera, but not "The black square" :-)

Letis gravatar imageLetis ( 2016-06-10 10:57:03 -0600 )edit

If image dynamics is low there is no keypoints

LBerger gravatar imageLBerger ( 2016-06-10 11:20:06 -0600 )edit

Yes. I checked keysFrame size. Error occurs when it empty. Added line: if (keysFrame.empty()) continue; Thank you

Letis gravatar imageLetis ( 2016-06-10 12:58:03 -0600 )edit