cvDilate/cvErode:如何避免分离对象之间的连接?

发布于 2024-10-07 11:33:58 字数 147 浏览 0 评论 0原文

我想在 OpenCv 中分离对象,如下图所示: alt text 但如果我使用 cvDilate 或 cvErode,对象会一起生长......如何使用 OpenCv 做到这一点?

I would like to separate objects in OpenCv like the following image it shows: alt text
But if I am using cvDilate or cvErode the objects grow together... how to do that with OpenCv?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瀟灑尐姊 2024-10-14 11:33:59

据我所知,OpenCV 没有“XOR 扩张”(尽管如果有的话那就太好了)。
为了获得类似的结果,您可以尝试腐蚀(如“d”中),并使用腐蚀的中心作为 Voronoi 分割的种子,然后您可以将其与原始图像进行 AND 运算。

As far as I know OpenCV does not have "dilation with XOR" (although that would be very nice to have).
To get similar results you might try eroding (as in 'd'), and using the eroded centers as seeds for a Voronoi segmentation which you could then AND with the original image.

又怨 2024-10-14 11:33:59

腐蚀和膨胀后尝试对图像进行阈值处理以消除弱元素。只应保留强区域,从而改善对象分离。顺便说一下,您能否更清楚地了解 cvDilate 或 cvErode 的问题。

after erosion and dilate try thresholding the image to eliminate weak elements. Only strong regions should remain and thus improve the object separation. By the way could you be a little more clear about your problem with cvDilate or cvErode.

星星的轨迹 2024-10-14 11:33:58

看起来您需要编写自己的扩张函数,然后自己添加异或功能。

根据 opencv 文档,这是 cvdilate 使用的规则:

dst=dilate(src,element): dst(x,y)=max((x',y') in element))src(x+x',y +y')

这是起点的伪代码(这不包括异或代码):

void my_dilate(img) {

  for(i = 0; i < img.height; i++) {
    for(j = 0; j < img.width; j++) {
       max_pixel = get_max_pixel_in_window(img, i, j);
       img.pixel(i,j) = max_pixel;
    }
  }  

}

int get_max_pixel_in_window(img, center_row, center_col) {
   int window_size = 3;
   int cur_max = 0;
   for(i = -window_size; i <= window_size; i++) {
     for(j = -window_size; j <= window_size; j++) {
        int cur_col = center_col + i; 
        int cur_row = center_row + j;
        if(out_of_bounds(img, cur_col, cur_row)) {
            continue;
        }
        int cur_pix = img.pixel(center_row + i, center_col + j);
        if(cur_pix > cur_max) {
           cur_max = cur_pix;
        }
     }
   }
   return cur_max;
}

// returns true if the x, y coordinate is outside of the image
int out_of_bounds(img, x, y) {
  if(x >= img.width || x < 0 || y >= img.height || y <= 0) {
    return 1;
  }
  return 0;
}

It looks like you will need to write your own dilate function and then add xor functionality yourself.

Per the opencv documentation, here is the rule that cvdilate uses:

dst=dilate(src,element): dst(x,y)=max((x',y') in element))src(x+x',y+y')

Here is pseudocode for a starting point (this does not include xor code):

void my_dilate(img) {

  for(i = 0; i < img.height; i++) {
    for(j = 0; j < img.width; j++) {
       max_pixel = get_max_pixel_in_window(img, i, j);
       img.pixel(i,j) = max_pixel;
    }
  }  

}

int get_max_pixel_in_window(img, center_row, center_col) {
   int window_size = 3;
   int cur_max = 0;
   for(i = -window_size; i <= window_size; i++) {
     for(j = -window_size; j <= window_size; j++) {
        int cur_col = center_col + i; 
        int cur_row = center_row + j;
        if(out_of_bounds(img, cur_col, cur_row)) {
            continue;
        }
        int cur_pix = img.pixel(center_row + i, center_col + j);
        if(cur_pix > cur_max) {
           cur_max = cur_pix;
        }
     }
   }
   return cur_max;
}

// returns true if the x, y coordinate is outside of the image
int out_of_bounds(img, x, y) {
  if(x >= img.width || x < 0 || y >= img.height || y <= 0) {
    return 1;
  }
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文