0
我有一个图像,我获得了它的二进制图像。我期望一个矩形的边界框,但我没有得到它。这是我的代码:检测OCR中的边界框?
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;
}
}
}
}`
这是我的形象:
我试着这条路。但有些情况下,图像是歪斜的。我想检测4个角点并使用它来旋转。你有什么建议吗? –
查找最大的斑点,并使用minAreaRect – Miki
或者简单地将此代码更改为使用RotatedRect而不是Rect,使用minAreaRect更改boundingRect。其余的应该是相同的(除了绘图结果) – Miki