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

标签 opencv crop

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

enter image description here

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

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

此图像的所需输出将是:

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

enter image description here

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

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

    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++;

结果(尚未调整到更大的区域)如下所示: Crops

要放大,就用(或者类似的属性,这个需要多测试)

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;

放大的结果可以在这里看到: enter image description here

关于opencv - 使用 OPENCV 将图像的裁剪部分提取到单独的图像中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37185456/

相关文章:

opencv - 为什么 CLion 不将我的项目与 Windows 版 OpenCV 链接?

c# - 如何在 EmguCv 中使用 OpenCv 3.0 CvInvoke.HoughLines 方法

python - 图像处理中的多线程——视频python opencv

iOS - 将图像裁剪到检测到的面部时出现问题

html - 使用 CSS(响应式)裁剪和缩放图像的特定区域

android - 在 Android 中获取字母作为路径的轮廓

python - 如何填充图像中的孔

c++ - 在 OpenCv 中乘以图像并在其上应用拉普拉斯滤波器

javascript - 如何在 JS/jQuery 上裁剪具有透明背景的 JPG 和 PNG

python - PIL裁剪和粘贴问题: Cropping doesn't create a cropped image