Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

"OpenCV Error: Assertion failed" after "detectAndCompute"

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);?