Halcon1D灰度测量

(1)1D灰度测量案例check_smd_tilt

* This program checks if SMDs are nearly horizontal or if they
* are tilted with respect to the camera.
* The images were taken with a small depth of field, therefore
* tilted SMDs are partly blurred.
* First, the SMDs are segmented from the image. Then, the
* amplitudes of the edges (first derivatives of the gray values)
* are computed. The edge amplitude is weak in blurred regions.
* Therefore, tilted SMDs can be detected by comparing the edge
* amplitudes in the left and right parts of the SMDs.
* 
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 640 * .7, 512 * .7, 'black', WindowHandle)
dev_set_draw ('margin')
dev_set_line_width (3)
dev_open_window (0, 640 * .7 + 12, 640 * .7, 512 * .7, 'black', GrayProfileWindow)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
set_display_font (GrayProfileWindow, 16, 'mono', 'true', 'false')
* 
NumImages := 10
for Index := 1 to NumImages by 1read_image (Image, 'smd/smd_tilted_' + Index$'02')* * Segment the SMDvar_threshold (Image, Region, 20, 20, 0.1, 2, 'dark')connection (Region, ConnectedRegions)select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 15000, 99999999)shape_trans (SelectedRegions, RegionTrans, 'convex')reduce_domain (Image, RegionTrans, ImageReduced)* * Check if the SMD is tilted*找出边缘,测量边缘图像的灰度sobel_amp (ImageReduced, EdgeAmplitude, 'sum_abs', 3)*根据条件找出最小内接矩形smallest_rectangle2 (RegionTrans, Row, Column, Phi, Length1, Length2)gen_rectangle2 (Rectangle, Row, Column, Phi, Length1 - 3, 20)gen_measure_rectangle2 (Row, Column, Phi, Length1 - 3, 20, 640, 512, 'nearest_neighbor', MeasureHandle)*测量从左到右每个像素的灰度值measure_projection (EdgeAmplitude, MeasureHandle, GrayValues)*计算有多少个像素Sequence := [1:|GrayValues|]*创建XY函数关系,类似画散点图create_funct_1d_pairs (Sequence, GrayValues, Function)*create_funct_1d_array (GrayValues, Function1)*smooth_funct_1d_gauss (Function1, 0.3, SmoothedFunction)*derivate_funct_1d (SmoothedFunction, 'first', Derivative)*derivate_funct_1d (SmoothedFunction, 'second', Derivative2)*create_funct_1d_array (Derivative, Function2)*create_funct_1d_array (Derivative2, Function3)*zero_crossings_funct_1d (Function2, ZeroCrossings)*create_funct_1d_array (ZeroCrossings, Function4)*plot_funct_1d (GrayProfileWindow, Function4, [], [], 'blue', 'axes_color', 'none')*画箭头,可用数组同时画多个gen_arrow_contour_xld (Arrow, [Row,Row], [Column,Column], [Row - Length1 * sin(Phi),Row + Length1 * sin(Phi)], [Column + Length1 * cos(Phi),Column - Length1 * cos(Phi)], 25, 25)* * Evaluate the edge amplitude of the SMD profileLength := |GrayValues|PartLeft := GrayValues[0:Length / 2]PartRight := GrayValues[Length / 2:Length - 1]RangeLeft := max(PartLeft) - min(PartLeft)RangeRight := max(PartRight) - min(PartRight)TiltRatio := RangeLeft / RangeRightif (TiltRatio > 3 or TiltRatio < 1.0 / 3)Color := 'red'String := 'Not OK:\nSMD is tilted'elseColor := 'green'String := 'OK:\nSMD is aligned'endif* * Display the resultsdev_set_window (WindowHandle)dev_display (Image)dev_set_color (Color)dev_display (Arrow)dev_display (RegionTrans)disp_message (WindowHandle, String, 'window', 12, 12, 'black', 'true')dev_set_window (GrayProfileWindow)dev_clear_window ()*将函数绘制成图表plot_funct_1d (GrayProfileWindow, Function, [], [], Color, 'axes_color', 'none')disp_message (GrayProfileWindow, ['Edge amplitude','Tilt ratio = ' + TiltRatio$'3.2f'], 'window', 12, 12, 'white', 'false')if (Index != NumImages)disp_continue_message (WindowHandle, 'black', 'true')stop ()endif
endfor

(2)1D灰度测量案例count_fish_sticks

* This program performs a completeness check by counting the
* fish sticks contained in each package.
* Here, one-dimensional measuring tools are used to determine
* the boundary between adjacent fish sticks.
* 
dev_update_off ()
dev_close_window ()
read_image (Image, 'food/fish_stick_package_01')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width * .9, Height * .9, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_open_window (0, Width * .9 + 12, 420, 150, 'black', GraphicsWindow)
set_display_font (GraphicsWindow, 16, 'mono', 'true', 'false')
dev_set_color ('yellow')
* 
* Read and process the images
NumImages := 6
for Index := 1 to NumImages by 1read_image (Image, 'food/fish_stick_package_' + Index$'02')* * Segment the content of the fish stick packagethreshold (Image, Region, 100, 255)closing_circle (Region, RegionClosing, 5)fill_up (RegionClosing, RegionFillUp)difference (RegionFillUp, RegionClosing, RegionDifference)fill_up (RegionDifference, RegionFillUp1)closing_circle (RegionFillUp1, RegionClosing1, 10)*求最小内接矩形smallest_rectangle2 (RegionClosing1, Row, Column, Phi, Length1, Length2)MeasureLength1 := Length1 - 5* * Measure the gray value profile of the fish stick packagegen_rectangle2 (Rectangle, Row, Column, Phi, Length1, 80)gen_measure_rectangle2 (Row, Column, Phi, MeasureLength1, 60, Width, Height, 'nearest_neighbor', MeasureHandle)measure_projection (Image, MeasureHandle, GrayValues)*生产函数create_funct_1d_array (GrayValues, Function)*获取函数中的所有极值点local_min_max_funct_1d (Function, 'plateaus_center', 'true', Minima, Maxima)* * Evaluate the gray profile*根据函数的X值获得Y值get_y_value_funct_1d (Function, Minima, 'mirror', YValues)SelectedMinima := subset(Minima,find(sgn(YValues - 50),-1))Num := |SelectedMinima| + 1StickEdges := [-5,SelectedMinima,2 * MeasureLength1 + 5]StickWidth := StickEdges[1:Num] - StickEdges[0:Num - 1]* * Display the resultsBorderX := Column + cos(Phi) * (MeasureLength1 * ((StickEdges - MeasureLength1) / MeasureLength1))BorderY := Row - sin(Phi) * (MeasureLength1 * ((StickEdges - MeasureLength1) / MeasureLength1))CenterX := (BorderX[1:Num] + BorderX[0:Num - 1]) / 2CenterY := (BorderY[1:Num] + BorderY[0:Num - 1]) / 2*绘制crossgen_cross_contour_xld (BorderCross, BorderY, BorderX, 15, rad(45))gen_cross_contour_xld (CenterCross, CenterY, CenterX, 15, rad(45))IndexFish := find(sgn(StickWidth - 20),1)if (IndexFish != -1)NumFish := |IndexFish|gen_rectangle2 (FishRegions, subset(CenterY,IndexFish), subset(CenterX,IndexFish), gen_tuple_const(NumFish,Phi), subset(StickWidth,IndexFish) / 2, gen_tuple_const(NumFish,Length2) / 2)elseNumFish := 0endifgen_empty_region (Flipped)IndexFlipped := find(sgn(StickWidth - 48),1)if (IndexFlipped != -1)NumFlipped := |IndexFlipped|gen_rectangle2 (Flipped, subset(CenterY,IndexFlipped), subset(CenterX,IndexFlipped), gen_tuple_const(NumFlipped,Phi), subset(StickWidth,IndexFlipped) / 2, gen_tuple_const(NumFlipped,Length2) / 2)elseNumFlipped := 0endif* dev_set_window (GraphicsWindow)dev_clear_window ()plot_funct_1d (GraphicsWindow, Function, [], [], 'yellow', 'axes_color', 'none')disp_message (GraphicsWindow, 'Gray Profile', 'window', 12, 12, 'white', 'false')dev_set_window (WindowHandle)dev_clear_window ()dev_display (Image)dev_set_color ('yellow')dev_set_line_width (1)dev_display (FishRegions)dev_display (BorderCross)dev_set_color ('red')dev_set_line_width (3)dev_display (Flipped)if (NumFish == 15 and NumFlipped == 0)String := 'OK'Color := 'green'elseString := 'Not OK'Color := 'red'endifString[1] := 'Number of fish sticks:' + NumFish$'3'if (NumFish != 15)Color := [Color,'red']elseColor := [Color,'white']endifif (NumFlipped != 0)String[2] := 'Flipped fishsticks:  ' + NumFlipped$'3'Color := [Color,'red']endifdisp_message (WindowHandle, String, 'window', 12, 12, Color, 'false')dev_set_color (Color[0])dev_display (Rectangle)if (Index < NumImages)disp_continue_message (WindowHandle, 'black', 'true')stop ()endif
endfor


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部