` 本帖最后由 jinyi7016 于 2016-4-19 07:29 编辑
IMG的erode是形态学的腐蚀函数。它的输入数据也是二值化的数据,与前面的dilate 函数是一样的。
函数在工程中是调用了编译好的库,是无法查看源码的,这里就是erode的源码,从注释中不难看出它的原理,与dilate的函数是类似的。
下面是erode的源码实现。
- void IMG_erode_bin
- (
- const unsigned char *restrict in_data,
- unsigned char *restrict out_data,
- const char *restrict mask,
- int cols
- )
- {
- int i;
- unsigned __int40_t p0l, p3l, p6l;
- unsigned m0, m1, m2, m3, m4, m5, m6, m7, m8;
- unsigned p0, p1, p2, p3, p4, p5, p6, p7, p8;
- unsigned rA, rB, rC, result;
- /* -------------------------------------------------------------------- */
- /* "Don't care" values in mask become '1's for the ORing step. We */
- /* do this by converting negative values to "-1" (all 1s in binary) */
- /* and converting positive values to 0. */
- /* -------------------------------------------------------------------- */
- m0 = mask[0] < 0 ? ~0U : 0;
- m1 = mask[1] < 0 ? ~0U : 0;
- m2 = mask[2] < 0 ? ~0U : 0;
- m3 = mask[3] < 0 ? ~0U : 0;
- m4 = mask[4] < 0 ? ~0U : 0;
- m5 = mask[5] < 0 ? ~0U : 0;
- m6 = mask[6] < 0 ? ~0U : 0;
- m7 = mask[7] < 0 ? ~0U : 0;
- m8 = mask[8] < 0 ? ~0U : 0;
- _nassert((int) cols % 4 == 0);
- #pragma MUST_ITERATE(2,,2);
- #pragma UNROLL(2);
-
- /* -------------------------------------------------------------------- */
- /* Iterate over the input, processing 32 pixels per iteration. */
- /* -------------------------------------------------------------------- */
- for (i=0; i
- /* ---------------------------------------------------------------- */
- /* Load in our 34-bit by 3-bit context for applying the 3x3 mask. */
- /* ---------------------------------------------------------------- */
- p0l = _dtol(_memd8_const(&in_data[i ]));
- p3l = _dtol(_memd8_const(&in_data[i + cols ]));
- p6l = _dtol(_memd8_const(&in_data[i + cols*2]));
- /* ---------------------------------------------------------------- */
- /* Generate 3 offset copies of each row. */
- /* ---------------------------------------------------------------- */
- p0 = p0l; p1 = p0l >> 1; p2 = p0l >> 2;
- p3 = p3l; p4 = p3l >> 1; p5 = p3l >> 2;
- p6 = p6l; p7 = p6l >> 1; p8 = p6l >> 2;
- /* ---------------------------------------------------------------- */
- /* Now sum the filtered pixels together by ANDing. */
- /* ---------------------------------------------------------------- */
- rA = p0 | m0; rA &= p1 | m1; if (!m2) rA &= p2;
- rB = p3 | m3; if (!m4) rB &= p4; if (!m5) rB &= p5;
- rC = p6 | m6; if (!m7) rC &= p7; if (!m8) rC &= p8;
- result = rA & rB & rC;
- /* ---------------------------------------------------------------- */
- /* Write the result as four bytes. */
- /* ---------------------------------------------------------------- */
- _mem4(&out_data[i]) = result;
- }
- }
复制代码
Erode的算法,与dilate是一样的,参考也是一样的,不同的是算子不同。
函数的使用方法如下: IMG_erode_bin(Image,out,dmask3x3,lineByte*bmpHeight);
Image是bmp图片的数据区,等等其他参数也是与膨胀的那个函数的用法是一样的。 下面是这个函数的运行结果 。
最后和源码与二进制文件会在最后的结项中。
`
|