【问题标题】:Detection ROI using openCV使用openCV检测ROI
【发布时间】:2013-03-31 18:09:43
【问题描述】:

我正在做一项工作,我必须找到感兴趣区域 (ROI),然后对图像执行阈值:

由于我不是计算机领域的,我遇到了一些困难。

我开始尝试通过以下代码找到投资回报率:

// code
string filename = "2011-06-11-09%3A12%3A15.387932.bmp";

Mat img = imread(filename)

if (!img.data)
{
    std::cout << "!!! imread não conseguiu abrir imagem: " << filename << std::endl;
    return -1;
}

cv::Rect roi;
roi.x = 0
roi.y = 90

roi.width = 400;
roi.height = 90;

cv::Mat crop = original_image(roi); 
cv::imwrite("2011-06-11-09%3A12%3A15.387932.bmp", crop);

非常感谢。

【问题讨论】:

    标签: c++ visual-c++ opencv image-processing


    【解决方案1】:

    我假设您有兴趣隔离图像的数字,并且您想手动指定 ROI(根据您编写的内容)。

    你可以用它use better coordinates for the ROI and crop that into a new cv::Mat 得到类似下面的输出:

    仅当您想隔离数字以便稍后进行一些识别时,对此图像执行阈值才有意义。 cv::inRange() 提供了一个很好的技术,它对所有通道(RGB 图像 == 3 通道)执行阈值操作。

    注意cv::Mat 按 BGR 顺序存储像素,在指定阈值时记住这一点很重要。

    作为一个简单的测试,您可以执行一个阈值从 B:70 G:90 R:100 到 B:140 G:140 R:140 得到以下输出:

    还不错!我稍微更改了您的代码以获得这些结果:

    #include <cv.h>
    #include <highgui.h>
    #include <iostream>
    
    int main()
    {
        cv::Mat image = cv::imread("input.jpg");
        if (!image.data)
        {
            std::cout << "!!! imread failed to load image" << std::endl;
            return -1;
        }
    
        cv::Rect roi;
        roi.x = 165;
        roi.y = 50;
        roi.width = 440;
        roi.height = 80;
    
        /* Crop the original image to the defined ROI */
    
        cv::Mat crop = image(roi);
        cv::imwrite("colors_roi.png", crop);
    
        /* Threshold the ROI based on a BGR color range to isolate yellow-ish colors */
    
        cv::Mat dest;
        cv::inRange(crop, cv::Scalar(70, 90, 100), cv::Scalar(140, 140, 140), dest);
        cv::imwrite("colors_threshold.png", dest);
    
        cv::imshow("Example", dest);    
        cv::waitKey();
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      相关资源
      最近更新 更多