如何确定感兴趣的区域,然后使用OpenCV裁剪图像

2024-05-16 08:09:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我问了一个类似的问题here,但这个问题更多地集中在tesseract上。

我有一个样本图像如下。我想把白色正方形作为我感兴趣的区域,然后裁剪出那个部分(正方形)并用它创建一个新的图像。我将使用不同的图像,所以广场不会总是在所有图像的同一位置。所以我需要找出正方形的边缘。

enter image description here

我可以执行哪些预处理方法来获得结果?


Tags: 方法图像区域here感兴趣边缘样本广场
2条回答

使用您的测试图像,我可以通过一个简单的erosion操作去除所有噪声。

在那之后,Mat上进行一个简单的迭代来查找角点像素是很简单的,我在this answer上讨论过这个问题。出于测试目的,我们可以在这些点之间绘制绿色线,以显示我们对原始图像感兴趣的区域:

最后,我在原始图像中设置ROI并裁剪出该部分。

最终结果显示在下图中:

我编写了一个示例代码,使用OpenCV的<强> C++接口< /强>来执行此任务。我对你将这段代码翻译成Python的技巧很有信心。如果你做不到这一点,就忘了代码,继续使用我在这个答案上共享的roadmap

#include <cv.h>
#include <highgui.h>

int main(int argc, char* argv[])
{
    cv::Mat img = cv::imread(argv[1]);
    std::cout << "Original image size: " << img.size() << std::endl;

    // Convert RGB Mat to GRAY
    cv::Mat gray;
    cv::cvtColor(img, gray, CV_BGR2GRAY);
    std::cout << "Gray image size: " << gray.size() << std::endl;

    // Erode image to remove unwanted noises
    int erosion_size = 5;
    cv::Mat element = cv::getStructuringElement(cv::MORPH_CROSS,
                                       cv::Size(2 * erosion_size + 1, 2 * erosion_size + 1),
                                       cv::Point(erosion_size, erosion_size) );
    cv::erode(gray, gray, element);

    // Scan the image searching for points and store them in a vector
    std::vector<cv::Point> points;
    cv::Mat_<uchar>::iterator it = gray.begin<uchar>();
    cv::Mat_<uchar>::iterator end = gray.end<uchar>();
    for (; it != end; it++)
    {
        if (*it) 
            points.push_back(it.pos()); 
    }

    // From the points, figure out the size of the ROI
    int left, right, top, bottom;
    for (int i = 0; i < points.size(); i++)
    {
        if (i == 0) // initialize corner values
        {
            left = right = points[i].x;
            top = bottom = points[i].y;
        }

        if (points[i].x < left)
            left = points[i].x;

        if (points[i].x > right)
            right = points[i].x;

        if (points[i].y < top)
            top = points[i].y;

        if (points[i].y > bottom)
            bottom = points[i].y;
    }
    std::vector<cv::Point> box_points;
    box_points.push_back(cv::Point(left, top));
    box_points.push_back(cv::Point(left, bottom));
    box_points.push_back(cv::Point(right, bottom));
    box_points.push_back(cv::Point(right, top));

    // Compute minimal bounding box for the ROI
    // Note: for some unknown reason, width/height of the box are switched.
    cv::RotatedRect box = cv::minAreaRect(cv::Mat(box_points));
    std::cout << "box w:" << box.size.width << " h:" << box.size.height << std::endl;

    // Draw bounding box in the original image (debugging purposes)
    //cv::Point2f vertices[4];
    //box.points(vertices);
    //for (int i = 0; i < 4; ++i)
    //{
    //    cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
    //}
    //cv::imshow("Original", img);
    //cv::waitKey(0);

    // Set the ROI to the area defined by the box
    // Note: because the width/height of the box are switched, 
    // they were switched manually in the code below:
    cv::Rect roi;
    roi.x = box.center.x - (box.size.height / 2);
    roi.y = box.center.y - (box.size.width / 2);
    roi.width = box.size.height;
    roi.height = box.size.width;
    std::cout << "roi @ " << roi.x << "," << roi.y << " " << roi.width << "x" << roi.height << std::endl;

    // Crop the original image to the defined ROI
    cv::Mat crop = img(roi);

    // Display cropped ROI
    cv::imshow("Cropped ROI", crop);
    cv::waitKey(0);

    return 0;
}

鉴于文本是唯一大的blob,而其他所有内容都不超过一个像素,一个简单的形态学打开就足够了

你可以这样做in opencvwith imagemagic

之后,白色矩形应该是图像中唯一剩下的东西。您可以使用opencvs findcontours、opencv的CvBlobs库或imagemagick-crop函数找到它

这是你的图像,有两个侵蚀步骤,然后是两个扩张步骤: enter image description here 您可以简单地将此图像插入到opencv findContours函数中,如Squares tutorial example中所示,以获取位置

相关问题 更多 >