检测OCR中的边界框?

时间:2016-01-14 14:25:36

标签: c++ opencv

我有一张图片,我获得了它的二进制图像。我期待一个矩形的边界框,但我没有得到它。这是我的代码:

vector<vector<Point>> contours;
Vec4i hierarchy;
findContours(binary, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
/*Mat drawing = Mat::zeros(binary.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++)
{
    Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
    drawContours(drawing, contours, i, color, 1, 8, hierarchy, 0, Point());
}
imshow("contours", drawing);*/

vector<Point> approx, approxRectangle;
Rect bounding_rect(0, 0, 0, 0);
double max_area = 0;
for (int i = 0; i < contours.size(); i++)// xet tung contour
{
    approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

    if (approx.size() == 4 && isContourConvex(Mat(approx)))
    {
        Rect box = boundingRect(contours[i]);
        if (bounding_rect.area() == 0){
            bounding_rect = box;
            approxRectangle = approx;
        }
        else{
            if (bounding_rect.area() < box.area()){
                bounding_rect = box;
                approxRectangle = approx;
            }
        }
    }
}`

这是我的形象:

1 个答案:

答案 0 :(得分:2)

您没有获得所需的结果,因为您正在寻找几乎矩形轮廓,但这不起作用,因为您感兴趣的轮廓不是矩形的。您可以看到(蓝色)该轮廓的近似值(在我的二值化图像上获得):

enter image description here

这表明这不是一个可靠的约束。

你可以很容易地解决这个问题,在这种情况下,计算每个轮廓的边界框,并保持最大(绿色):

enter image description here

代码:

#include <opencv2/opencv.hpp>
#include <iostream>
#include <algorithm>
using namespace std;
using namespace cv;

int main()
{
    // Load image
    Mat3b img = imread("path_to_image");

    // Convert to grayscale
    Mat1b binary;
    cvtColor(img, binary, COLOR_BGR2GRAY);

    // Binarize (remove anti-aliasing artifacts)
    binary = binary > 200;

    // Find contours
    vector<vector<Point>> contours;
    findContours(binary.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));

    // Compute the bounding boxes
    vector<Rect> boxes;
    for (int i = 0; i < contours.size(); ++i)
    {
        boxes.push_back(boundingRect(contours[i]));
    }

    // Find index of largest contours
    int idx_largest_box = distance(boxes.begin(), max_element(boxes.begin(), boxes.end(), [](const Rect& lhs, const Rect& rhs) {
        return lhs.area() < rhs.area();
    }));

    // Draw largest box
    rectangle(img, boxes[idx_largest_box], Scalar(0,255,0));

    imshow("Result", img);
    waitKey();

    return 0;
}