Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在IOS中获得完美的文档边缘检测?_Ios_Opencv - Fatal编程技术网

如何在IOS中获得完美的文档边缘检测?

如何在IOS中获得完美的文档边缘检测?,ios,opencv,Ios,Opencv,首先,如果有人问这个问题,我很抱歉。我正在开发一个可以检测文档角落的应用程序。我现在正在使用openCV检测边缘。我使用openCV实现了这一点,但我没有得到完美的结果 void find_squares(Mat& image, cv::vector<cv::vector<cv::Point>>&squares) { // blur will enhance edge detection Mat blurred(image);

首先,如果有人问这个问题,我很抱歉。我正在开发一个可以检测文档角落的应用程序。我现在正在使用openCV检测边缘。我使用openCV实现了这一点,但我没有得到完美的结果

   void find_squares(Mat& image, cv::vector<cv::vector<cv::Point>>&squares)
{
    // blur will enhance edge detection
    Mat blurred(image);
    //cv::resize(image, image, cvSize(0.25, 0.25));
    Mat gray0(blurred.size(), CV_8U), gray;

    //medianBlur(image, blurred, 9); //default 9;
    GaussianBlur(image, blurred, cvSize(9, 9), 2.0,2.0);

    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(&blurred, 1, &gray0, 1, ch, 1);

        // try several threshold levels
        const int threshold_level = 4;
        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);
                }
            }
        }
    }
}
我也试过,但我能从这个开始

我的代码检测文档的角,但不是完美的结果

   void find_squares(Mat& image, cv::vector<cv::vector<cv::Point>>&squares)
{
    // blur will enhance edge detection
    Mat blurred(image);
    //cv::resize(image, image, cvSize(0.25, 0.25));
    Mat gray0(blurred.size(), CV_8U), gray;

    //medianBlur(image, blurred, 9); //default 9;
    GaussianBlur(image, blurred, cvSize(9, 9), 2.0,2.0);

    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(&blurred, 1, &gray0, 1, ch, 1);

        // try several threshold levels
        const int threshold_level = 4;
        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);
                }
            }
        }
    }
}
void find_squares(材质和图像,cv::向量和正方形)
{
//模糊将增强边缘检测
Mat模糊(图像);
//cv::resize(图像,图像,cvSize(0.25,0.25));
Mat灰色0(模糊的大小(),CV_8U),灰色;
//medianBlur(图像,模糊,9);//默认值9;
高斯模糊(图像,模糊,cvSize(9,9),2.0,2.0);
矢量等值线;
//在图像的每个颜色平面中查找正方形
对于(int c=0;c<3;c++)
{
int ch[]={c,0};
混音通道(&模糊,1,&灰色,0,1,通道,1);
//尝试几个阈值级别
const int threshold_level=4;
对于(int l=0;l=(l+1)*255/阈值\u级;
}
//找到等高线并将其存储在列表中
findContours(灰色、等高线、等高线列表、等高线链近似简单);
//测试轮廓
向量近似;
对于(size_t i=0;i1000&&
isContourConvex(材料(近似)))
{
双最大余弦=0;
对于(int j=2;j<5;j++)
{
双余弦=fabs(角度(约[j%4],约[j-2],约[j-1]);
最大余弦=最大值(最大余弦,余弦);
}
如果(最大余弦<0.3)
正方形。推回(大约);
}
}
}
}
}
因此,我的问题是:

1) 有没有其他图书馆可以这样做

2) 上面的代码有什么问题吗?我应该在检测前添加一些图像处理吗


3) 你能做到吗?如果可以,那么是否有用于边缘检测的样本代码来源?

您能否更详细地解释使用该代码处理的结果如何不令人满意?你说你试过BradLarson GPUImage,但是你已经有代码了,不是吗?你不能执行它吗?我已经使用了实时检测边缘的教程使用openCV我得到了这个结果,请帮助我们能够执行它,但是文档的边缘检测不太合适。它只在页面背景暗时检测,但结果不太频繁。嗨,我正在创建类似的应用程序,并使用你建议的代码。但面对类似的问题,你解决了吗?请帮助我,如果你已经这样做了,这将是很大的帮助。提前准备好。