opencv - OpenCV文档检测FIX

标签 opencv

我需要像这里的OpenCV C++/Obj-C: Detecting a sheet of paper / Square Detection

当我的背景和前景不相同时,我的代码就像一个魅力一样工作,但是如果我的背景与文档的颜色几乎相同,那么它将无法工作。

这是带有米色bg +几乎是米色文档的图片,不起作用。.有人可以帮忙解决这个问题吗?

https://i.imgur.com/81DrIIK.jpg

代码在这里:

vector<Point> getPoints(Mat image)
{
    int width = image.size().width;
    int height = image.size().height;
    Mat image_proc = image.clone();
    vector<vector<Point> > squares;

    // blur will enhance edge detection
    Mat blurred(image_proc);
    medianBlur(image_proc, 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);
                }
            }
        }

        double largest_area = -1;
        int largest_contour_index = 0;
        for(int i=0;i<squares.size();i++)
        {
            double a =contourArea(squares[i],false);
            if(a>largest_area)
            {
                largest_area = a;
                largest_contour_index = i;
            }
        }

        __android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "Scaning size() %d",squares.size());
        vector<Point> points;
        if(squares.size() > 0)
        {
            points = squares[largest_contour_index];
        }
        else
        {
            points.push_back(Point(0, 0));
            points.push_back(Point(width, 0));
            points.push_back(Point(0, height));
            points.push_back(Point(width, height));
        }

        return points;
    }
}

谢谢

最佳答案

您可以在thresholdS空间中进行HSV-color-space操作。 https://en.wikipedia.org/wiki/HSL_and_HSV#General_approach

我只是按照以下方式分割了BGRHSV的 channel 。需要更多的操作。

enter image description here

enter image description here

关于opencv - OpenCV文档检测FIX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47381639/

相关文章:

c++ - OpenCV Clang mat.hpp 错误 : call to member function 'ptr' is ambiguous

C++;浮点异常;没有./运算符

android - 使用 OpenCV 进行模板匹配

c++ - GaussianBlur 不应该是对称的吗?

c++ - 填充与矩形相交的蒙版轮廓

actionscript-3 - 使用类似 haar 的功能在 Flash 中进行对象检测

c++ - OpenCV 环境变量已更改

c++ - OpenCV:分割 cv::Mat

opencv - 使用cvMinMaxLoc()识别白色区域

python - 在 OpenCV 和 Python 中控制视频流的对比度和亮度