使用 OPENCV 将图像的裁剪部分提取到单独的图像中

Extract cropped parts of image into separate images using OPENCV

如何使用 C++ 将 EACH crop 提取到单独的图像中? (我的 Opencv 版本 2.4.10)

我过滤了轮廓以匹配车牌所需的尺寸 width/height 比例。 (第三张图片 - 矩形 3)

现在我需要将所有找到的候选项提取到 "i" 与 "i" 候选项具有相同大小的单独图像中,这样我就可以分割字符并使用 OCR 算法。

此图像的所需输出将是:

两张图片,每张都包含裁剪后的版本 (理想情况下提取一些额外添加的 width/height,如图所示) 找到的边界框。

这对我来说是个问题,如果我需要单独的图像,或者我可以简单地使用仅包含裁剪部分(和所示图像中的黑色背景)的整个图像来分割字符。

我在这里提供我的部分代码:

    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;

放大的结果可以在这里看到: