资源:
链接:https://pan.baidu.com/s/1kmxdMk67E-7QCsG5mKnc7A
提取码:555s
图片
代码
* 1.读取并显示图片 *************************************
dev_close_window ()
read_image (Image, './img.png')
get_image_size (Image, Width, Height)
dev_open_window_fit_size (0, 0, Width, Height, -1, -1, WindowHandle)
dev_display (Image)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
* 2.预处理图片 *****************************************
rgb1_to_gray (Image, GrayImage)
gauss_filter (GrayImage, ImageGauss, 5)
* 3.连通域分析 *****************************************
mean_image (ImageGauss, ImageMean, 9, 9)
dyn_threshold (ImageGauss, ImageMean, RegionDynThresh, 5, 'light')
connection (RegionDynThresh, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 30, 99999)
* 4.形态学处理与分水岭处理 *****************************************
fill_up (SelectedRegions, RegionFillUp)
dilation_circle (RegionFillUp, RegionDilation, 1.5)
erosion_circle (RegionDilation, RegionErosion, 1.5)
get_image_size (ImageGauss, Width1, Height1)
* 区域距离换算
distance_transform (RegionErosion, DistanceImage, \
'city-block', 'true', Width1, Height1)
* 比例放大
scale_image_max (DistanceImage, ImageScaleMax)
* 图片反色
invert_image (ImageScaleMax, ImageInvert)
* 分水岭处理
watersheds_threshold (ImageInvert, Basins, 150)
* 5.二值化获取 米粒区域 ******************************************
dev_set_draw ('margin')
threshold (ImageInvert, Region, 0, 254)
connection (Region, ConnectedRegions1)
area_center (ConnectedRegions1, Area, Row, Column)
* 6.米粒区域与分水岭结果使用 ******************************************
union1 (ConnectedRegions1, RegionUnion)
sort_region (Basins, SortedRegions, 'upper_left', 'true', 'row')
count_obj (Basins, Number)
for Index := 1 to Number by 1
select_obj (SortedRegions, ObjectSelected, Index)
intersection (ObjectSelected, RegionUnion, RegionIntersection)
dev_clear_window ()
dev_display (Image)
dev_display (RegionIntersection)
* stop()
endfor
* 7.显示粘连分割结果 **********************************************
select_obj (SortedRegions, ObjectSelected1, 15)
intersection (ObjectSelected1, RegionUnion, RegionLeft)
select_obj (SortedRegions, ObjectSelected2, 16)
intersection (ObjectSelected2, RegionUnion, RegionRight)
dev_clear_window ()
dev_display (Image)
dev_set_color ('red')
dev_display (RegionLeft)
dev_set_color ('blue')
dev_display (RegionRight)