2017-02-12 65 views
1

我正在研究一个项目,该项目将帮助我们纠正图像的方向。应用透视变换校正图像中纸张的度数

在此代码中,我正在检测一张纸。 我使用的步骤 1.应用houghLine变换 2.检测转角。 3.应用透视变换。 而所有这一切,我能够检测纸张,但它只适用于只有一个或两个图像它不适用于所有图像,我不明白为什么,

我认为在此代码中的问题是它无法正确检测角落,因此我无法纠正图像的角度。

它的工作原理是图像

sheet

上,但是当我用一些其他的图片代替这种话,我不能这样做

sheet2

#include <cv.h> 
#include <highgui.h> 


using namespace std; 
using namespace cv; 


Point2f center(0,0); 

Point2f computeIntersect(Vec4i a, 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))) 
    { 
     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 Point2f(-1, -1); 
} 

void sortCorners(vector<Point2f>& corners, Point2f center) 
{ 
    vector<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){ 
     Point2f tl = top[0].x > top[1].x ? top[1] : top[0]; 
     Point2f tr = top[0].x > top[1].x ? top[0] : top[1]; 
     Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0]; 
     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); 
    } 
} 

int main() 
{ 
    Mat src,cann,hsv; 
    src = imread("C:\\im.jpg",WINDOW_AUTOSIZE); 

    if (src.empty()) 
     return -1; 

    imshow("original",src); 

    blur(src, src, Size(3, 3)); 
    Canny(src, cann, 50, 200, 3); 
    cvtColor(cann, hsv, CV_GRAY2BGR); 

    vector<Vec4i> lines; 
    HoughLinesP(cann, lines, 1, CV_PI/180, 70, 30, 10); 
    for(size_t i = 0; i < lines.size(); i++) 
    { 
     Vec4i l = lines[i]; 
     line(hsv, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 2, CV_AA); 
    } 

    // Expand the lines 
    for (int i = 0; i < lines.size(); i++) 
    { 
     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]; 
    } 

    vector<Point2f> corners; 
    for (int i = 0; i < lines.size(); i++) 
    { 
     for (int j = i+1; j < lines.size(); j++) 
     { 
      Point2f pt = computeIntersect(lines[i], lines[j]); 
      if (pt.x >= 0 && pt.y >= 0) 
       corners.push_back(pt); 
     } 
    } 

    vector<Point2f> approx; 
    approxPolyDP(Mat(corners), approx, arcLength(Mat(corners), true) * 0.02, true); 

    //if (approx.size() != 4) 
// { 
    // cout << "The object is not quadrilateral!" << endl; 
     //return -1; 
    //} 



    // 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) 
    { 
     cout << "The corners were not sorted correctly!" << endl; 
     return -1; 
    } 

    Mat dst = src.clone(); 

    // Draw lines 
    for (int i = 0; i < lines.size(); i++) 
    { 
     Vec4i v = lines[i]; 
     line(dst, Point(v[0], v[1]), Point(v[2], v[3]), CV_RGB(0,255,0)); 
    } 

    // Draw corner points 
    circle(dst, corners[0], 3, CV_RGB(255,0,0), 2); 
    circle(dst, corners[1], 3, CV_RGB(0,255,0), 2); 
    circle(dst, corners[2], 3, CV_RGB(0,0,255), 2); 
    circle(dst, corners[3], 3, CV_RGB(255,255,255), 2); 
    // Draw mass center 
    circle(dst, center, 3, CV_RGB(255,255,0), 2); 

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

    vector<Point2f> quad_pts; 
    quad_pts.push_back(Point2f(0, 0)); 
    quad_pts.push_back(Point2f(quad.cols, 0)); 
    quad_pts.push_back(Point2f(quad.cols, quad.rows)); 
    quad_pts.push_back(Point2f(0, quad.rows)); 

    Mat transmtx = getPerspectiveTransform(corners, quad_pts); 
    warpPerspective(src, quad, transmtx, quad.size()); 

    imshow("blurr",src); 
    imshow("canney",cann); 
    imshow("hough",hsv); 
    imshow("image", dst); 
    imshow("quadrilateral", quad); 

    waitKey(0); 
    return 0; 
} 

请请帮助我,我真的被这个困住了。

+2

[Applying perspective transform correct the degree of sheet paper]可能的重复项(http://stackoverflow.com/questions/42183766/applying-perspective-transform-correct-the-degree-of-sheet-of-paper ) – beaker

+0

纸张是否总是白色和/或比背景更亮?并且纸张是否总是与图像的轴线大致对齐?你必须使用OpenCV,还是可以编写自定义算法?您可能会发现桶形失真问题(https://en.wikipedia.org/wiki/Distortion_(optics))和/或皱纹纸,在这种情况下,边缘不会笔直。这对霍夫来说会是一个问题。在较简单的情况下,可以通过定制累加器来解决问题,以允许粗略或稍微弯曲的线条。根据您的完整图像集,还有其他方法可以解决该问题。 – Rethunk

+0

对于理想情况(现在是正确的),纸张将始终为白色,没有纸张与不同的对齐方式,我在opencv中实现 – Chandan

回答

0

您的算法假定HoughLinesP函数将始终只检测到4行,并且每个行将位于纸张的不同边缘。但是,这种假设是错误的。在你的特定情况下,当你使用第二张图片时,当你处理第二张图片时,它会返回5行。 Click to see the detected lines (marked by non-gray colours).

快速修复

我改变第六HoughLinesP参数(minLineThreshold参数)的值至70。在此之后,在图像中检测到只有四个行,但另一个bug浮出水面;检测到5个角落而不是4个。原因?两个相对的边缘不平行,它们远离图像区域相交。这种情况导致了这个问题:

if (pt.x >= 0 && pt.y >= 0) 
      corners.push_back(pt); 

检查角坐标是否为非负值是不够的。相反,你必须确保角落在某些有意义的边界内;在你的情况下,这些可能是图像的边界。

if (pt.x >= 0 && pt.y >= 0 && pt.x <src.cols && pt.y < src.rows) 
      corners.push_back(pt); 

改变阈值和固定的条件后,我得到这样的结果:(Click to see an image)

警告

正如你所看到的,但另一个问题浮出水面 - 不是尽可能准确地检测到的角落他们可能是。您可以使用canny边缘提供的信息来获得您的优势。但我不想冒这个问题的范围,所以我会停下来。

我将我的解决方案命名为“快速修复”,因为它只能解决一个特定情况。如果您想要更通用的解决方案,并且如果您想继续使用算法,则必须在每次使用HoughLineP之前计算合理的阈值估计值。