天天看点

halcon图像处理——1D测量

整理一下最近做的关于使用halcon1D测量的工作。

目的:测量下图中黑线的距离(自己随意画的)

halcon图像处理——1D测量

因为接触halcon不久,大多数算子都不知道有什么用,通过看例程以及网上查找资料,主要用了以下几个算子完成:

一、measure_pairs

Row := 
Column := 
Length1 := 
Length2 := 
Angle := 
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, , , '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, , )
gen_rectangle1(Rectangle, , ,, )
reduce_domain(ImageMean, Rectangle, ImageReduced)
create_shape_model (ImageReduced, 'auto', rad(-), rad(), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
gen_rectangle2(Rectangle1, , ,, , )
*Read another image
read_image (Image1, 'C:\Users\Desktop\5.bmp')
mean_image(Image1, ImageMean2, , )
find_shape_model(ImageMean2, ModelID,  rad(-), rad(), , , , 'least_squares', , , Row2, Column2, Angle2, Score)
if(|Score|>)
   dev_display_shape_matching_results(ModelID, 'red', Row2, Column2, Angle2, , , )
   area_center(Rectangle, Area, Row00, Column00)
   vector_angle_to_rigid(Row2, Column2, Angle2,Row00, Column00, ,  HomMat2D)
   affine_trans_image(ImageMean2, ImageAffinTrans, HomMat2D, 'constant', 'false')
   dev_clear_window()
   dev_display(ImageAffinTrans)
   dev_display(Rectangle1)

   gen_measure_rectangle2(,,,,,Width, Height, 'nearest_neighbor', MeasureHandle2)
   measure_pairs(ImageAffinTrans, MeasureHandle2, , , 'all', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
endif
           

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

二、distance_ss

程序如下:

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

threshold_sub_pix(ImageMean, Border, )
segment_contours_xld(Border, ContoursSplit, 'lines', , , )
select_contours_xld(ContoursSplit, SelectedContours, 'contour_length', , , -, )
union_adjacent_contours_xld(SelectedContours, UnionContours, , , 'attr_keep')
select_obj(UnionContours,ObjectSelected1,)
select_obj(UnionContours,ObjectSelected2,)
fit_line_contour_xld(ObjectSelected1, 'tukey', -, , , , RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr1, Nc1, Dist1)
fit_line_contour_xld(ObjectSelected2, 'tukey', -, , , , 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算子,我认为,这种方法之所以存在问题,主要就是分割边界造成的。

继续阅读