基于halcon的简易测量实例

模拟电源插头目标尺寸测量

原图

在这里插入图片描述

代码

dev_close_window ()
read_image (Image, '电源头.bmp')
get_image_size (Image, Width, Height)
dev_open_window_fit_size (0, 0, Width, Height, -1, -1, WindowHandle)
rgb1_to_gray (Image, GrayImage)
dev_display (GrayImage)
threshold (GrayImage, Regions, 0, 127)
*开运算
opening_rectangle1 (Regions, RegionOpening, 10, 10)
*填充区域空白
fill_up (RegionOpening, RegionFillUp)
*获取区域旋转角度
orientation_region (RegionFillUp, Phi)
*获取区域中心位置
area_center (RegionFillUp, Area, Row, Column)
*旋转矩阵获取
vector_angle_to_rigid (Row, Column, Phi, Row, Column, 0, HomMat2D)
*仿射变换
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
affine_trans_region (RegionFillUp, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
*生成最小外接矩形,用于确定测量位置
smallest_rectangle1 (RegionAffineTrans, Row1, Column1, Row2, Column2)

dev_set_color ('green')
dev_set_draw ('margin')
dev_set_line_width(3)
set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
dev_display (ImageAffineTrans)

*使用测量助手工具生成代码,再手动改变测量位置参数
* Measure 01: Code generated by Measure 01
* Measure 01: Prepare measurement
AmplitudeThreshold := 40
RoiWidthLen2 := 5
set_system ('int_zooming', 'true')
* Measure 01: Coordinates for line Measure 01 [0]
LineRowStart_Measure_01_0 :=  Row1-20
LineColumnStart_Measure_01_0 := 0.5*(Column1+Column2)
LineRowEnd_Measure_01_0 := Row2
LineColumnEnd_Measure_01_0 := 0.5*(Column1+Column2)
* Measure 01: Convert coordinates to rectangle2 type
TmpCtrl_Row := 0.5*(LineRowStart_Measure_01_0+LineRowEnd_Measure_01_0)
TmpCtrl_Column := 0.5*(LineColumnStart_Measure_01_0+LineColumnEnd_Measure_01_0)
TmpCtrl_Dr := LineRowStart_Measure_01_0-LineRowEnd_Measure_01_0
TmpCtrl_Dc := LineColumnEnd_Measure_01_0-LineColumnStart_Measure_01_0
TmpCtrl_Phi := atan2(TmpCtrl_Dr, TmpCtrl_Dc)
TmpCtrl_Len1 := 0.5*sqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc)
TmpCtrl_Len2 := RoiWidthLen2
* Measure 01: Create measure for line Measure 01 [0]
* Measure 01: Attention: This assumes all images have the same size!
gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 640, 480, 'nearest_neighbor', MsrHandle_Measure_01_0)
* Measure 01: ***************************************************************
* Measure 01: * The code which follows is to be executed once / measurement *
* Measure 01: ***************************************************************
* Measure 01: *************
* Measure 01: * ATTENTION *
* Measure 01: *************
* Measure 01: The image from the graphics window is not available. At this point,
* Measure 01: it is necessary to ensure an image is stored in the variable 'Image'
* Measure 01: Execute measurements
measure_pos (ImageAffineTrans, MsrHandle_Measure_01_0, 1.3, AmplitudeThreshold, 'all', 'all', Row_Measure_01_0, Column_Measure_01_0, Amplitude_Measure_01_0, Distance_Measure_01_0)
* Measure 01: Do something with the results

*画测量位置线
disp_line (WindowHandle, Row1-20, 0.5*(Column1+Column2), Row2+20, 0.5*(Column1+Column2))

dev_set_color ('blue')
*测量结果
for Index := 1 to |Row_Measure_01_0|-1 by 1
    set_tposition (WindowHandle, (Row_Measure_01_0[Index]+Row_Measure_01_0[Index-1])/2 -6, Column_Measure_01_0[Index]+5)
    write_string (WindowHandle, Distance_Measure_01_0[Index-1])
endfor

*画测量夹线
disp_line (WindowHandle, Row_Measure_01_0, Column_Measure_01_0-5, Row_Measure_01_0, Column_Measure_01_0+5)

*保存当前窗口
dump_window_image (Image1, WindowHandle)
*显示测量结果提示语
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
set_tposition (WindowHandle, 10, 10)
write_string (WindowHandle, '测量结果如下:')
*仿射变换回测量原始图
vector_angle_to_rigid (Row, Column, 0, Row, Column, Phi, HomMat2D1)
affine_trans_image (Image1, ImageAffineTrans1, HomMat2D1, 'constant', 'false')
*保存测量结果
dump_window (WindowHandle, 'png', '电源头测量结果')

结果

在这里插入图片描述

  • 0
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值