实施侵蚀,扩大C,C ++ [英] implementing erosion, dilation in C, C++

查看:182
本文介绍了实施侵蚀,扩大C,C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解了二进制图像扩张是如何做的。

I have theoretical understanding of how dilation in binary image is done.

AFAIK,如果我的SE(结构化元素)是这个

AFAIK, If my SE (structuring element) is this

0 1
1 1.


$ b b p。代表中心,我的形象(二进制是这样)

where . represents the centre, and my image(binary is this)

0 0 0 0 0
0 1 1 0 0
0 1 0 0 0
0 1 0 0 0
0 0 0 0 0

所以膨胀的结果是

0 1 1 0 0 
1 1 1 0 0
1 1 0 0 0
1 1 0 0 0
0 0 0 0 0


b $ b

根据SE,并取所有这三个移位的并集,将图像移动到0,+1(向上)和-1(向左)方向,我得到了上述结果。

I got above result by shifting Image in 0, +1 (up) and and -1(left) direction, according to SE, and taking the union of all these three shifts.

现在,我需要弄清楚如何在C,C ++中实现它。
我不知道如何开始和如何采取集合的联合。
我想到表示原始图像,三个移位图像和通过并集获得的最终图像;所有使用矩阵。

Now, I need to figure out how to implement this in C, C++. I am not sure how to begin and how to take the union of sets. I thought of representing original image,three shifted images and final image obtained by taking union; all using matrix.

有没有地方我可以得到一些开始的示例解决方案或任何想法继续?

Is there any place where I can get some sample solution to start with or any ideas to proceed ?

谢谢。

推荐答案

有很多示例实现。Google 是您的朋友:)


EDIT

以下是过程的伪代码(非常类似于在2D中进行卷积)。确定有更聪明的方式来做:

There are tons of sample implementations out there.. Google is your friend :)

EDIT
The following is a pseudo-code of the process (very similar to doing a convolution in 2D). Im sure there are more clever way to doing it:

// grayscale image, binary mask
void morph(inImage, outImage, kernel, type) {
 // half size of the kernel, kernel size is n*n (easier if n is odd)
 sz = (kernel.n - 1 ) / 2;

 for X in inImage.rows {
  for Y in inImage.cols {

   if ( isOnBoundary(X,Y, inImage, sz) ) {
    // check if pixel (X,Y) for boundary cases and deal with it (copy pixel as is)
    // must consider half size of the kernel
    val = inImage(X,Y);       // quick fix
   }

   else {
    list = [];

    // get the neighborhood of this pixel (X,Y)
    for I in kernel.n {
     for J in kernel.n {
      if ( kernel(I,J) == 1 ) {
       list.add( inImage(X+I-sz, Y+J-sz) );
      }
     }
    }

    if type == dilation {
     // dilation: set to one if any 1 is present, zero otherwise
     val = max(list);
    } else if type == erosion {
     // erosion: set to zero if any 0 is present, one otherwise
     val = min(list);
    }
   }

   // set output image pixel
   outImage(X,Y) = val;
  }
 }
}

上述代码基于此教程(请查看网页末尾的源代码)。

The above code is based on this tutorial (check the source code at the end of the page).


EDIT2


list.add inImage(X + I-sz,Y + J-sz));

list.add( inImage(X+I-sz, Y+J-sz) );

这个想法是我们要叠加内核掩码(大小为nxn),以位于(X,Y)的当前图像像素上的sz(掩码的一半大小)为中心,然后获得掩码值为1的像素的强度(我们将它们添加到列表)。一旦提取了该像素的所有邻居,我们将输出图像像素设置为该列表的最大值(最大强度)用于扩展,最小值用于侵蚀(当然,这只适用于灰度图像和二进制掩码)

假设上面的语句中的X / Y和I / J的索引从0开始。
如果你喜欢,你总是可以用I / J的索引面板(从-sz到+ sz)有一个小的改变(我链接到的教程正在使用)...

The idea is that we want to superimpose the kernel mask (of size nxn) centered at sz (half size of mask) on the current image pixel located at (X,Y), and then just get the intensities of the pixels where the mask value is one (we are adding them to a list). Once extracted all the neighbors for that pixel, we set the output image pixel to the maximum of that list (max intensity) for dilation, and min for erosion (of course this only work for grayscale images and binary mask)
The indices of both X/Y and I/J in the statement above are assumed to start from 0. If you prefer, you can always rewrite the indices of I/J in terms of half the size of the mask (from -sz to +sz) with a small change (the way the tutorial I linked to is using)...


示例

考虑放置这个3x3内核掩码并以像素(X,Y)为中心,看看我们如何遍历它周围的邻域:


Example:
Consider this 3x3 kernel mask placed and centered on pixel (X,Y), and see how we traverse the neighborhood around it:

 --------------------
|      |       |     |    sz = 1;
 --------------------     for (I=0 ; I<3 ; ++I)
|      | (X,Y) |     |      for (J=0 ; J<3 ; ++J)
 --------------------         vect.push_back( inImage.getPixel(X+I-sz, Y+J-sz) );
|      |       |     |
 --------------------

这篇关于实施侵蚀,扩大C,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆