【issue-halcon例程学习】forest.hdev

例程功能

 从红外图像中提取森林特征。

代码如下

dev_close_window ()
dev_update_window ('off')
read_image (Forest, 'forest_air1')
get_image_size (Forest, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
decompose3 (Forest, Red, Green, Blue)
dev_display (Red)
threshold (Blue, BlueBright, 80, 255)
connection (BlueBright, BlueBrightConnection)
select_shape (BlueBrightConnection, Path, 'area', 'and', 100, 100000000)
dev_set_color ('red')
dev_set_draw ('margin')
dev_display (Path)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
threshold (Red, RedBright, 120, 255)
connection (RedBright, RedBrightConnection)
select_shape (RedBrightConnection, RedBrightBig, 'area', 'and', 1500, 10000000)
closing_circle (RedBrightBig, RedBrightClosing, 7.5)
opening_circle (RedBrightClosing, RedBrightOpening, 9.5)
connection (RedBrightOpening, RedBrightOpeningConnection)
select_shape (RedBrightOpeningConnection, BeechBig, 'area', 'and', 1000, 100000000)
select_gray (BeechBig, Blue, Beech, 'mean', 'and', 0, 59)
dev_display (Red)
dev_display (Beech)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
union1 (Beech, BeechUnion)
complement (BeechUnion, NotBeech)
difference (NotBeech, Path, NotBeechNotPath)
reduce_domain (Red, NotBeechNotPath, NotBeechNotPathRed)
threshold (NotBeechNotPathRed, BrightRest, 150, 255)
connection (BrightRest, BrightRestConnection)
select_shape (BrightRestConnection, Meadow, 'area', 'and', 500, 1000000)
dev_display (Red)
dev_display (Meadow)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
union2 (Path, RedBrightClosing, BeechPath)
smooth_image (Red, RedGauss, 'gauss', 4.0)
invert_image (RedGauss, Invert)
watersheds (Invert, SpruceRed, Watersheds)
select_shape (SpruceRed, SpruceRedLarge, 'area', 'and', 100, 5000)
select_gray (SpruceRedLarge, Red, SpruceRedInitial, 'max', 'and', 100, 200)
gen_empty_obj (LocalThresh)
count_obj (SpruceRedInitial, NumSpruce)
dev_update_var ('off')
dev_update_pc ('off')
for i := 1 to NumSpruce by 1
    select_obj (SpruceRedInitial, SingleSpruce, i)
    min_max_gray (SingleSpruce, Red, 50, Min, Max, Range)
    reduce_domain (Red, SingleSpruce, SingleSpruceRed)
    threshold (SingleSpruceRed, SingleSpruceBright, Min, 255)
    connection (SingleSpruceBright, SingleSpruceBrightCon)
    select_shape_std (SingleSpruceBrightCon, MaxAreaSpruce, 'max_area', 70)
    concat_obj (MaxAreaSpruce, LocalThresh, LocalThresh)
endfor
opening_circle (LocalThresh, FinalSpruce, 1.5)
dev_set_line_width (2)
dev_set_color ('red')
dev_display (Red)
dev_display (FinalSpruce)
dev_set_color ('green')
dev_display (Beech)
dev_set_color ('yellow')
dev_display (Meadow)

要点

  1. decompose3-在红外图像中,多通道图像可以进行拆分独立分析
     例程中road即在blue通道图中通过area筛选而来;
	read_image (Forest, 'forest_air1')
	decompose3 (Forest, Red, Green, Blue)
	threshold (Blue, BlueBright, 80, 255)
	connection (BlueBright, BlueBrightConnection)
	select_shape (BlueBrightConnection, Path, 'area', 'and', 100, 100000000)

 例程中Beech trees即在red通道图中通过灰度值和区域大小筛选而来;注意前处理操作: 打散->第一轮筛选->闭运算->开运算->打散,可以借鉴;

	threshold (Red, RedBright, 120, 255)
	connection (RedBright, RedBrightConnection)
	select_shape (RedBrightConnection, RedBrightBig, 'area', 'and', 1500, 10000000)
	closing_circle (RedBrightBig, RedBrightClosing, 7.5)
	opening_circle (RedBrightClosing, RedBrightOpening, 9.5)
	connection (RedBrightOpening, RedBrightOpeningConnection)
	select_shape (RedBrightOpeningConnection, BeechBig, 'area', 'and', 1000, 100000000)
	select_gray (BeechBig, Blue, Beech, 'mean', 'and', 0, 59)

 例程中Meadows 对象有相似的光学特性,可以通过亮度进行阈值分割。在roadBeech trees被分割出来的前提下,可以通过取反complement和取不同difference缩小待分割区域;

	union1 (Beech, BeechUnion)
	complement (BeechUnion, NotBeech)
	difference (NotBeech, Path, NotBeechNotPath)
	reduce_domain (Red, NotBeechNotPath, NotBeechNotPathRed)
	threshold (NotBeechNotPathRed, BrightRest, 150, 255)
	connection (BrightRest, BrightRestConnection)
	select_shape (BrightRestConnection, Meadow, 'area', 'and', 500, 1000000)
  1. 创建空对象,在循环中筛选出结果存放在对象中;
	gen_empty_obj (LocalThresh)
	for condition
		# do sth.
		concat_obj	(rest, LocalThresh, LocalThresh)
	endfor

你可能感兴趣的:(学习,视觉检测)