如何使用 C++ 将 EACH crop 提取到单独的图像中? (我的Opencv版本是2.4.10)
我过滤了轮廓以匹配车牌所需的尺寸宽度/高度比。 (第三张图片 - 矩形 3)
现在我需要将所有找到的候选项提取到与“i”候选项具有相同大小的“i”个单独图像中,这样我就可以分割字符并使用 OCR 算法。
此图像的所需输出将是:
两张图片,每张都包含裁剪后的版本 (理想情况下提取一些额外的宽度/高度,如图所示) 找到的边界框。
这对我来说是个问题,如果我需要单独的图像,或者我可以简单地使用仅包含裁剪部分(和所示图像中的黑色背景)的整个图像来分割字符。
我在这里提供我的部分代码:
findContours(crop, contours, hierarchy, CV_RETR_TREE,
CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<Point2f> ContArea(contours.size());
for (int i = 0; i < contours.size(); i++) {
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
boundRect[i] = boundingRect(Mat(contours_poly[i]));
}
// Draw polygonal contour + filled bonding rects
Mat drawing4 = Mat::zeros(src_gray.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));
rectangle(drawing4, boundRect[i].tl(), boundRect[i].br(), color,
CV_FILLED, 1, 0);
}
imshow("Rectangles4", drawing4);
float ratio;
Mat drawing3 = Mat::zeros(crop.size(), CV_8UC3);
// Draw bonding rects
for (int i = 0; i < contours.size(); i++) {
Scalar color = Scalar(0, 255, 0);
double a = contourArea(contours[i]);
ratio = (float) boundRect[i].width / (float) boundRect[i].height;
//check for min, max size of area and its ratios
if ((a > 200 && a < 2600) && ((ratio >= 1.3) && (ratio <= 10))) {
printf("a: %f ratios: %f", a, ratio);
//drawContours(drawing3, contours_poly, (int) i, color, 1, 8,
vector<Vec4i>(), 0, Point());
rectangle(drawing3, boundRect[i].tl(), boundRect[i].br(), color,
CV_FILLED, 1, 0);
}
}
imshow("Rectangles3", drawing3);
最佳答案
感谢 Miki 提供答案!
裁剪显示在代码的编辑部分
float ratio;
int j=0;
Mat crops[10];
Mat drawing3 = Mat::zeros(crop.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(0,255,0);
double a = contourArea(contours[i]);
ratio = (float)boundRect[i].width/(float)boundRect[i].height;
if ((a > 200 && a < 2600)&&((ratio>=1.3)&&(ratio<=10))){
printf(" a: %f ratios: %f image%d",a,ratio,i);
drawContours(drawing3, contours_poly, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point());
rectangle(drawing3, boundRect[i].tl(), boundRect[i].br(), color,
CV_FILLED, 1, 0);
Mat cropx(src_resized(boundRect[i]));
crops[j]=cropx;
imshow("crops",crops[0]);
if (j==1) imshow("crops1",crops[1]);
j++;
waitKey(0);
}
}
imshow("Rectangles3", drawing3);
基本上只需要多几行代码
//outside the loop
int j=0;
Mat crops[10];
//inside the loop
Mat cropx(src_resized(boundRect[i]));
crops[j]=cropx;
j++;
要放大,就用(或者类似的属性,这个需要多测试)
boundRect[i].x=boundRect[i].x -boundRect[i].width/4;
boundRect[i].y=boundRect[i].y -boundRect[i].height/4;
boundRect[i].width=boundRect[i].width +boundRect[i].width/2;
boundRect[i].height=boundRect[i].height +boundRect[i].height/2;
关于opencv - 使用 OPENCV 将图像的裁剪部分提取到单独的图像中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37185456/