OpenCV检测最大宽度的矩形

use*_*394 2 java android opencv

我试图从该图像中获取这个矩形: 想要矩形

使用 OpenCV 找到了这个解决方案:

 private Bitmap findRectangle(Bitmap src) throws Exception {
        Mat imageMat = new Mat();
        Utils.bitmapToMat(src, imageMat);

        Mat imgSource=imageMat.clone();

        Imgproc.cvtColor(imgSource, imageMat, Imgproc.COLOR_BGR2GRAY);

        //find the contours
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Imgproc.findContours(imageMat, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);

        Imgproc.Canny(imageMat,imageMat,0,255);
        Bitmap canny=Bitmap.createBitmap(imageMat.cols(),imageMat.rows(),Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(imageMat,canny);

        Imgproc.GaussianBlur(imageMat, imageMat, new  org.opencv.core.Size(1, 1), 2, 2);
        Bitmap blur=Bitmap.createBitmap(imageMat.cols(),imageMat.rows(),Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(imageMat,blur);

        MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point

        for (int idx = 0; idx < contours.size(); idx++) {
            temp_contour = contours.get(idx);
            //check if this contour is a square

            MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );

            int contourSize = (int)temp_contour.total();
            MatOfPoint2f approxCurve_temp = new MatOfPoint2f();
            Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize*0.05, true);

            if (approxCurve_temp.total() == 4) {
                MatOfPoint points = new MatOfPoint( approxCurve_temp.toArray() );
                Rect rect = Imgproc.boundingRect(points);
                Imgproc.rectangle(imgSource, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height), new Scalar(255, 0, 0, 255), 3);

            }

        }
        Bitmap analyzed=Bitmap.createBitmap(imgSource.cols(),imgSource.rows(),Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(imgSource,analyzed);

        return analyzed;
    }
Run Code Online (Sandbox Code Playgroud)

我得到的最好的是: 结果图像

问题是矩形并不完美,也许发现里面的白色数字可能是最好的选择,但我对 OpenCV 不太了解。

原图: 在此输入图像描述

zin*_*rod 5

这是一个非常简单的 C++ 实现,它尝试搜索文本框。检测的准确性取决于三个参数:

提供给将灰度图像转换为二进制的函数的值。cv::threshold

/宽比,因为文本框的高度相对小于宽度,以及文本框的面积。

Mat img = imread("image.jpg",-1), gray, binary;

/*pre-processing steps*/
uchar thresh = 80;
cvtColor(img, gray, cv::COLOR_BGR2GRAY);
GaussianBlur(gray, gray, Size(7,7), 0);
// change the thresh value to fine tune this program for your images
threshold(gray, binary, thresh, 255, cv::THRESH_BINARY_INV);

/*contour searching*/
std::vector<std::vector<Point>> contours;
std::vector<Vec4i> hierarchy;
findContours(binary, contours, hierarchy, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);

/*Filtering contours based on height/width ratio and bounding box area*/
std::vector<Rect> boxes;
double box_ratio = 0.3;
int box_area = 20000;

for(auto& cnt : contours)
{
    auto box = minAreaRect(cnt).boundingRect();
    // we are searching for a rectangle which a has relatively large area,
    // and the height is smaller than the width, so the
    // height/width ratio should be small. Change the these two values for fine tuning
    if((min(box.width,box.height)/double(max(box.width,box.height)) < box_ratio) && box.area() > box_area )
    {
        boxes.push_back(box);
    }

}

Mat txt_box = img(boxes.at(0));
Run Code Online (Sandbox Code Playgroud)

结果