使用openCV检测ROI

use*_*242 2 c++ opencv image-processing visual-c++

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

在此输入图像描述

因为我不是来自计算领域,所以我遇到了一些困难.

我开始尝试通过以下代码找到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);
Run Code Online (Sandbox Code Playgroud)

非常感谢你.

kar*_*lip 8

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

您可以使用更好的ROI坐标并将其裁剪成新的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;
}
Run Code Online (Sandbox Code Playgroud)