zoukankan      html  css  js  c++  java
  • OpenCV 透视变换【图像归一化矫正】

     

    OpenCV 透视变换【扑克牌矫正】

                                                     

    参考文献:

    http://www.cnblogs.com/self-control/archive/2013/01/18/2867022.html

    http://opencv-code.com/tutorials/automatic-perspective-correction-for-quadrilateral-objects/ 

     

    透视变换:

    http://blog.csdn.net/xiaowei_cqu/article/details/26478135

     

     

    具体流程为:

    a)载入图像→灰度化→边缘处理得到边缘图像(edge map)

    cv::Mat im = cv::imread(filename);

    cv::Mat gray;

    cvtColor(im,gray,CV_BGR2GRAY);

    Canny(gray,gray,100,150,3);

     

    b)霍夫变换进行直线检测,此处使用的是probabilistic Hough transform(cv::HoughLinesP)而不是standard Hough transform(cv::HoughLines)

    std::vector<Vec4i> lines;

    cv::HoughLinesP(gray,lines,1,CV_PI/180,70,30,10);

    for(int i = 0; i < lines.size(); i++)

        line(im,cv::Point(lines[i][0],lines[i][1]),cv::Point(lines[i][2],lines[i][3]),Scalar(255,0,0),2,8,0);

     

    c)通过上面的图我们可以看出,通过霍夫变换检测到的直线并没有将整个边缘包含,但是我们要求的是四个顶点所以并不一定要直线真正的相交,下面就要求四个顶点的坐标,公式为:

    perspective-quadrilateral-line-intersections-equation.png

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    cv::Point2f computeIntersect(cv::Vec4i a, cv::Vec4i b)
    {
        intx1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3];
        intx3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];
     
        if(floatd = ((float)(x1-x2) * (y3-y4)) - ((y1-y2) * (x3-x4)))
        {
            cv::Point2f pt;
            pt.x = ((x1*y2 - y1*x2) * (x3-x4) - (x1-x2) * (x3*y4 - y3*x4)) / d;
            pt.y = ((x1*y2 - y1*x2) * (y3-y4) - (y1-y2) * (x3*y4 - y3*x4)) / d;
            returnpt;
        }
        else
            returncv::Point2f(-1, -1);
    }
      

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    std::vector<cv::Point2f> corners;
    for (int i = 0; i < lines.size(); i++)
    {
        for(intj = i+1; j < lines.size(); j++)
        {
            cv::Point2f pt = computeIntersect(lines[i], lines[j]);
            if(pt.x >= 0 && pt.y >= 0)
                corners.push_back(pt);
        }
    }

     
    d)检查是不是四边形
    1
    2
    3
    4
    5
    6
    7
    8
    9
    std::vector<cv::Point2f> approx;
    cv::approxPolyDP(cv::Mat(corners), approx,
                     cv::arcLength(cv::Mat(corners),true) * 0.02,true);
     
    if (approx.size() != 4)
    {
        std::cout <<"The object is not quadrilateral!"<< std::endl;
        return-1;
    }

      

     
    e)确定四个顶点的具体位置(top-left, bottom-left, top-right, and bottom-right corner)→通过四个顶点求出映射矩阵来.
    void sortCorners(std::vector<cv::Point2f>& corners, cv::Point2f center)
    {
        std::vector<cv::Point2f> top, bot;
     
        for(inti = 0; i < corners.size(); i++)
        {
            if(corners[i].y < center.y)
                top.push_back(corners[i]);
            else
                bot.push_back(corners[i]);
        }
     
        cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0];
        cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1];
        cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0];
        cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1];
     
        corners.clear();
        corners.push_back(tl);
        corners.push_back(tr);
        corners.push_back(br);
        corners.push_back(bl);
    }

     下面是获得中心点坐标然后利用上面的函数确定四个顶点的坐标

    for (int i = 0; i < corners.size(); i++)
        center += corners[i];
     
    center *= (1. / corners.size());
    sortCorners(corners, center);

     定义目的图像并初始化为0

    cv::Mat quad = cv::Mat::zeros(300, 220, CV_8UC3);

     获取目的图像的四个顶点

    std::vector<cv::Point2f> dst_pt;
    dst.push_back(cv::Point2f(0,0));
    dst.push_back(cv::Point2f(quad.cols,0));
    dst.push_back(cv::Point2f(quad.cols,quad.rows));
    dst.push_back(cv::Point2f(0,quad.rows));

     计算映射矩阵

    cv::Mat transmtx = cv::getPerspectiveTransform(corners, quad_pts);

    进行透视变换并显示结果

    cv::warpPerspective(im, quad, transmtx, quad.size());
    cv::imshow("quadrilateral", quad);

      

     

     

     

     

     

     

     

     

     

     

     

     

     

    1. // affine transformation.cpp : 定义控制台应用程序的入口点。  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5.   
    6. /** 
    7.  * Automatic perspective correction for quadrilateral objects. See the tutorial at 
    8.  * http://opencv-code.com/tutorials/automatic-perspective-correction-for-quadrilateral-objects/ 
    9.  */  
    10. #include <opencv2/imgproc/imgproc.hpp>  
    11. #include <opencv2/highgui/highgui.hpp>  
    12. #include <iostream>  
    13.   
    14. #pragma comment(lib,"opencv_core2410d.lib")            
    15. #pragma comment(lib,"opencv_highgui2410d.lib")            
    16. #pragma comment(lib,"opencv_imgproc2410d.lib")      
    17.   
    18.   
    19.   
    20. cv::Point2f center(0,0);  
    21.   
    22. cv::Point2f computeIntersect(cv::Vec4i a, cv::Vec4i b)  
    23. {  
    24.     int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];  
    25.     float denom;  
    26.   
    27.     if (float d = ((float)(x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4)))  
    28.     {  
    29.         cv::Point2f pt;  
    30.         pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;  
    31.         pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;  
    32.         return pt;  
    33.     }  
    34.     else  
    35.         return cv::Point2f(-1, -1);  
    36. }  
    37.   
    38. void sortCorners(std::vector<cv::Point2f>& corners,   
    39.                  cv::Point2f center)  
    40. {  
    41.     std::vector<cv::Point2f> top, bot;  
    42.   
    43.     for (int i = 0; i < corners.size(); i++)  
    44.     {  
    45.         if (corners[i].y < center.y)  
    46.             top.push_back(corners[i]);  
    47.         else  
    48.             bot.push_back(corners[i]);  
    49.     }  
    50.     corners.clear();  
    51.       
    52.     if (top.size() == 2 && bot.size() == 2){  
    53.         cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0];  
    54.         cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1];  
    55.         cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0];  
    56.         cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1];  
    57.       
    58.           
    59.         corners.push_back(tl);  
    60.         corners.push_back(tr);  
    61.         corners.push_back(br);  
    62.         corners.push_back(bl);  
    63.     }  
    64. }  
    65.   
    66. int main()  
    67. {  
    68.     cv::Mat src = cv::imread("image.jpg");  
    69.     if (src.empty())  
    70.         return -1;  
    71.   
    72.     cv::Mat bw;  
    73.     cv::cvtColor(src, bw, CV_BGR2GRAY);  
    74.     cv::blur(bw, bw, cv::Size(3, 3));  
    75.     cv::Canny(bw, bw, 100, 100, 3);  
    76.   
    77.     std::vector<cv::Vec4i> lines;  
    78.     cv::HoughLinesP(bw, lines, 1, CV_PI/180, 70, 30, 10);  
    79.   
    80.     // Expand the lines  
    81.     for (int i = 0; i < lines.size(); i++)  
    82.     {  
    83.         cv::Vec4i v = lines[i];  
    84.         lines[i][0] = 0;  
    85.         lines[i][1] = ((float)v[1] - v[3]) / (v[0] - v[2]) * -v[0] + v[1];   
    86.         lines[i][2] = src.cols;   
    87.         lines[i][3] = ((float)v[1] - v[3]) / (v[0] - v[2]) * (src.cols - v[2]) + v[3];  
    88.     }  
    89.       
    90.     std::vector<cv::Point2f> corners;  
    91.     for (int i = 0; i < lines.size(); i++)  
    92.     {  
    93.         for (int j = i+1; j < lines.size(); j++)  
    94.         {  
    95.             cv::Point2f pt = computeIntersect(lines[i], lines[j]);  
    96.             if (pt.x >= 0 && pt.y >= 0)  
    97.                 corners.push_back(pt);  
    98.         }  
    99.     }  
    100.   
    101.     std::vector<cv::Point2f> approx;  
    102.     cv::approxPolyDP(cv::Mat(corners), approx, cv::arcLength(cv::Mat(corners), true) * 0.02, true);  
    103.   
    104.     if (approx.size() != 4)  
    105.     {  
    106.         std::cout << "The object is not quadrilateral!" << std::endl;  
    107.         return -1;  
    108.     }  
    109.       
    110.     // Get mass center  
    111.     for (int i = 0; i < corners.size(); i++)  
    112.         center += corners[i];  
    113.     center *= (1. / corners.size());  
    114.   
    115.     sortCorners(corners, center);  
    116.     if (corners.size() == 0){  
    117.         std::cout << "The corners were not sorted correctly!" << std::endl;  
    118.         return -1;  
    119.     }  
    120.     cv::Mat dst = src.clone();  
    121.   
    122.     // Draw lines  
    123.     for (int i = 0; i < lines.size(); i++)  
    124.     {  
    125.         cv::Vec4i v = lines[i];  
    126.         cv::line(dst, cv::Point(v[0], v[1]), cv::Point(v[2], v[3]), CV_RGB(0,255,0));  
    127.     }  
    128.   
    129.     // Draw corner points  
    130.     cv::circle(dst, corners[0], 3, CV_RGB(255,0,0), 2);  
    131.     cv::circle(dst, corners[1], 3, CV_RGB(0,255,0), 2);  
    132.     cv::circle(dst, corners[2], 3, CV_RGB(0,0,255), 2);  
    133.     cv::circle(dst, corners[3], 3, CV_RGB(255,255,255), 2);  
    134.   
    135.     // Draw mass center  
    136.     cv::circle(dst, center, 3, CV_RGB(255,255,0), 2);  
    137.   
    138.     cv::Mat quad = cv::Mat::zeros(300, 220, CV_8UC3);  
    139.   
    140.     std::vector<cv::Point2f> quad_pts;  
    141.     quad_pts.push_back(cv::Point2f(0, 0));  
    142.     quad_pts.push_back(cv::Point2f(quad.cols, 0));  
    143.     quad_pts.push_back(cv::Point2f(quad.cols, quad.rows));  
    144.     quad_pts.push_back(cv::Point2f(0, quad.rows));  
    145.   
    146.     cv::Mat transmtx = cv::getPerspectiveTransform(corners, quad_pts);  
    147.     cv::warpPerspective(src, quad, transmtx, quad.size());  
    148.   
    149.     cv::imshow("image", dst);  
    150.     cv::imshow("quadrilateral", quad);  
    151.     cv::waitKey();  
    152.     return 0;  
    153. }  

    实现结果:


     

     

  • 相关阅读:
    Jenkins的安装
    nginx的正则
    nginx的详解(四)
    nginx的详解(三)
    nginx的详解(二)
    Linux基础(七)
    linux-syslog服务
    Django中使用Oracle数据库
    django-admin-simpleui
    closewait---文件描述符
  • 原文地址:https://www.cnblogs.com/fag888/p/5789170.html
Copyright © 2011-2022 走看看