动态阈值分割 dyn_threshold

手册里面的particle例子,例子的任务是分析颗粒在液体中。在这个应用程序的主要困难:存在两种类型的对象:大明亮物体和较低的小物体的对比。此外噪音使分割的存在困难;无法使用全局灰度阈值threshold进行分割;所以采用先将大块不需要检测的部分去除掉,再通过灰度动态阈值dyn_threshold分割图像得到想要的内容。

处理图片和结果图片:


以下是代码:

[plain]  view plain  copy
  1. read_image (Image, 'particle')  
  2. *获取图像  
  3.   
  4. dev_display (Image)  
  5. *显示图像  
  6.   
  7. threshold (Image, Large, 110, 255)  
  8. *灰度阈值分割图像  
  9.   
  10. dilation_circle (Large, LargeDilation, 7.5)  
  11. *圆角膨胀  
  12.   
  13. dev_display (Image)  
  14. dev_set_draw ('margin')  
  15. dev_set_line_width (3)  
  16. dev_set_color ('red')  
  17. dev_display (LargeDilation)  
  18. dev_set_draw ('fill')  
  19. *显示图像  
  20.   
  21. complement (LargeDilation, NotLarge)  
  22. *返回补充图像,即获得去除大斑点后的图像NotLarge  
  23.   
  24. reduce_domain (Image, NotLarge, ParticlesRed)  
  25. *减去除了NotLarge图像,即去除大斑点后的图像,减少运算  
  26.   
  27. mean_image (ParticlesRed, Mean, 31, 31)  
  28. *平滑处理图像  
  29.   
  30. dyn_threshold (ParticlesRed, Mean, SmallRaw, 3, 'light')  
  31. *选择灰度阈值;网友详解:当前背景之间差异明显时,可以设定全局阈值进行threshold,但很多情况下由于背景不均一,  
  32. *目标体经常表现为比背景局部亮一些或暗一些,无法确定全局阈值操作,需要通过其邻域找到一个合适的阈值进  
  33. *行分割。ThresholdImage是参考图像,通过与OrigImage对比找到领域确定阈值,一般采用平滑滤波算子  
  34. *(如mean_image)获取参考图像。offset设定邻域比较的区间范围,灰度值变化在offset范围内均是可以接受的。  
  35.   
  36. opening_circle (SmallRaw, Small, 2.5)  
  37. *消除小区域(小于圆形结构元素)和光滑的边界地区  
  38.   
  39. connection (Small, SmallConnection)  
  40. *显示联通区域  
  41.   
  42. dev_display (Image)  
  43. *这句不加窗口显示效果会有雪花  
  44.   
  45. dev_set_colored (12)  
  46. dev_display (SmallConnection)  
  47. *显示结果图像  

下图是只用threshold时候的实验效果,无法分割出小斑点: