OpenCV检测图像中的长方形画布或纸张并提取图像内容

转自:知乎,利用OpenCV检测图像中的长方形画布或纸张并提取图像内容
MyCode:

#include
#include
#include
#include
#include
#include
#include

using namespace std;
using namespace cv;
double angle(Point pt1, Point pt2, Point pt0)
{
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    return (dx1*dx2 + dy1*dy2) / sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}


vector largestRect(Mat &img){
    Mat hsv;
    cvtColor(img, hsv, CV_RGB2HSV);
    vector channels;
    split(hsv, channels);
    vector rect;

    double maxArea = 0.0;
    for (int i = 0; i <= 90; i++){

        Mat binaryImage = channels[1] < i;

        imshow("binaryImage", binaryImage);
        cvWaitKey(0);

        if (countNonZero(binaryImage)>img.rows*img.cols*0.3)
            break;

        //找到连通区域
        vector<vector > contours;
        //contours存储轮廓,内每个元素保存了一组由连续的Point点
        //构成的点的集合的向量,每一组Point点集就是一个轮廓。  



        findContours(binaryImage, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
        //CV_RETR_EXTERNAL只检测最外围轮廓,CV_CHAIN_APPROX_SIMPLE仅保存轮廓的拐点信息

            // 多边形近似
            for (int i = 0; i < contours.size(); ++i)
            {
            vector polygon;
            approxPolyDP(contours[i], polygon, arcLength(contours[i], 1) * 0.02, 1);
            double area = fabs(contourArea(polygon));
            // 把不可能是矩形的区域丢掉
            if (isContourConvex(polygon) &&
                polygon.size() == 4 &&
                area > maxArea)
            {
                double maxCosine = 0;
                for (int j = 2; j < 5; j++)
                {
                    double cosine =
                        fabs(angle(polygon[j % 4], polygon[j - 2], polygon[j - 1]));
                    maxCosine = MAX(maxCosine, cosine);
                }
                if (maxCosine < 0.3)//找出接近90度的角!
                {
                    maxArea = area;
                    rect = polygon;
                }
            }
            }
    }
        return rect;
    }




int main(){
    Mat img = imread("1.jpg");
    vector Rec=largestRect(img);
    vectorRec_f;
    Mat(Rec).copyTo(Rec_f);//类型转换-->Point 转换为 Point2f  //参考:https://stackoverflow.com/questions/7386210/convert-opencv-2-vectorpoint2i-to-vectorpoint2f
    vector corners(4);
    Mat H,result;
    result = img.clone();

    corners[0] = Point2f(0, 0);
    corners[1] = Point2f(0, 392);
    corners[2] = Point2f(290, 392);
    corners[3] = Point2f(290, 0);


    H = getPerspectiveTransform(Rec_f, corners);
    cout << H << endl;
    warpPerspective(img, result, H, result.size(), INTER_LINEAR, BORDER_CONSTANT);
    imshow("transformImg", result);
    cvWaitKey(0);
    for (int i = 0; i < Rec.size(); i++){
        cout << Rec[i] << endl;
    }

    for (int i = 0; i < 4; i++){
        circle(img, Rec[i], 10, Scalar(0, 0, 0),-1);
    }

    line(img, Rec[0], Rec[1], Scalar(0, 255, 0));
    line(img, Rec[1], Rec[2], Scalar(0, 255, 0));
    line(img, Rec[2], Rec[3], Scalar(0, 255, 0));
    line(img, Rec[3], Rec[0], Scalar(0, 255, 0));


    imwrite("res.jpg", img);
    imwrite("transformImg.jpg", result);
    cvWaitKey(0);
    getchar();
    return 0;
}

结果图:
找出矩形
res.jpg
OpenCV检测图像中的长方形画布或纸张并提取图像内容_第1张图片

投影映射
transformImg.jpg
OpenCV检测图像中的长方形画布或纸张并提取图像内容_第2张图片

你可能感兴趣的:(opencv)