Ask Your Question

Revision history [back]

OpenCV Perspective corrention and cropping

I'm currently trying to correct the perspective of a random taken image showing a rectangle.

The perspective correction is working fine, but i want to crop the image to the target, too. Si I've tried to transform the given contour of my target by the perspective matrix (cv::Mat) and crop it with the results.

My method is currently crashing at the marked line with the following error.

OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/matrix.cpp, line 2430
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/matrix.cpp:2430: error: (-215) mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0) in function create

Code:

cv::Mat correctMat(cv::Mat mat, std::vector<cv::Point> contour) {
    double minObjectSize = 100.0;

    if (contour.size() == 4) {
        cv::Rect rect = cv::boundingRect(contour);

        if (rect.height < minObjectSize || rect.width < minObjectSize) {
            NSLog(@"Objects size was too small: %d * %d", rect.width, rect.height);
        }
        else {
            std::vector<Point2f> quad_pts;
            std::vector<Point2f> squre_pts;

            quad_pts.push_back(Point2f(contour[0].x, contour[0].y));
            quad_pts.push_back(Point2f(contour[1].x, contour[1].y));
            quad_pts.push_back(Point2f(contour[3].x, contour[3].y));
            quad_pts.push_back(Point2f(contour[2].x, contour[2].y));

            squre_pts.push_back(Point2f(rect.x, rect.y));
            squre_pts.push_back(Point2f(rect.x, rect.y + rect.height));
            squre_pts.push_back(Point2f(rect.x + rect.width, rect.y));
            squre_pts.push_back(Point2f(rect.x + rect.width, rect.y + rect.height));

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

            cv::line(mat, quad_pts[0], quad_pts[1], Scalar(0,0,255), 5, CV_AA, 0);
            cv::line(mat, quad_pts[1], quad_pts[2], Scalar(0,0,255), 5, CV_AA, 0);
            cv::line(mat, quad_pts[2], quad_pts[3], Scalar(0,0,255), 5, CV_AA, 0);
            cv::line(mat, quad_pts[3], quad_pts[0], Scalar(0,0,255), 5, CV_AA, 0);

            warpPerspective(mat, transformed, transmtx, mat.size());

            std::vector<cv::Point2f> transformedPoints;

            // Crash
            cv::transform(quad_pts, transformedPoints, transmtx);

            cv::Mat cropped = transformed(cv::boundingRect(transformedPoints));

            fixColorOfMat(cropped);

            return cropped;
        }
    }

    return mat;
}

I don not really know what the error message is telling me so i hope somebody here could help me solving this crash.

OpenCV Perspective corrention and cropping

I'm currently trying to correct the perspective of a random taken image showing a rectangle.

The perspective correction is working fine, but i want to crop the image to the target, too. Si I've tried to transform the given contour of my target by the perspective matrix (cv::Mat) and crop it with the results.

My method is currently crashing at the marked line with the following error.

OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/matrix.cpp, line 2430
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/matrix.cpp:2430: error: (-215) mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0) in function create

Code:

cv::Mat correctMat(cv::Mat mat, std::vector<cv::Point> contour) {
    double minObjectSize = 100.0;

    if (contour.size() == 4) {
        cv::Rect rect = cv::boundingRect(contour);

        if (rect.height < minObjectSize || rect.width < minObjectSize) {
            NSLog(@"Objects size was too small: %d * %d", rect.width, rect.height);
        }
        else {
            std::vector<Point2f> quad_pts;
            std::vector<Point2f> squre_pts;

            quad_pts.push_back(Point2f(contour[0].x, contour[0].y));
            quad_pts.push_back(Point2f(contour[1].x, contour[1].y));
            quad_pts.push_back(Point2f(contour[3].x, contour[3].y));
            quad_pts.push_back(Point2f(contour[2].x, contour[2].y));

            squre_pts.push_back(Point2f(rect.x, rect.y));
            squre_pts.push_back(Point2f(rect.x, rect.y + rect.height));
            squre_pts.push_back(Point2f(rect.x + rect.width, rect.y));
            squre_pts.push_back(Point2f(rect.x + rect.width, rect.y + rect.height));

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

            cv::line(mat, quad_pts[0], quad_pts[1], Scalar(0,0,255), 5, CV_AA, 0);
            cv::line(mat, quad_pts[1], quad_pts[2], Scalar(0,0,255), 5, CV_AA, 0);
            cv::line(mat, quad_pts[2], quad_pts[3], Scalar(0,0,255), 5, CV_AA, 0);
            cv::line(mat, quad_pts[3], quad_pts[0], Scalar(0,0,255), 5, CV_AA, 0);

            warpPerspective(mat, transformed, transmtx, mat.size());

            std::vector<cv::Point2f> transformedPoints;

            // Crash
            cv::transform(quad_pts, transformedPoints, transmtx);

            cv::Mat cropped = transformed(cv::boundingRect(transformedPoints));

            fixColorOfMat(cropped);

            return cropped;
        }
    }

    return mat;
}

I don not really know what the error message is telling me so i hope somebody here could help me solving this crash.

OpenCV Perspective corrention and cropping

I'm currently trying to correct the perspective of a random taken image showing a rectangle.

The perspective correction is working fine, but i want to crop the image to the target, too. Si I've tried to transform the given contour of my target by the perspective matrix (cv::Mat) and crop it with the results.

My method is currently crashing at the marked line with the following error.

OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/matrix.cpp, line 2430
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/matrix.cpp:2430: error: (-215) mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0) in function create

Code:

cv::Mat correctMat(cv::Mat mat, std::vector<cv::Point> contour) {
    double minObjectSize = 100.0;

    if (contour.size() == 4) {
        cv::Rect rect = cv::boundingRect(contour);

        if (rect.height < minObjectSize || rect.width < minObjectSize) {
            NSLog(@"Objects size was too small: %d * %d", rect.width, rect.height);
        }
        else {
            std::vector<Point2f> quad_pts;
            std::vector<Point2f> squre_pts;

            quad_pts.push_back(Point2f(contour[0].x, contour[0].y));
            quad_pts.push_back(Point2f(contour[1].x, contour[1].y));
            quad_pts.push_back(Point2f(contour[3].x, contour[3].y));
            quad_pts.push_back(Point2f(contour[2].x, contour[2].y));

            squre_pts.push_back(Point2f(rect.x, rect.y));
            squre_pts.push_back(Point2f(rect.x, rect.y + rect.height));
            squre_pts.push_back(Point2f(rect.x + rect.width, rect.y));
            squre_pts.push_back(Point2f(rect.x + rect.width, rect.y + rect.height));

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

            cv::line(mat, quad_pts[0], quad_pts[1], Scalar(0,0,255), 5, CV_AA, 0);
            cv::line(mat, quad_pts[1], quad_pts[2], Scalar(0,0,255), 5, CV_AA, 0);
            cv::line(mat, quad_pts[2], quad_pts[3], Scalar(0,0,255), 5, CV_AA, 0);
            cv::line(mat, quad_pts[3], quad_pts[0], Scalar(0,0,255), 5, CV_AA, 0);

            warpPerspective(mat, transformed, transmtx, mat.size());

            std::vector<cv::Point2f> transformedPoints;

            // Crash
            cv::transform(quad_pts, transformedPoints, transmtx);

            cv::Mat cropped = transformed(cv::boundingRect(transformedPoints));

            fixColorOfMat(cropped);

            return cropped;
        }
    }

    return mat;
}

I don not really know what the error message is telling me so i hope somebody here could help me solving this crash.