halcon图像处理——1D测量

整理一下最近做的关于使用halcon1D测量的工作。
目的:测量下图中黑线的距离(自己随意画的)
原图
因为接触halcon不久,大多数算子都不知道有什么用,通过看例程以及网上查找资料,主要用了以下几个算子完成:
一、measure_pairs

Row := 400
Column := 600
Length1 := 400
Length2 := 100
Angle := 0
gen_rectangle2 (ROI, Row, Column, Angle, Length1, Length2)

gen_measure_rectangle2 (Row, Column, Angle, Length1, Length2, Width, Height, 'bilinear', MeasureHandle)
dev_display (ROI)

measure_pairs (Image, MeasureHandle, 1, 20, 'negative', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)

close_measure (MeasureHandle)

以上是例程fuze里面的几个主要算子,我用这个方法可以成功的得到结果,程序很简单,几乎是复制例程就好,只需要注意measure_pairs 的Threshold参数符合自己的图片就行。
在此基础上,而后又增加了模板,不管图片是旋转了还是平移都可以测量,只是因为不同的图片Threshold阈值不同,所以我用了mean_image中值滤波,程序如下:

*Creat model
read_image (Image, 'C:\Users\Desktop\1.bmp')
get_image_size(Image, Width, Height)
mean_image(Image, ImageMean, 9, 9)
gen_rectangle1(Rectangle, 20, 300,800, 1000)
reduce_domain(ImageMean, Rectangle, ImageReduced)
create_shape_model (ImageReduced, 'auto', rad(-180), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
gen_rectangle2(Rectangle1, 480, 600,0, 300, 100)
*Read another image
read_image (Image1, 'C:\Users\Desktop\5.bmp')
mean_image(Image1, ImageMean2, 9, 9)
find_shape_model(ImageMean2, ModelID,  rad(-180), rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row2, Column2, Angle2, Score)
if(|Score|>0)
   dev_display_shape_matching_results(ModelID, 'red', Row2, Column2, Angle2, 1, 1, 0)
   area_center(Rectangle, Area, Row00, Column00)
   vector_angle_to_rigid(Row2, Column2, Angle2,Row00, Column00, 0,  HomMat2D)
   affine_trans_image(ImageMean2, ImageAffinTrans, HomMat2D, 'constant', 'false')
   dev_clear_window()
   dev_display(ImageAffinTrans)
   dev_display(Rectangle1)

   gen_measure_rectangle2(480,600,0,300,100,Width, Height, 'nearest_neighbor', MeasureHandle2)
   measure_pairs(ImageAffinTrans, MeasureHandle2, 1, 10, 'all', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
endif

这种方法几乎可以达到测量目的了,只是跟我的工作要求不符,而且测量精度不知道能不能保证,于是当做了一个备用方案。

二、distance_ss
程序如下:

read_image (Image, 'C:\Users\Desktop\1.bmp')
mean_image(Image, ImageMean, 3, 3)

threshold_sub_pix(ImageMean, Border, 128)
segment_contours_xld(Border, ContoursSplit, 'lines', 500, 4, 2)
select_contours_xld(ContoursSplit, SelectedContours, 'contour_length', 150, 1200, -0.5, 0.5)
union_adjacent_contours_xld(SelectedContours, UnionContours, 10, 1, 'attr_keep')
select_obj(UnionContours,ObjectSelected1,1)
select_obj(UnionContours,ObjectSelected2,2)
fit_line_contour_xld(ObjectSelected1, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr1, Nc1, Dist1)
fit_line_contour_xld(ObjectSelected2, 'tukey', -1, 0, 5, 2, RowBegin2, ColBegin2, RowEnd2, ColEnd2, Nr2, Nc2, Dist2)

distance_ss(RowBegin1, ColBegin1, RowEnd1, ColEnd1,RowBegin2, ColBegin2, RowEnd2, ColEnd2,DistanceMin, DistanceMax)

这个方法依然有问题存在,而且问题更,严重,当图片形状发生明显变化的时候,得到的UnionContours不止有两个,这种情况下测量精度就只能看天意了。
不过如果图片形状不会发生非常明显的变化的话,测量的重复性是非常高的,几乎没有变化。
除了这两个算子,我还试过其他算子,比如根据测量助手得到的measure_pos,具体问题见我的另一个帖子:http://www.ihalcon.com/read-6630.html

初步达到测量目的后,剩下的目标是提高测量的精度和重复性,不排除继续寻找其他算子,但是在distance_ss的基础上的话,是要深入研究segment_contours_xld算子,我认为,这种方法之所以存在问题,主要就是分割边界造成的。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值