HALCON示例程序count_fish_sticks.hdev鱼棒完整性检测
示例程序源码(加注释)
- 关于显示类函数解释
dev_update_off ()
dev_close_window ()
read_image (Image, ‘food/fish_stick_package_01’)
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width * .9, Height * .9, ‘black’, WindowHandle)
set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)
dev_set_draw (‘margin’)
dev_open_window (0, Width * .9 + 12, 420, 150, ‘black’, GraphicsWindow)
set_display_font (GraphicsWindow, 16, ‘mono’, ‘true’, ‘false’)
dev_set_color (‘yellow’) - 读入图片
NumImages := 6
for Index := 1 to NumImages by 1
read_image (Image, ‘food/fish_stick_package_’ + Index$‘02’)- 二值化
threshold (Image, Region, 100, 255) - 使用圆形元素进行闭运算
closing_circle (Region, RegionClosing, 5) - 填充孔洞
fill_up (RegionClosing, RegionFillUp) - 求取区域不想交部分
difference (RegionFillUp, RegionClosing, RegionDifference) - 填充孔洞
fill_up (RegionDifference, RegionFillUp1) - 使用圆形元素进行闭运算
closing_circle (RegionFillUp1, RegionClosing1, 10) - 求取区域的可旋转的最小外接矩形
smallest_rectangle2 (RegionClosing1, Row, Column, Phi, Length1, Length2)
MeasureLength1 := Length1 - 5 - 生成一个可旋转矩形框
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, 80) - gen_measure_rectangle2提取垂直于矩形的直边。
- gen_measure_rectangle2(::行坐标,列坐标,角度,矩形长度/2,矩形宽度/2,测量宽度,测量高度,算法:测量句柄)
gen_measure_rectangle2 (Row, Column, Phi, MeasureLength1, 60, Width, Height, ‘nearest_neighbor’, MeasureHandle) - measure_projection - 提取垂直于矩形或环形弧的一维灰度值轮廓。
- measure_projection(图片:: 测量句柄:灰度值)
measure_projection (Image, MeasureHandle, GrayValues) - 关闭测量句柄
close_measure (MeasureHandle) - create_funct_1d_array - 从一系列y值创建一个函数。
- create_funct_1d_array(::: Y值:函数)
create_funct_1d_array (GrayValues, Function) - local_min_max_funct_1d - 计算函数的局部最小点和最大点。
- local_min_max_funct_1d(::功能,模式,插值:最小,最大)
local_min_max_funct_1d (Function, ‘plateaus_center’, ‘true’, Minima, Maxima) - get_y_value_funct_1d - 返回指定X位置的函数Y的值。
- get_y_value_funct_1d(::函数,X位置,边界处理方法:Y的值)
get_y_value_funct_1d (Function, Minima, ‘mirror’, YValues) - 这句话的所有关于数组操作的算子解释贴子,这句话就是筛选局部最小值低于50的点。
SelectedMinima := subset(Minima,find(sgn(YValues - 50),-1))
Num := |SelectedMinima| + 1
StickEdges := [-5,SelectedMinima,2 * MeasureLength1 + 5]
StickWidth := StickEdges[1:Num] - StickEdges[0:Num - 1] - 计算每个鱼块的中心点与边界,并画X
BorderX := Column + cos(Phi) * (MeasureLength1 * ((StickEdges - MeasureLength1) / MeasureLength1))
BorderY := Row - sin(Phi) * (MeasureLength1 * ((StickEdges - MeasureLength1) / MeasureLength1))
CenterX := (BorderX[1:Num] + BorderX[0:Num - 1]) / 2
CenterY := (BorderY[1:Num] + BorderY[0:Num - 1]) / 2
gen_cross_contour_xld (BorderCross, BorderY, BorderX, 15, rad(45))
gen_cross_contour_xld (CenterCross, CenterY, CenterX, 15, rad(45)) - 筛选大于20小于48的鱼块
IndexFish := find(sgn(StickWidth - 20),1)
if (IndexFish != -1)
NumFish := |IndexFish|
gen_rectangle2 (FishRegions, subset(CenterY,IndexFish), subset(CenterX,IndexFish), gen_tuple_const(NumFish,Phi), subset(StickWidth,IndexFish) / 2, gen_tuple_const(NumFish,Length2) / 2)
else
NumFish := 0
endif
gen_empty_region (Flipped)
IndexFlipped := find(sgn(StickWidth - 48),1)
if (IndexFlipped != -1)
NumFlipped := |IndexFlipped|
gen_rectangle2 (Flipped, subset(CenterY,IndexFlipped), subset(CenterX,IndexFlipped), gen_tuple_const(NumFlipped,Phi), subset(StickWidth,IndexFlipped) / 2, gen_tuple_const(NumFlipped,Length2) / 2)
else
NumFlipped := 0
endif - 显示结果
dev_set_window (GraphicsWindow)
dev_clear_window () - plot_funct_1d - 此过程绘制表示坐标系中函数或曲线的元组。这个不是很常用。
plot_funct_1d (GraphicsWindow, Function, [], [], ‘yellow’, ‘axes_color’, ‘none’)
disp_message (GraphicsWindow, ‘Gray Profile’, ‘window’, 12, 12, ‘white’, ‘false’)
dev_set_window (WindowHandle)
dev_clear_window ()
dev_display (Image)
dev_set_color (‘yellow’)
dev_set_line_width (1)
dev_display (FishRegions)
dev_display (BorderCross)
dev_set_color (‘red’)
dev_set_line_width (3)
dev_display (Flipped)
if (NumFish == 15 and NumFlipped == 0)
String := ‘OK’
Color := ‘green’
else
String := ‘Not OK’
Color := ‘red’
endif
String[1] := ‘Number of fish sticks:’ + NumFish′3′if(NumFish!=15)Color:=[Color,′red′]elseColor:=[Color,′white′]endifif(NumFlipped!=0)String[2]:=′Flippedfishsticks:′+NumFlipped'3' if (NumFish != 15) Color := [Color,'red'] else Color := [Color,'white'] endif if (NumFlipped != 0) String[2] := 'Flipped fishsticks: ' + NumFlipped′3′if(NumFish!=15)Color:=[Color,′red′]elseColor:=[Color,′white′]endifif(NumFlipped!=0)String[2]:=′Flippedfishsticks:′+NumFlipped’3’
Color := [Color,‘red’]
endif
disp_message (WindowHandle, String, ‘window’, 12, 12, Color, ‘false’)
dev_set_color (Color[0])
dev_display (Rectangle)
if (Index < NumImages)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endif
endfor
- 二值化
处理思路
这个例子是主要讲解了二维测量的方法。使用的是gen_measure_rectangle2、measure_projection、create_funct_1d_array 、local_min_max_funct_1d、get_y_value_funct_1d完成的测量,当然后续会有更简单的直观的方法。
后记
大家有什么问题可以向我提问哈,我看到了第一时间回复,希望在学习的路上多多结交良师益友。