前言
本文以一个项目来讲解,检测电池的正负极
一、原图
二、实现思路
- 首先定义一个region,用来创建模板,为了寻找所有的圆
- 使用算子intensity检测灰度,因为负极基本全亮
三、halcon完整脚本
dev_get_window (WindowHandle)
read_image (Image, '1103_001.bmp')
draw_circle (WindowHandle, Row, Column, Radius)
gen_image_const (Image1, 'byte', Radius*2+10, Radius*2+10)
gen_circle (Circle, 5+Radius, 5+Radius, Radius)
overpaint_region (Image1, Circle, 255, 'fill')
create_scaled_shape_model (Image1, 'auto', 0, 0, 'auto', 0.8, 1.2, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
read_image (Image2, '1103_002.bmp')
find_scaled_shape_model (Image2, ModelID, 0, 0, 0.8, 1.2, 0.5, 0, 0.5, ['least_squares','max_deformation 1'], 0, 0.1, Row1, Column1, Angle1, Scale1, Score1)
R1 := []
C1 := []
S1 := []
D1 := []
R2 := []
C2 := []
S2 := []
D2 := []
tuple_length (Score1, Length)
for Index := 0 to Length-1 by 1
if (Score1[Index]>0.85)
gen_circle (Circle1, Row1[Index], Column1[Index], Radius*95/100)
intensity (Circle1, Image2, Mean, Deviation)
if (Mean>125)
gen_circle (Circle2, Row1[Index], Column1[Index], Radius*30/100)
difference (Circle1, Circle2, RegionDifference)
intensity (RegionDifference, Image2, Mean1, Deviation1)
if (Deviation1>35)
tuple_concat (R1, Row1[Index], R1)
tuple_concat (C1, Column1[Index], C1)
tuple_concat (S1, Score1[Index], S1)
tuple_concat (D1, Deviation1, D1)
else
tuple_concat (R2, Row1[Index], R2)
tuple_concat (C2, Column1[Index], C2)
tuple_concat (S2, Score1[Index], S2)
tuple_concat (D2, Deviation1, D2)
endif
endif
endif
endfor
dev_display (Image2)
dev_set_color ('red')
disp_cross (WindowHandle, R1, C1, 60, 0)
dev_set_color ('green')
disp_cross (WindowHandle, R2, C2, 60, 0)