OpenCV如何在轮廓内裁剪图像的一部分

7

enter image description here

我刚开始学习OpenCv。我想要裁剪一张图片中被红圈框起来的文字部分。你们能帮我找到解决方案吗?我已经尝试了一些方法,并成功将红圈裁剪并存储在一个mat矩阵中。

while(1)
    {
        capture>>img0;
        imshow("original", img0);
        imwrite("original.jpg", img0);
        cv::inRange(img0,cv::Scalar(0,0,100),cv::Scalar(76,85,255),img1);
        imshow("threshold.jpg", img1);
        imwrite("threshold.jpg", img1);
        // find the contours
        vector< vector<Point> > contours;
        findContours(img1, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

        Mat mask = Mat::zeros(img1.rows, img1.cols, CV_8UC1);

        drawContours(mask, contours, -1, Scalar(255), CV_FILLED);

        Mat crop(img0.rows, img0.cols, CV_8UC3);

        crop.setTo(Scalar(255,255,255));

        img0.copyTo(crop, mask);

        normalize(mask.clone(), mask, 0.0, 255.0, CV_MINMAX, CV_8UC3);

        imshow("mask", mask);
        imshow("cropped", crop);

        imwrite("mask.jpg", mask);
        imwrite("cropped.jpg", crop);

        if(waitKey(30)=='27')
        {
            break;
        }
    }
    return 0;`[original image[cropped image][1]`

我想从这张图片中裁剪出一段文字。请帮助我找到解决方案,并分享需要遵循的方法或步骤。

提前致谢。


那么你的问题是什么?因为看起来你已经尝试了一些方法,成功将红色圆形裁剪并存储在了一个 mat 中。 - lmiguelmh
我想要裁剪文本。 - paul ruben
我认为你需要包含一个示例图像来使你的问题更清晰。 - lmiguelmh
是的,我在尝试添加但是无法添加。 - paul ruben
嗨,Imiguelmh 我已经添加了图片。 - paul ruben
1个回答

2
如果您想只提取文本,可以尝试这个方法:
drawContours(mask, contours, -1, Scalar(255), CV_FILLED);
vector<Rect> boundRect( contours.size() );
for(int i=0;i<contours.size();i++)
    {
        boundRect[i] = boundingRect(contours[i]);//enclose in Rect
        Mat ROI,ROI_txt;
        if(boundRect[i].width>30 && boundRect[i].height>30)//ignore noise rects
        {
            ROI=img0(boundRect[i]);//extract Red circle on ROI
            inRange(ROI,Scalar(0,0,0),cv::Scalar(50,50,50),ROI_txt);
            //black colour threshold to extract black text
        }
    }

谢谢您的回复。我会尝试检查这个问题。 - paul ruben

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接