OpenCV检测宽度最大的矩形

时间:2017-11-09 18:55:20

标签: java android opencv

我试图从该图片中获取此矩形: Wanted rectangle

使用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;
    }

我得到的最好的是: result image

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

原始图片: enter image description here

2 个答案:

答案 0 :(得分:3)

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

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

高/宽比率,因为文本框的高度相对小于宽度,文本框的区域

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

result

答案 1 :(得分:1)

这是几乎相同的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 rectangle

此外,您还可以使用其他信息:右侧是红色数字。