OpenCV C ++ / Obj-C:检测一张纸/方形检测 [英] OpenCV C++/Obj-C: Detecting a sheet of paper / Square Detection

查看:241
本文介绍了OpenCV C ++ / Obj-C:检测一张纸/方形检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的测试应用程序中成功地实现了OpenCV平方检测的例子,但是现在需要过滤输出,因为它的安静混乱 - 或者我的代码错了吗?



我对纸张的四个角点感兴趣,以减少偏斜(例如



原始图片



点击



代码:

 双角度(cv :: Point pt1,cv :: Point pt2,cv :: 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-
}

- (std :: vector< std :: vector< cv :: Point>>)findSquaresInImage:(cv :: Mat)_image
{
std :: vector< std :: vector< cv :: Point> >正方形;
cv :: Mat pyr,timg,gray0(_image.size(),CV_8U),gray;
int thresh = 50,N = 11;
cv :: pyrDown(_image,pyr,cv :: Size(_image.cols / 2,_image.rows / 2));
cv :: pyrUp(pyr,timg,_image.size());
std :: vector< std :: vector< cv :: Point> >轮廓;
for(int c = 0; c <3; c ++){
int ch [] = {c,0};
mixChannels(& timg,1,& gray0,1,ch,1);
for(int l = 0; l if(l == 0){
cv :: Canny(gray0,gray,0,thresh,5) ;
cv :: dilate(gray,gray,cv :: Mat(),cv :: Point(-1,-1));
}
else {
gray = gray0> =(1 + 1)* 255 / N;
}
cv :: findContours(gray,contour,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
std :: vector< cv :: Point>约;
for(size_t i = 0; i {
cv :: approxPolyDP(cv :: Mat(contours [i]),approx,arcLength (cv :: Mat(contours [i]),true)* 0.02,true);
if(approx.size()== 4&& fabs(contourArea(cv :: Mat(approx)))> 1000& cv :: isContourConvex(cv :: Mat )){
double maxCosine = 0;

for(int j = 2; j <5; j ++)
{
double cosine = fabs(angle(approximately [j%4],approximately [j-2 ],约[j-1]));
maxCosine = MAX(maxCosine,cosine);
}

if(maxCosine <0.3){
squares.push_back(approx);
}
}
}
}
}
return squares;
}

EDIT 17/08/2012:



要在图片上绘制检测到的正方形,请使用以下代码:

  cv :: Mat debugSquares(std :: vector< std :: vector< cv :: Point>> square,cv :: Mat image)
{
for = 0; i //绘制轮廓
cv :: drawContours(image,squares,i,cv :: Scalar(255,0,0),1 ,8,std :: vector< cv :: Vec4i(),0,cv :: Point());

//绘制边界rect
cv :: Rect rect = boundingRect(cv :: Mat(squares [i]));
cv :: rectangle(image,rect.tl(),rect.br(),cv :: Scalar(0,255,0),2,8,0);

//绘制旋转矩形
cv :: RotatedRect minRect = minAreaRect(cv :: Mat(squares [i]));
cv :: Point2f rect_points [4];
minRect.points(rect_points);
for(int j = 0; j <4; j ++){
cv :: line(image,rect_points [j],rect_points [(j + 1)%4],cv :: Scalar (0,0,255),1,8); // blue
}
}

return image;
}


解决方案

Stackoverflow,由于我无法找到相关的实现,我决定接受挑战。



我对OpenCV中的正方形演示和生成的C ++代码做了一些修改能够检测图像中的一张纸:

  void find_squares(Mat& image,vector< vector< Point> ;& square)
{
// blur会增强边缘检测
Mat blur(image);
medianBlur(image,blurred,9);

Mat gray0(blurred.size(),CV_8U),gray;
vector< vector< Point> >轮廓;

//在图像的每个颜色平面中找到正方形
for(int c = 0; c <3; c ++)
{
int ch [] = {c,0};
mixChannels(& blurred,1,& gray0,1,ch,1);

//尝试几个阈值级别
const int threshold_level = 2;
for(int l = 0; l< threshold_level; l ++)
{
//使用Canny而不是零阈值!
// Canny有助于捕获带渐变阴影的正方形
if(l == 0)
{
Canny(gray0,gray,10,20,3) //

// Dilate有助于消除边缘段之间的潜在空洞
dilate(gray,gray,Mat(),Point(-1,-1)
}
else
{
gray = gray0> =(1 + 1)* 255 / threshold_level;
}

//查找轮廓并将它们存储在列表中
findContours(gray,contour,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);

//测试轮廓
矢量< Point>约;
for(size_t i = 0; i {
//精确比例的近似轮廓
//轮廓周长
approxPolyDP(Mat(contoururs [i]),approx,arcLength(Mat(contoururs [i]),true)* 0.02,true);

//注意:使用区域的绝对值,因为
//区域可以为正或负 - 根据
//轮廓方向
if (approx.size()== 4&&
fabs(contourArea(Mat(approx)))> 1000&&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;

for(int j = 2; j <5; j ++)
{
double cosine = fabs(angle(approximately [j%4],approximately [j-2 ],约[j-1]));
maxCosine = MAX(maxCosine,cosine);
}

if(maxCosine <0.3)
squares.push_back(approx);
}
}
}
}
}


$ b b

在执行此过程之后,纸张将是向量<向量<点>中的最大平方。 >





我让你写函数找到最大的广场。 ;)


I successfully implemented the OpenCV square-detection example in my test application, but now need to filter the output, because it's quiet messy - or is my code wrong?

I'm interested in the four corner points of the paper for skew reduction (like that) and further processing …

Input & Output:

Original image:

click

Code:

double angle( cv::Point pt1, cv::Point pt2, cv::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);
}

- (std::vector<std::vector<cv::Point> >)findSquaresInImage:(cv::Mat)_image
{
    std::vector<std::vector<cv::Point> > squares;
    cv::Mat pyr, timg, gray0(_image.size(), CV_8U), gray;
    int thresh = 50, N = 11;
    cv::pyrDown(_image, pyr, cv::Size(_image.cols/2, _image.rows/2));
    cv::pyrUp(pyr, timg, _image.size());
    std::vector<std::vector<cv::Point> > contours;
    for( int c = 0; c < 3; c++ ) {
        int ch[] = {c, 0};
        mixChannels(&timg, 1, &gray0, 1, ch, 1);
        for( int l = 0; l < N; l++ ) {
            if( l == 0 ) {
                cv::Canny(gray0, gray, 0, thresh, 5);
                cv::dilate(gray, gray, cv::Mat(), cv::Point(-1,-1));
            }
            else {
                gray = gray0 >= (l+1)*255/N;
            }
            cv::findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
            std::vector<cv::Point> approx;
            for( size_t i = 0; i < contours.size(); i++ )
            {
                cv::approxPolyDP(cv::Mat(contours[i]), approx, arcLength(cv::Mat(contours[i]), true)*0.02, true);
                if( approx.size() == 4 && fabs(contourArea(cv::Mat(approx))) > 1000 && cv::isContourConvex(cv::Mat(approx))) {
                    double maxCosine = 0;

                    for( int j = 2; j < 5; j++ )
                    {
                        double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                        maxCosine = MAX(maxCosine, cosine);
                    }

                    if( maxCosine < 0.3 ) {
                        squares.push_back(approx);
                    }
                }
            }
        }
    }
    return squares;
}

EDIT 17/08/2012:

To draw the detected squares on the image use this code:

cv::Mat debugSquares( std::vector<std::vector<cv::Point> > squares, cv::Mat image )
{
    for ( int i = 0; i< squares.size(); i++ ) {
        // draw contour
        cv::drawContours(image, squares, i, cv::Scalar(255,0,0), 1, 8, std::vector<cv::Vec4i>(), 0, cv::Point());

        // draw bounding rect
        cv::Rect rect = boundingRect(cv::Mat(squares[i]));
        cv::rectangle(image, rect.tl(), rect.br(), cv::Scalar(0,255,0), 2, 8, 0);

        // draw rotated rect
        cv::RotatedRect minRect = minAreaRect(cv::Mat(squares[i]));
        cv::Point2f rect_points[4];
        minRect.points( rect_points );
        for ( int j = 0; j < 4; j++ ) {
            cv::line( image, rect_points[j], rect_points[(j+1)%4], cv::Scalar(0,0,255), 1, 8 ); // blue
        }
    }

    return image;
}

解决方案

This is a recurring subject in Stackoverflow and since I was unable to find a relevant implementation I decided to accept the challenge.

I made some modifications to the squares demo present in OpenCV and the resulting C++ code below is able to detect a sheet of paper in the image:

void find_squares(Mat& image, vector<vector<Point> >& squares)
{
    // blur will enhance edge detection
    Mat blurred(image);
    medianBlur(image, blurred, 9);

    Mat gray0(blurred.size(), CV_8U), gray;
    vector<vector<Point> > contours;

    // find squares in every color plane of the image
    for (int c = 0; c < 3; c++)
    {
        int ch[] = {c, 0};
        mixChannels(&blurred, 1, &gray0, 1, ch, 1);

        // try several threshold levels
        const int threshold_level = 2;
        for (int l = 0; l < threshold_level; l++)
        {
            // Use Canny instead of zero threshold level!
            // Canny helps to catch squares with gradient shading
            if (l == 0)
            {
                Canny(gray0, gray, 10, 20, 3); // 

                // Dilate helps to remove potential holes between edge segments
                dilate(gray, gray, Mat(), Point(-1,-1));
            }
            else
            {
                    gray = gray0 >= (l+1) * 255 / threshold_level;
            }

            // Find contours and store them in a list
            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

            // Test contours
            vector<Point> approx;
            for (size_t i = 0; i < contours.size(); i++)
            {
                    // approximate contour with accuracy proportional
                    // to the contour perimeter
                    approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

                    // Note: absolute value of an area is used because
                    // area may be positive or negative - in accordance with the
                    // contour orientation
                    if (approx.size() == 4 &&
                            fabs(contourArea(Mat(approx))) > 1000 &&
                            isContourConvex(Mat(approx)))
                    {
                            double maxCosine = 0;

                            for (int j = 2; j < 5; j++)
                            {
                                    double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                                    maxCosine = MAX(maxCosine, cosine);
                            }

                            if (maxCosine < 0.3)
                                    squares.push_back(approx);
                    }
            }
        }
    }
}

After this procedure is executed, the sheet of paper will be the largest square in vector<vector<Point> >:

I'm letting you write the function to find the largest square. ;)

这篇关于OpenCV C ++ / Obj-C:检测一张纸/方形检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆