根据现有的标签(而不是二进制图像)查找使用 OpenCV 连接的组件
Find connected components with OpenCV based on existed labeling, but not binary image
OpenCV有一个在二进制图像上查找连接组件的功能:(cv::connectedComponents(((,但它不考虑现有的标签。仅在具有相同标签的像素内查找连接组件的正确方法是什么?
例如,我有代码:
Mat test = Mat::zeros(1, 4, DataType<int>::type);
test.at<int>(0, 0) = 1;
test.at<int>(0, 1) = 2;
test.at<int>(0, 2) = 0;
test.at<int>(0, 3) = 1;
test.convertTo(test, CV_8U);
connectedComponents(test, test);
std::cout << test << std::endl;
它有输入矩阵[1, 2, 0, 1]
,并将其标记为[1, 1, 0, 2]
。但我想得到[1, 2, 0, 3]
.有没有办法用OpenCV做到这一点?
我的问题解决方案:
Mat connected_components(const Mat &labels)
{
Mat res, input;
labels.convertTo(input, CV_8U);
connectedComponents(input, res);
res.convertTo(res, DataType<int>::type);
double n_labels;
minMaxLoc(res, nullptr, &n_labels);
res += labels * (n_labels + 1);
std::map<int, int> new_ids;
for (int row = 0; row < labels.rows; ++row)
{
auto row_res_data = res.ptr<int>(row);
for (int col = 0; col < labels.cols; ++col)
{
auto cur_lab = row_res_data[col];
if (cur_lab == 0)
continue;
auto iter = new_ids.emplace(cur_lab, new_ids.size() + 1);
row_res_data[col] = iter.first->second;
}
}
return res;
}
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 在C++中查找文件
- 在图像中查找相似的(大)模式
- 根据现有的标签(而不是二进制图像)查找使用 OpenCV 连接的组件
- 在屏幕上查找图像
- OPENCV C - 查找图像中包含的图像
- 用于在黑白图像上查找未连接区域(岛屿)的算法
- 在opencv c++中查找图像卷积时出现分割错误(核心转储)
- 图像处理 - OpenCV - 查找图像中圆的面积和中心坐标
- 使用 OpenCV 库查找图像的尺寸(以英寸为单位)
- OpenCV:查找二进制 Mat 图像的所有非零坐标
- 为什么我的 OpenCL 3D 图像查找不起作用?
- 将图像重新映射到不同大小并查找像素位置
- 使用逐位运算符查找bmp图像的宽度和高度
- OpenCV如何在二进制图像中查找连接组件的列表
- 在阈值图像中查找行间距- R/ c++图像处理
- opencvc++在阈值图像中查找移动
- 根据颜色查找图像中的所有对象
- 查找表(LUT)是否应该是图像的属性(在架构中)