隶属于:
C:\Users\Public\Documents\MVTec\HALCON-23.05-Progress\examples\hdevelop\Applications\Completeness-Check
此例:分割不同背景中的绿色植物。其中算子segment_image_mser是新的,可以借鉴学习,以及不同的颜色空间域,可以尝试使用
算子segment_image_mser
segment_image_mser 是一种基于 Maximally Stable Extremal Regions(MSER,极大稳定极值区域)的图像分割方法。它通过分析图像的灰度值变化来分割出具有均匀灰度值的区域,特别适用于在不均匀背景或变化光照条件下稳健地分割目标物体。
主要参数
Polarity:决定提取的区域类型。
‘dark’:仅提取比周围区域更暗的 MSER。
‘light’:仅提取比周围区域更亮的 MSER。
‘both’(默认):提取两种类型的 MSER。
MinArea 和 MaxArea:限制返回的 MSER 的大小。过小的 MinArea 值(如小于 5)会显著增加运行时间,尤其是对于噪声图像。
Delta:影响算法的选择性。较大的 Delta 值会导致提取的 MSER 数量减少,而较小的 Delta 值会导致更多的 MSER。
GenParamName 和 GenParamValue:用于微调 MSER 的分割,常见的通用参数包括:
‘max_variation’:在 Delta 阈值范围内,组件面积的最大变化量。较大的值会导致更多的 MSER。
‘min_diversity’:两个重叠 MSER 的大小的最小相对差异。较小的值会导致更多的重叠 MSER。
‘may_touch_border’:控制是否返回接触输入域边界的区域。
‘min_gray’ 和 ‘max_gray’:通过应用阈值动态减少输入域,忽略指定灰度值范围之外的像素。
分割过程
首先,使用从 0 到最大灰度值的所有阈值对图像进行分割。
将阈值区域分割为连通组件(4-连通邻域),并监测每个组件的面积随阈值增加的变化。
MSER 是在 Delta 阈值范围内面积变化不显著的组件。要被接受为 MSER,组件的面积变化必须是局部最小值,并且小于 ‘max_variation’。
重叠 MSER 的多样性必须大于 ‘min_diversity’。
应用场景
目标检测:在复杂背景中提取目标物体。
特征提取:为后续的图像分析或机器学习任务提取稳定特征。
如果你需要进一步了解或实现该算法,可以参考相关图像处理库(如 HALCON)的文档。
1
这个例子展示了如何在不受控制的条件下分割图像中的植物,这些图像的背景各不相同。
*
dev_update_off ()
dev_close_window ()
read_image (Image, 'plants/plants_01')
dev_open_window_fit_image (Image, 0, 0, 400,300, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('green')
*
* Main loop
NumImages := 6
for I := 1 to NumImages by 1
* Read RGB image and transform to CIELab color space.
read_image (Image, 'plants/plants_' + I$'02')
decompose3 (Image, Red, Green, Blue)
trans_from_rgb (Red, Green, Blue, L, A, B, 'cielab')
* Segment stable regions in the a-channel
* (which corresponds to red-green variations within the image).
* 'may_touch_border' is set to 'true' to enable the extraction of plants
* at the image border.
* 在 a 通道分割稳定区域(a 通道对应图像中的红色-绿色变化)
* 将 'may_touch_border' 参数设置为 'true',以便提取位于图像边缘的植物
segment_image_mser (A, MSERDark, MSERLight, 'dark', 60, 600000, 1, 'may_touch_border', 1)
* Only use regions with at least a little fraction of green.
threshold (A, TmpRegion1, 20, 110)
threshold (Green, TmpRegion2, 30, 255)
intersection (TmpRegion1, TmpRegion2, RegionGreen)
intersection (MSERDark, RegionGreen, RegionIntersection)
* Filter out noise.
select_shape (RegionIntersection, SelectedRegions, ['area', 'compactness'], \
'or', [2500, 0], [2500000, 10])
* Eliminate overlaps.
union1 (SelectedRegions, RegionUnion)
connection (RegionUnion, RegionPlants)
* Display results.
dev_display (Image)
dev_display (RegionPlants)
dev_disp_text ('Segment plants ' + I + '/' + NumImages, 'window', 12, 12, 'black', [], [])
if (I < NumImages)
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
endif
endfor