为什么OpenCV没有针对区域的专用数据结构?
我使用MVTec的Halcon图像处理库已经半年了,使用OpenCV已经1年了 1。我发现Halcon比OpenCV好得多的一点是OpenCV没有专门针对区域的数据结构。 如果我只想在一个大空间中存储一个小区域,这将是很大的浪费。除了Mat类之外,OpenCV不应该有一些专门的区域数据结构吗 2。第二个问题是前一个问题的结果,即OpenCV很难遍历区域。为什么OpenCV没有针对区域的专用数据结构?,opencv,structure,space,Opencv,Structure,Space,我使用MVTec的Halcon图像处理库已经半年了,使用OpenCV已经1年了 1。我发现Halcon比OpenCV好得多的一点是OpenCV没有专门针对区域的数据结构。 如果我只想在一个大空间中存储一个小区域,这将是很大的浪费。除了Mat类之外,OpenCV不应该有一些专门的区域数据结构吗 2。第二个问题是前一个问题的结果,即OpenCV很难遍历区域。 想象一下这样的场景,在执行阈值之后,我有10个连接的区域块,我想迭代这10个块来处理它们中的每一个。据我所知,我必须首先使用FindConto
想象一下这样的场景,在执行阈值之后,我有10个连接的区域块,我想迭代这10个块来处理它们中的每一个。据我所知,我必须首先使用FindContour获得每个区域的所有轮廓,然后使用drawContour获得该区域。那么,我可以说这些区域是由等高线数据存储的,每次我想要得到这些区域时,我必须调用drawContours吗
Mat myImage = imread("Path_To_Source_Image");
threshold(myImage, region, 128, 1, THRESH_BINARY);
vector<vector<Point>> contours;
findContours(region, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size, i++){
Mat oneBlock(myImage.size(), CV_8U);
drawContours(oneBlock, contours, i, Scalar(255), -1);
// Now I finally get my region
// ***************************************
//
// Do my image procesing for that region
//
// ***************************************
}
Mat myImage=imread(“路径到源图像”);
阈值(myImage,region,128,1,THRESH_二进制);
矢量等值线;
findContours(区域、轮廓、外部翻新、链近似简单);
对于(int i=0;i
表示区域的常用方法是使用Mat1i
(又称CV\u 32S
类型的Mat
),其中包含区域索引,即标签
然后,只需使用以下命令即可访问第i个区域:Mat1b region\u mask=(labels==i)代码>
使用OpenCV<3.0,您可以使用findConturs
和drawContours
创建标签
图像:
#include <opencv2\opencv.hpp>
#include <vector>
using namespace cv;
using namespace std;
int main()
{
// Read image from file
Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
Mat1b region;
threshold(img, region, 200, 255, THRESH_BINARY);
vector<vector<Point>> contours;
findContours(region.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
Mat1i labels(img.rows, img.cols, int(0));
int n_labels = contours.size();
for (int i = 0; i < contours.size(); ++i)
{
drawContours(labels, contours, i, Scalar(i + 1), CV_FILLED); // Note the +1, since 0 is the background
}
// Now you can get your regiones as:
// Label "0" is the background
for (int i = 1; i <= n_labels; ++i)
{
Mat1b region_mask = (labels == i);
imshow("Region", region_mask);
waitKey();
}
return 0;
}
不幸的是,我没有Halcon的经验。你能更好地解释什么是区域吗?它们是某种子图像还是更像稀疏像素位置?区域上可以进行何种处理?根据我的理解/直觉,子图像和遮罩的组合可能是区域。“每次我想拿回该区域时,我必须调用drawContours?”-为什么不只记住drawContours的结果?
#include <opencv2\opencv.hpp>
#include <vector>
using namespace cv;
using namespace std;
int main()
{
// Read image from file
Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
Mat1b region;
threshold(img, region, 200, 255, THRESH_BINARY);
Mat1i labels;
int n_labels = connectedComponents(region, labels);
// Now you can get your regiones as:
// Label "0" is the background
for (int i = 1; i <= n_labels; ++i)
{
Mat1b region_mask = (labels == i);
imshow("Region", region_mask);
waitKey();
}
return 0;
}