简体   繁体   中英

OpenCV detect rectangle with the largest width

I'm trying to get this rectangle from that image: 想要的矩形

Found this solution using 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;
    }

The best i got was this: 结果图片

The problem is that the rectangle isn't perfect, maybe find the white numbers inside of that can be a best option, but i don't know too much of OpenCV.

Original image: 在此处输入图片说明

This is a very simple C++ implementation which tries to search for the text box. The accuracy of the detection depends on three parameters:

The thresh value provided to cv::threshold function to convert gray image to binary.

The height/width ratio , since the height of the text box is relatively smaller than the width, and the area of the text box.

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));

结果

Here is the almost same solution on java:

private Bitmap findRoi(Bitmap sourceBitmap) {

    Mat sourceMat = new Mat(sourceBitmap.getWidth(), sourceBitmap.getHeight(), CV_8UC3);
    Utils.bitmapToMat(sourceBitmap, sourceMat);

    Mat grayMat = new Mat(sourceBitmap.getWidth(), sourceBitmap.getHeight(), CV_8UC3);
    Imgproc.cvtColor(sourceMat, grayMat, Imgproc.COLOR_BGR2GRAY);
    Imgproc.threshold(grayMat, grayMat, 125, 200, Imgproc.THRESH_BINARY);

    // find contours
    List<MatOfPoint> whiteContours = new ArrayList<>();
    Rect largestRect = null;
    Imgproc.findContours(grayMat, whiteContours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    // find appropriate bounding rectangles
    for (MatOfPoint contour : whiteContours) {
        RotatedRect boundingRect = Imgproc.minAreaRect(new MatOfPoint2f(contour.toArray()));
        double rectangleArea = boundingRect.size.area();

        // test min ROI area in pixels
        if (rectangleArea > 10000) {
            Point rotated_rect_points[] = new Point[4];
            boundingRect.points(rotated_rect_points);
            Rect rect = Imgproc.boundingRect(new MatOfPoint(rotated_rect_points));

            // test horizontal ROI orientation and aspect ratio
            if (rect.width > 3 * rect.height) {
                if (largestRect == null) {
                    largestRect = rect;
                } else {
                    if (rect.width > largestRect.width) {
                        largestRect = rect;
                    }
                }
            }
        }
    }

    Mat roiMat = new Mat(sourceMat, largestRect);

    Bitmap bitmap = Bitmap.createBitmap(roiMat.cols(), roiMat.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(roiMat, bitmap);
    return bitmap;
}

ROI矩形

Also, you can use additional information: red number places on the right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM