gpt4 book ai didi

matlab - imregionalmax matlab 函数在 opencv 中的等价物

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:33 25 4
gpt4 key购买 nike

我有一个连接组件的图像(圆圈填充)。如果我想分割它们,我可以使用分水岭算法。我更喜欢为分水岭编写自己的函数而不是使用 OPENCV 中的内置函数。我成功了我怎么办使用 opencv 找到对象的区域最大值?

最佳答案

我自己写了一个函数。我的结果与 MATLAB 非常相似,尽管不准确。此函数是为 CV_32F 实现的,但可以轻松修改为其他类型。

  1. 我通过检查所有邻居来标记所有不属于最小区域的点。其余区域是最小值、最大值或拐点区域。
  2. 我使用连接组件来标记每个区域。
  3. 我检查每个区域是否有任何属于最大值的点,如果是,那么我将该标签放入一个向量中。
  4. 最后,我对错误标签进行排序,删除所有重复项,然后将输出中的所有点标记为非最小值。
  5. 剩下的就是最小值区域。

代码如下:

//  output is a binary image
// 1: not a min region
// 0: part of a min region
// 2: not sure if min or not
// 3: uninitialized
void imregionalmin(cv::Mat& img, cv::Mat& out_img)
{
// pad the border of img with 1 and copy to img_pad
cv::Mat img_pad;
cv::copyMakeBorder(img, img_pad, 1, 1, 1, 1, IPL_BORDER_CONSTANT, 1);

// initialize binary output to 2, unknown if min
out_img = cv::Mat::ones(img.rows, img.cols, CV_8U)+2;

// initialize pointers to matrices
float* in = (float *)(img_pad.data);
uchar* out = (uchar *)(out_img.data);

// size of matrix
int in_size = img_pad.cols*img_pad.rows;
int out_size = img.cols*img.rows;

int x, y;
for (int i = 0; i < out_size; i++) {
// find x, y indexes
y = i % img.cols;
x = i / img.cols;

neighborCheck(in, out, i, x, y, img_pad.cols); // all regions are either min or max
}

cv::Mat label;
cv::connectedComponents(out_img, label);

int* lab = (int *)(label.data);

in = (float *)(img.data);
in_size = img.cols*img.rows;

std::vector<int> bad_labels;

for (int i = 0; i < out_size; i++) {
// find x, y indexes
y = i % img.cols;
x = i / img.cols;

if (lab[i] != 0) {
if (neighborCleanup(in, out, i, x, y, img.rows, img.cols) == 1) {
bad_labels.push_back(lab[i]);
}
}
}

std::sort(bad_labels.begin(), bad_labels.end());
bad_labels.erase(std::unique(bad_labels.begin(), bad_labels.end()), bad_labels.end());

for (int i = 0; i < out_size; ++i) {
if (lab[i] != 0) {
if (std::find(bad_labels.begin(), bad_labels.end(), lab[i]) != bad_labels.end()) {
out[i] = 0;
}
}
}
}

int inline neighborCleanup(float* in, uchar* out, int i, int x, int y, int x_lim, int y_lim)
{
int index;
for (int xx = x - 1; xx < x + 2; ++xx) {
for (int yy = y - 1; yy < y + 2; ++yy) {
if (((xx == x) && (yy==y)) || xx < 0 || yy < 0 || xx >= x_lim || yy >= y_lim)
continue;
index = xx*y_lim + yy;
if ((in[i] == in[index]) && (out[index] == 0))
return 1;
}
}

return 0;
}

void inline neighborCheck(float* in, uchar* out, int i, int x, int y, int x_lim)
{
int indexes[8], cur_index;
indexes[0] = x*x_lim + y;
indexes[1] = x*x_lim + y+1;
indexes[2] = x*x_lim + y+2;
indexes[3] = (x+1)*x_lim + y+2;
indexes[4] = (x + 2)*x_lim + y+2;
indexes[5] = (x + 2)*x_lim + y + 1;
indexes[6] = (x + 2)*x_lim + y;
indexes[7] = (x + 1)*x_lim + y;
cur_index = (x + 1)*x_lim + y+1;

for (int t = 0; t < 8; t++) {
if (in[indexes[t]] < in[cur_index]) {
out[i] = 0;
break;
}
}

if (out[i] == 3)
out[i] = 1;
}

关于matlab - imregionalmax matlab 函数在 opencv 中的等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11205025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com