如何在SuperpixelSLIC中找到分段的唯一标签
How to find unique labels for segments in SuperpixelSLIC
我使用cv::ximgproc::SuperpixelSLIC opencvc++来生成图像段。我希望每个段的标签是唯一的。这是我的代码。
Mat segmentImage() {
int num_iterations = 4;
int prior = 2;
bool double_step = false;
int num_levels = 10;
int num_histogram_bins = 5;
int width, height;
width = h1.size().width;
height = h1.size().height;
seeds = createSuperpixelSLIC(h1);
Mat mask;
seeds->iterate(num_iterations);
Mat labels;
seeds->getLabels(labels);
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
if (labels.at<int>(i, j) == 0)
cout << i << " " << j << " " << labels.at<int>(i, j) << endl;
}
}
ofstream myfile;
myfile.open("label.txt");
myfile << labels;
myfile.close();
seeds->getLabelContourMask(mask, false);
h1.setTo(Scalar(0, 0, 255), mask);
imshow("result", h1);
imwrite("result.png", h1);
return labels;
}
在label.txt文件中,我观察到标签0已被赋予两个段(即段包括像素(0,0)和像素(692,442))。这两段距离相当远。
这是正常的事情还是我的代码不正确?请帮我找到每个片段的唯一标签
你本质上需要的是一个连接组件算法。如果不知道您使用的确切SLIC实现,SLIC通常倾向于产生断开连接的超像素,即具有相同标签的断开连接的段。我使用的一个简单的解决方案是连接组件算法形式在这里:https://github.com/davidstutz/matlab-multi-label-connected-components(最初从这里:http://xenia.media.mit.edu/~rahimi/connected/)。注意,这个存储库包含一个MatLab包装器。在您的情况下,您只需要connected_components.h
和以下代码:
#include "connected_components.h"
// ...
void relabelSuperpixels(cv::Mat &labels) {
int max_label = 0;
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
if (labels.at<int>(i, j) > max_label) {
max_label = labels.at<int>(i, j);
}
}
}
int current_label = 0;
std::vector<int> label_correspondence(max_label + 1, -1);
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
int label = labels.at<int>(i, j);
if (label_correspondence[label] < 0) {
label_correspondence[label] = current_label++;
}
labels.at<int>(i, j) = label_correspondence[label];
}
}
}
int relabelConnectedSuperpixels(cv::Mat &labels) {
relabelSuperpixels(labels);
int max = 0;
for (int i = 0; i < labels.rows; ++i) {
for (int j = 0; j < labels.cols; ++j) {
if (labels.at<int>(i, j) > max) {
max = labels.at<int>(i, j);
}
}
}
ConnectedComponents cc(2*max);
cv::Mat components(labels.rows, labels.cols, CV_32SC1, cv::Scalar(0));
int component_count = cc.connected<int, int, std::equal_to<int>, bool>((int*) labels.data, (int*) components.data, labels.cols,
labels.rows, std::equal_to<int>(), false);
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
labels.at<int>(i, j) = components.at<int>(i, j);
}
}
// component_count would be the NEXT label index, max is the current highest!
return component_count - max - 1;
}
在获取到的标签中输入relabelConnectedSuperpixels
。
相关文章:
- 分段故障(堆芯转储)矢量
- 数组的指针从不分段故障
- 何时在引用或唯一指针上使用移动语义
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 在线编译器中的分段C++没有打印消息
- 计算排序向量的向量中唯一值的计数
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 通过组合不同的类型来创建唯一的id
- 使用Unique_ptr确保工厂中的对象唯一
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 如何在SuperpixelSLIC中找到分段的唯一标签