1

我是openCV的新手,我已经检测到纸张的边缘,但是在边缘上画线后我的结果图像变得模糊,我如何在纸张的边缘上画线,这样我的图像质量不受影响。

我错过了什么..

我的代码如下。

非常感谢。

在此处输入图像描述

-(void)forOpenCV
{
   if( imageView.image != nil )
   {

      cv::Mat greyMat=[self cvMatFromUIImage:imageView.image];
      vector<vector<cv::Point> > squares;

      cv::Mat img= [self debugSquares: squares: greyMat ];


      imageView.image =[self UIImageFromCVMat: img];

   }

}


- (cv::Mat) debugSquares: (std::vector<std::vector<cv::Point> >) squares : (cv::Mat &)image
{
NSLog(@"%lu",squares.size());

// blur will enhance edge detection

Mat blurred(image);
medianBlur(image, blurred, 9);

Mat gray0(image.size(), CV_8U), gray;
vector<vector<cv::Point> > contours;

// find squares in every color plane of the image
for (int c = 0; c < 3; c++)
{
    int ch[] = {c, 0};
    mixChannels(&image, 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(), cv::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<cv::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);
            }
        }
    }
}

NSLog(@"%lu",squares.size());


for( size_t i = 0; i < squares.size(); i++ )
{


    cv:: Rect rectangle = boundingRect(Mat(squares[i]));
    if(i==squares.size()-1)////Detecting Rectangle here
    {
        const cv::Point* p = &squares[i][0];


        int n = (int)squares[i].size();

         NSLog(@"%d",n);



        line(image, cv::Point(507,418), cv::Point(507+1776,418+1372), Scalar(255,0,0),2,8);

        polylines(image, &p, &n, 1, true, Scalar(255,255,0), 5, CV_AA);



        fx1=rectangle.x;
        fy1=rectangle.y;
        fx2=rectangle.x+rectangle.width;
        fy2=rectangle.y+rectangle.height;


        line(image, cv::Point(fx1,fy1), cv::Point(fx2,fy2), Scalar(0,0,255),2,8);


    }



}


return image;
}
4

2 回答 2

1

代替

Mat blurred(image);

你需要做

Mat blurred = image.clone();

因为第一行并没有复制图像,而是创建了第二个指向相同数据的指针。当您模糊图像时,您也在更改原始图像。相反,您需要做的是创建实际数据的真实副本并对该副本进行操作。

OpenCV 参考说明:

通过使用复制构造函数或赋值运算符,右侧可以是矩阵或表达式,见下文。再次,如介绍中所述,矩阵分配是 O(1) 操作,因为它只复制标题并增加引用计数器。

Mat::clone() 方法可用于在需要时获取矩阵的完整(又名深度)副本。

于 2012-11-21T13:07:52.233 回答
1

第一个问题很容易通过对原始图像的副本进行整个处理来解决。这样,在获得正方形的所有点后,您可以在原始图像上绘制线条,并且不会模糊。

第二个问题是裁剪,可以通过在原始图像中定义一个 ROI(感兴趣区域)然后将其复制到新的 Mat 来解决。我已经在这个答案中证明了这一点:

// Setup a Region Of Interest
cv::Rect roi;
roi.x = 50
roi.y = 10
roi.width = 400;
roi.height = 450;

// Crop the original image to the area defined by ROI
cv::Mat crop = original_image(roi);

cv::imwrite("cropped.png", crop);
于 2012-11-23T17:08:03.070 回答