简体   繁体   English

OpenCV:断言失败(src.checkVector(2,CV_32F)

[英]OpenCV: Assertion failed (src.checkVector(2, CV_32F)

I'm currently trying to correct the perspective of an image within an UIImage extension.我目前正在尝试纠正UIImage扩展中的图像透视。

When getPerspectiveTranform gets called I'm getting the following Asserting.getPerspectiveTranform被调用时,我得到以下断言。

Error错误

OpenCV Error: Assertion failed (src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4) in getPerspectiveTransform, file /Volumes/build-storage/build/master_iOS-mac/opencv/modules/imgproc/src/imgwarp.cpp, line 6748
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Volumes/build-storage/build/master_iOS-mac/opencv/modules/imgproc/src/imgwarp.cpp:6748: error: (-215) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function getPerspectiveTransform

Code代码

- (UIImage *)performPerspectiveCorrection {
    Mat src = [self genereateCVMat];
    Mat thr;
    cv::cvtColor(src, thr, CV_BGR2GRAY);

    cv::threshold(thr, thr, 70, 255, CV_THRESH_BINARY);

    std::vector< std::vector <cv::Point> > contours; // Vector for storing contour
    std::vector< cv::Vec4i > hierarchy;
    int largest_contour_index=0;
    int largest_area=0;

    cv::Mat dst(src.rows,src.cols, CV_8UC1, cv::Scalar::all(0)); //create destination image

    cv::findContours(thr.clone(), contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0)); // Find the contours in the image

    for (int i = 0; i< contours.size(); i++) {
        double a = cv::contourArea(contours[i], false); //  Find the area of contour
        if (a > largest_area){
            largest_area=a;
            largest_contour_index=i; //Store the index of largest contour
        }
    }

    cv::drawContours( dst,contours, largest_contour_index, cvScalar(255,255,255),CV_FILLED, 8, hierarchy );

    std::vector<std::vector<cv::Point> > contours_poly(1);
    approxPolyDP( cv::Mat(contours[largest_contour_index]), contours_poly[0],5, true );
    cv::Rect boundRect = cv::boundingRect(contours[largest_contour_index]);

    if(contours_poly[0].size() >= 4){
        std::vector<cv::Point> quad_pts;
        std::vector<cv::Point> squre_pts;

        quad_pts.push_back(cv::Point(contours_poly[0][0].x,contours_poly[0][0].y));
        quad_pts.push_back(cv::Point(contours_poly[0][1].x,contours_poly[0][1].y));
        quad_pts.push_back(cv::Point(contours_poly[0][3].x,contours_poly[0][3].y));
        quad_pts.push_back(cv::Point(contours_poly[0][2].x,contours_poly[0][2].y));

        squre_pts.push_back(cv::Point(boundRect.x,boundRect.y));
        squre_pts.push_back(cv::Point(boundRect.x,boundRect.y+boundRect.height));
        squre_pts.push_back(cv::Point(boundRect.x+boundRect.width,boundRect.y));
        squre_pts.push_back(cv::Point(boundRect.x+boundRect.width,boundRect.y+boundRect.height));

        Mat transmtx = getPerspectiveTransform(quad_pts, squre_pts);
        Mat transformed = Mat::zeros(src.rows, src.cols, CV_8UC3);
        warpPerspective(src, transformed, transmtx, src.size());

        return [UIImage imageByCVMat:transformed];
    }
    else {
        NSLog(@"Make sure that your are getting 4 corner using approxPolyDP...");
        return self;
    }
}

I know it's late but I confronted the same issue, so maybe it'll help anyone.我知道已经晚了,但我遇到了同样的问题,所以也许它会帮助任何人。

The error occours because src and dst in getPerspectiveTransform(src, dst);错误发生是因为 getPerspectiveTransform(src, dst); 中的 src 和 dst; has to be type of vector<Point2f> not vector<Point> .必须是vector<Point2f>而不是vector<Point>

So it should be like:所以它应该是这样的:

std::vector<cv::Point2f> quad_pts;
std::vector<cv::Point2f> squre_pts;


quad_pts.push_back(cv::Point2f(contours_poly[0][0].x,contours_poly[0][0].y));

// etc.

squre_pts.push_back(cv::Point2f(boundRect.x,boundRect.y));

//etc.

@Domaijnik hi,opencv sift return a type of point2f keypoints according to Doc . @Domaijnik 嗨,opencv sift 根据Doc返回一种 point2f 关键点。 it still give me the same error.它仍然给我同样的错误。 here's my code这是我的代码

import cv2
import numpy as np

# pts1,pts2 are sift keypoints
def ransac(pts1, pts2, img_l, img_r, max_iters=500, epsilon=1):
    best_matches = []
    # Number of samples
    N = 4

    for i in range(max_iters):
        # Get 4 random samples from features
        id1 = np.random.randint(0, len(pts1), N)
        id2 = np.random.randint(0, len(pts2), N)
        src = []
        dst = []
        for i in range(N):
            # Edit1 : pt is Point2f
            src.append(pts1[id1[i]].pt)
            dst.append(pts2[id2[i]].pt)

        src = np.mat(src)
        dst = np.mat(dst)

        # Calculate the homography matrix H
        H = cv2.getPerspectiveTransform(src, dst)
        Hp = cv2.perspectiveTransform(pts1[None], H)[0]

        # Find the inliers by computing the SSD(p',Hp) and saving inliers (feature pairs) that are SSD(p',Hp) &lt; epsilon
        inliers = []
        for i in range(len(pts1)):
            ssd = np.sum(np.square(pts2[i] - Hp[i]))
            if ssd &lt; epsilon:
                inliers.append([pts1[i], pts2[i]])

        # Keep the largest set of inliers and the corresponding homography matrix
        if len(inliers) &gt; len(best_matches):
            best_matches = inliers

    return best_matches

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 断言失败(image.type()== CV_32F)。 GPU卷积。 OpenCV的 - Assertion failed (image.type() == CV_32F). GPU convolution. OpenCV OpenCV错误:断言失败((img.depth()== CV_8U || img.depth()== CV_32F) - OpenCV error : Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) OpenCV C++ 读取图像和补丁 NaN 错误:断言失败 (_a.depth() == CV_32F) 在 patchNaNs - OpenCV C++ read image and patch NaN Error: Assertion failed (_a.depth() == CV_32F) in patchNaNs OpenCV错误:断言失败(samples.cols == var_count &amp;&amp; samples.type()== CV_32F) - OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in predict 打印CV_32F opencv矩阵值 - Printing CV_32F opencv matrix values OpenCV类型CV_32F和CV_32FC1之间的区别 - Difference between OpenCV type CV_32F and CV_32FC1 OpenCV:断言失败((img.depth()== CV_8U || img.depth()== CV_32F)&amp;&amp; img.type()== templ.type()) - OpenCV: assertation failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type()) 在QPixmap中显示cv :: Mat(类型CV_32F) - Display a cv::Mat (type CV_32F) in a QPixmap OpenCV C ++:使用converTo从CV_32F转换为CV8U提供了意外的值 - OpenCV C++: Conversion from CV_32F to CV8U using converTo is giving unexpected values Mat的convertTo函数在OpenCV中将灰度图像的类型转换为CV_32F时会引发断言错误 - Mat's convertTo function complains assert error while converting the type of grayscale image to CV_32F in OpenCV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM