OpenCV 可自动调整参数的透视变换

在shiter大牛的基础之上,对于他的程序做了一定的修改。 
首先,通过两个循环使得霍夫变换两个参数:角度的分辨率和点个数的阈值可以变换,这样就不必对于每一张图像都手动的设置阈值。其次,过滤掉了两个距离很近的直线,使得能够正确找到物体的四个轮廓的直线。

#include <opencv2/imgproc/imgproc.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <iostream>  
#include <set>
 
#pragma comment(lib,"opencv_core2413d.lib")            
#pragma comment(lib,"opencv_highgui2413d.lib")            
#pragma comment(lib,"opencv_imgproc2413d.lib")      
 
 
 
cv::Point2f center(0,0);  
 
cv::Point2f computeIntersect(cv::Vec4i a, cv::Vec4i b)  
{  
    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];  
    float denom;  
 
    if (float d = ((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;  
        return pt;  
    }  
    else  
        return cv::Point2f(-1, -1);  
}  
 
//确定四个点的中心线
void sortCorners(std::vector<cv::Point2f>& corners,   
                 cv::Point2f center)  
{  
    std::vector<cv::Point2f> top, bot;  
 
    for (int i = 0; i < corners.size(); i++)  
    {  
        if (corners[i].y < center.y)  
            top.push_back(corners[i]);  
        else  
            bot.push_back(corners[i]);  
    }  
    corners.clear();  
 
    if (top.size() == 2 && bot.size() == 2){  
        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.push_back(tl);  
        corners.push_back(tr);  
        corners.push_back(br);  
        corners.push_back(bl);  
    }  
}  
 
//计算直线端点的距离
bool Disserence(int a,int b)
{
    if (a * a + b * b < 100)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
int main()  
{  
    cv::Mat src = cv::imread("001.jpg");  
    if (src.empty())  
        return -1;  
 
    cv::Mat bw;  
    cv::cvtColor(src, bw, CV_BGR2GRAY);  
    cv::blur(bw, bw, cv::Size(3, 3));  
    cv::Canny(bw, bw, 100, 100, 3);  
 
    std::vector<cv::Vec4i> lines;
    std::vector<cv::Point2f> corners;
    std::vector<cv::Point2f> approx; 
    int HoughThre = 20;
    int HoughTheta = 30;
    /*
    void HoughLinesP(InputArray image,OutputArray lines, double rho, double theta, int threshold, double minLineLength=0,double maxLineGap=0 )
    image为输入图像,要求是8位单通道图像
    lines为输出的直线向量,每条线用4个元素表示,即直线的两个端点的4个坐标值
    rho和theta分别为距离和角度的分辨率
    threshold为阈值,即步骤3中的阈值
    minLineLength为最小直线长度,在步骤5中要用到,即如果小于该值,则不被认为是一条直线
    maxLineGap为最大直线间隙,在步骤4中要用到,即如果有两条线段是在一条直线上,但它们之间因为有间隙,所以被认为是两个线段,如果这个间隙大于该值,则被认为是两条线段,否则是一条。
    */
    for(;HoughTheta <= 180;HoughTheta = HoughTheta + 30)
    {
        HoughThre = 30;
 
        for(;HoughThre < 300;HoughThre++)
        {
            lines.clear();
            corners.clear();
            approx.clear();
            cv::HoughLinesP(bw, lines, 1, CV_PI/HoughTheta, HoughThre, 30, 50);             //需要不断的变更霍夫变换的参数,才可以使得刚好找到四条直线,确定出边缘
 
            // Expand the lines
            for (int i = 0; i < lines.size(); i++)  
            {  
                cv::Vec4i v = lines[i];  
                lines[i][0] = 0;  
                lines[i][1] = ((float)v[1] - v[3]) / (v[0] - v[2]) * -v[0] + v[1];   
                lines[i][2] = src.cols;   
                lines[i][3] = ((float)v[1] - v[3]) / (v[0] - v[2]) * (src.cols - v[2]) + v[3];  
            }  
 
 
 
            //删除距离过近的两条直线
            std::set<int> ErasePt;
            for (int i = 0; i < lines.size(); i++)
            {
                for (int j = i + 1; j < lines.size(); j++)
                {
                    if (Disserence(abs(lines[i][0] - lines[j][0]),abs(lines[i][1] - lines[j][1])) && (Disserence(abs(lines[i][2] - lines[j][2]),abs(lines[i][3] - lines[j][3]))))
                    {
                        ErasePt.insert(j);
                    }
                }
            }
        //  std::vector<cv::Vec4i>::iterator it = lines.end();
            int Num = lines.size();
            while (Num != 0)
            {
                std::set<int>::iterator j = ErasePt.find(Num);  
                if (j != ErasePt.end())
                {       
                    lines.erase(lines.begin() + Num - 1);
                } 
                Num--;
            }
            if (lines.size() != 4)
            {
                continue;
            }
 
            //计算直线的交点,保存在图像范围内的部分
 
            for (int i = 0; i < lines.size(); i++)  
            {  
                for (int j = i+1; j < lines.size(); j++)  
                {  
                    cv::Point2f pt = computeIntersect(lines[i], lines[j]);  
                    if (pt.x >= 0 && pt.y >= 0 && pt.x <= src.cols && pt.y <= src.rows)             //保证交点在图像的范围之内
                        corners.push_back(pt);  
                }  
            }
            if (corners.size() != 4)
            {
                continue;
            }
 
 
            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;  
            //}
 
            if (lines.size() == 4 && corners.size() == 4 && approx.size() == 4)
            {
                break;
            }
//          std::cout<<".";
        }
 
        std::cout<<std::endl<<"One Cycle";
        if (lines.size() == 4 && corners.size() == 4 && approx.size() == 4)
            break;
        if (HoughTheta == 180 && HoughThre >= 299)
        {
            return -1;
        }
    }
 
    cv::Mat dst = src.clone(); 
    //for (int i = 0; i < lines.size(); i++)  
    //{  
    //  cv::Vec4i v = lines[i];  
    //  cv::line(dst, cv::Point(v[0], v[1]), cv::Point(v[2], v[3]), CV_RGB(0,255,0));  
    //} 
 
    //cvNamedWindow("image",0);
    //cv::imshow("image", dst);
    //cvWaitKey();
 
 
 
 
    // Get mass center  
    for (int i = 0; i < corners.size(); i++)  
        center += corners[i];  
    center *= (1. / corners.size());  
 
    sortCorners(corners, center);  
    if (corners.size() == 0){  
        std::cout << "The corners were not sorted correctly!" << std::endl;  
        return -1;  
    }  
 
 
    // Draw lines  
    for (int i = 0; i < lines.size(); i++)  
    {  
        cv::Vec4i v = lines[i];  
        cv::line(dst, cv::Point(v[0], v[1]), cv::Point(v[2], v[3]), CV_RGB(0,255,0));  
    } 
 
    cvNamedWindow("image",0);
    cv::imshow("image", dst);
 
    cv::waitKey(); 
    // Draw corner points  
    cv::circle(dst, corners[0], 3, CV_RGB(255,0,0), 2);  
    cv::circle(dst, corners[1], 3, CV_RGB(0,255,0), 2);  
    cv::circle(dst, corners[2], 3, CV_RGB(0,0,255), 2);  
    cv::circle(dst, corners[3], 3, CV_RGB(255,255,255), 2);  
 
    // Draw mass center  
    cv::circle(dst, center, 3, CV_RGB(255,255,0), 2);  
 
    cv::Mat quad = cv::Mat::zeros(300, 220, CV_8UC3);  
 
    std::vector<cv::Point2f> quad_pts;  
    quad_pts.push_back(cv::Point2f(0, 0));  
    quad_pts.push_back(cv::Point2f(quad.cols, 0));  
    quad_pts.push_back(cv::Point2f(quad.cols, quad.rows));  
    quad_pts.push_back(cv::Point2f(0, quad.rows));  
 
    cv::Mat transmtx = cv::getPerspectiveTransform(corners, quad_pts);  
    cv::warpPerspective(src, quad, transmtx, quad.size());  
 
    cv::imshow("image", dst);  
    cv::imshow("quadrilateral", quad);  
    cv::waitKey();  
    return 0;  
} 

结果图: 
OpenCV 可自动调整参数的透视变换 OpenCV 可自动调整参数的透视变换

OpenCV 可自动调整参数的透视变换 OpenCV 可自动调整参数的透视变换 
程序依然存在的问题是:对于一些测试的图片,依然无法找到物体四周的直线,也就做不了透视变换了。

上一篇:OpenCV Java 实现票据、纸张的四边形边缘检测与提取、摆正


下一篇:Android系统中的dp和px的转换