OpenCV C++裁剪图像

3
所以,我成功使用opencv c++代码检测到文档边框。现在,我需要裁剪该图像并使其看起来体面。
边框检测效果不错,如下图所示: 裁剪前 但是,当我尝试裁剪时,它看起来像这样: 裁剪后 以下是我正在使用的代码:
extern "C"
JNIEXPORT void JNICALL
Java_prisca_ctest_ScanActivity_doWithMat(JNIEnv *env, jobject instance, jlong matNumber) {
    //reference to the image from Java
    Mat &image = *(Mat *) matNumber;
    //resize image
    image = GetSquareImage(image);
    //add color effects for better detection
    Mat gray;
    cvtColor(image, gray, CV_BGR2GRAY);
    GaussianBlur(gray, gray, Size(5, 5), 0);
    Mat edged;
    Canny(gray, edged, 75, 200);
    //find contours and sort them from biggest to smallest
    vector<vector<Point> > contours;
    vector<Point> screenCnt;
    findContours(edged, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
    // sort contours
    sort(contours.begin(), contours.end(), compareContourAreas);

    Rect boundRect;

    //itterate over the contours and get the biggest contour for image
    for (int i = (int) (contours.size() - 1); i > 0; i--) {
        double peri = arcLength(Mat(contours[i]), true);
        vector<Point> approx;
        approxPolyDP(Mat(contours[i]), approx, 0.02 * peri, true);
        if (approx.size() == 4) {
            screenCnt = approx;
            boundRect = boundingRect(approx);
            break;
        }
    }
    Scalar color = Scalar(0, 255, 0);
    drawContours(image, Mat(screenCnt), -1, color, 5);
    //crop image with boundRect
    image = Mat(image, boundRect).clone();
}


Mat GetSquareImage(const cv::Mat &img, int target_width = 500) {
    int width = img.cols,
            height = img.rows;

    cv::Mat square = cv::Mat::zeros(target_width, target_width, img.type());

    int max_dim = (width >= height) ? width : height;
    float scale = ((float) target_width) / max_dim;
    cv::Rect roi;
    if (width >= height) {
        roi.width = target_width;
        roi.x = 0;
        roi.height = height * scale;
        roi.y = (target_width - roi.height) / 2;
    } else {
        roi.y = 0;
        roi.height = target_width;
        roi.width = width * scale;
        roi.x = (target_width - roi.width) / 2;
    }

    cv::resize(img, square(roi), roi.size());

    return square;
}


extern "C"
bool compareContourAreas(std::vector<cv::Point> contour1, std::vector<cv::Point> contour2) {
    double i = fabs(contourArea(cv::Mat(contour1)));
    double j = fabs(contourArea(cv::Mat(contour2)));
    return (i < j);
}

首先,我按照python代码的教程操作。他调整图片大小以获得所需结果,但当我这样做时,我的文本不可读,我的图像也没有像应该那样被裁剪(裁剪掉周围的背景,只留下纸张等)。是否有人有相关经验,如果有,能否帮助我?

看一下这篇文章,它是用Python编写的。 - Jeru Luke
2个回答

5

1
还发现了这个可能有用的链接 http://answers.opencv.org/question/497/extract-a-rotatedrect-area/ - Luke Greenwood
这看起来很有前途,谢谢。至于图像质量,你能告诉我更多吗? - Miljan Vulovic
原始图像本身看起来有点模糊,考虑到您的代码根本不会降低图像质量,这可能仅仅是由于安卓手机上的相机造成的。也许值得再拍一张文字更加清晰的照片。 - Luke Greenwood
不,它所做的是获取原始图片的某个区域。这个区域本身在技术上具有较低的分辨率,但它并不会降低图像质量。 - Luke Greenwood
我卡在getRectSubPix()上了,它因某种原因崩溃了,有什么想法吗? - Miljan Vulovic
显示剩余3条评论

3

我创建了一个带有本地支持代码的git仓库,可以正确裁剪图像,请在链接中找到它。

如果您有更好的解决方案,请随意编辑代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接