我正在做一项工作,在这里我必须找到感兴趣的区域(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);非常感谢。
发布于 2013-04-09 13:21:55
我假设您对隔离图像的数字感兴趣,并且希望手动指定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;
}https://stackoverflow.com/questions/15899799
复制相似问题