天天看點

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算子,我認為,這種方法之是以存在問題,主要就是分割邊界造成的。

繼續閱讀