使用OPENCV将图像的裁剪部分提取到单独的图像中
问题描述:
如何使用C++将每个作物提取到单独的图像中? (我的opencv的版本2.4.10)使用OPENCV将图像的裁剪部分提取到单独的图像中
我已经过滤的轮廓以匹配车牌的期望尺寸的宽度/高度比。 (第三幅图像 - 矩形3)
现在我需要将所有找到的候选图像提取到与“我”候选人具有相同大小的“我”单独图像中,以便我可以分割字符并使用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);
答
由于三木:
我在这里提供我的代码的一部分!
裁剪显示在代码
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);
Basicaly只是几个代码
//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;
对于每个边界框:'垫cropped_image_i(original_image(BBOX))',或深拷贝:'垫cropped_image_i = original_image(BBOX).clone( )' – Miki
这似乎工作可爱!非常感谢! 唯一的问题是在显示图像下一个作物似乎重叠最后一个作物。 是因为我把它表现为这个吗? imshow(“庄稼”,作物[j]); 经过测试,它仅在显示imshow中的图像时重叠 我要添加一个答案。 :) – Whatever