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