图像的腐蚀与膨胀

来源:互联网 发布:容我喝一杯82年的java 编辑:程序博客网 时间:2024/05/18 12:02
腐蚀:对一幅图像取位移再求交得到,用于从一幅图像中去除小而无用的目标
膨胀:对一幅图像取位移再求并得到,连接图像中的断续点和填补图像中的空洞。
腐蚀与膨胀不是逆运算,因为由腐蚀得到的图像不是都能通过膨胀复原
开启:先腐蚀在膨胀  可看作是腐蚀图像的膨胀恢复
闭合:先膨胀在腐蚀  可看作是膨胀图像的腐蚀恢复
  1. //腐蚀OR膨胀数组
  2. const KERNEL kernel_erosion[]=
  3. {
  4. //垂直方向
  5. {0,1,0,
  6. 0,1,0,
  7. 0,1,0,
  8. 1,3
  9. },
  10. //水平方向
  11. {0,0,0,
  12. 1,1,1,
  13. 0,0,0,
  14. 1,3
  15. },
  16. //十方向
  17. {0,1,0,
  18. 1,1,1,
  19. 0,1,0,
  20. 1,3
  21. },
  22. //8方向
  23. {1,1,1,
  24. 1,1,1,
  25. 1,1,1,
  26. 1,3
  27. }
  28. };

腐蚀代码:
  1. /*************************************************************************
  2. * 函数名称:Erosion()
  3. * 参数:BYTE* bmp,LONG width,LONG height ------图像参数
  4. KERNEL array 腐蚀方向数组
  5. * 返回值:无
  6. * 说明:对图像进行不同方向的腐蚀处理
  7. ************************************************************************/
  8. voidErosion(BYTE *bmp,LONG width,LONG height,KERNEL array);
  1. voidMyProcess::Erosion(BYTE *bmp,LONG width,LONG height,KERNEL kernel)
  2. {
  3. LONG i,j,k,m;//循环变量
  4. LONG door=128;//像素阈值
  5. BYTE *temp_bmp =new BYTE[(width+6)*(height+2)];
  6. BmpFilter(temp_bmp,bmp,width,height);
  7. //腐蚀处理,取各方向最大值
  8. for(i=1;i<height+1;i++)
  9. for(j=3;j<width+3;j++)
  10. {
  11. for(k=-1;k<kernel.Dimention-1;k++)
  12. for(m=-1;m<kernel.Dimention-1;m++)
  13. {
  14. int a = temp_bmp[(i+k)*(width+6)+j+m*3];
  15. int b = kernel.Element[k+1][m+1];
  16. if(a*b>door) bmp[(i-1)*width+j-3]=255;
  17. }
  18. }
  19. delete[] temp_bmp;
  20. }
膨胀代码:
  1. /*************************************************************************
  2. * 函数名称:Dilation()
  3. * 参数:BYTE* bmp,LONG width,LONG height ------图像参数
  4. KERNEL array 膨胀方向数组
  5. * 返回值:无
  6. * 说明:对图像进行不同方向的膨胀处理
  7. ************************************************************************/
  8. voidDilation(BYTE *bmp,LONG width,LONG height,KERNEL array);
  1. voidMyProcess::Dilation(BYTE *bmp,LONG width,LONG height,KERNEL kernel)
  2. {
  3. LONG i,j,k,m;//循环变量
  4. LONG door=128;//像素阈值
  5. BYTE *temp_bmp =new BYTE[(width+6)*(height+2)];
  6. BmpFilter(temp_bmp,bmp,width,height);
  7. //膨胀处理,取各方向最小值
  8. for(i=1;i<height+1;i++)
  9. for(j=3;j<width+3;j++)
  10. {
  11. for(k=-1;k<kernel.Dimention-1;k++)
  12. for(m=-1;m<kernel.Dimention-1;m++)
  13. {
  14. int a = temp_bmp[(i+k)*(width+6)+j+m*3];
  15. int b = kernel.Element[k+1][m+1];
  16. if(a*b<=door) bmp[(i-1)*width+j-3]=0;
  17. }
  18. }
  19. delete[] temp_bmp;
  20. }

0 0
原创粉丝点击