通过本例程巩固了对剖面灰度值的描述,并进行测量,算子使用同“halcon例程学习笔记(2)”。此例程进一步学习了halcon中的基本函数的使用方法:
subset 求取一个数组的子集
find 查找相应条件的值的位置索引
数组的错位减法的使用。
其它基本图像处理基本算子,可以查阅halcon使用手册,详细了解。
例程代码如下:
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 1 read_image (Image, 'food/fish_stick_package_'+Index$'02') * * Segment the content of the fish stick package threshold (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 package gen_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) close_measure (MeasureHandle) create_funct_1d_array (GrayValues, Function) *获得函数局域最大最小值点的位置 local_min_max_funct_1d (Function, 'plateaus_center', 'true', Minima, Maxima) * * Evaluate the gray profile get_y_value_funct_1d (Function, Minima, 'mirror', YValues) *查找在YValues值上下波动50的值并求取Minima中的子集 SelectedMinima := subset(Minima,find(sgn(YValues-50),-1)) Num := |SelectedMinima|+1 *产生一维数组, StickEdges := [-5, SelectedMinima, 2*MeasureLength1+5] *错位减法 StickWidth := StickEdges[1:Num]-StickEdges[0:Num-1] * * Display the results BorderX := [ Column+cos(Phi)*(MeasureLength1*((StickEdges-MeasureLength1)/MeasureLength1))] BorderY := [ Row-sin(Phi)*(MeasureLength1*((StickEdges-MeasureLength1)/MeasureLength1))] CenterX := (BorderX[1:Num]+BorderX[0:Num-1])/2 CenterY := (BorderY[1:Num]+BorderY[0:Num-1])/2 gen_cross_contour_xld (BorderCross, BorderY, BorderX, 15, rad(45)) gen_cross_contour_xld (CenterCross, CenterY, CenterX, 15, rad(45)) *查找StickWidth数组中大于20的点坐标 IndexFish := find(sgn(StickWidth-20),1) *查找StickWidth数组中小于20的点坐标 IndexFishx := 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) else NumFish := 0 endif *创建一个空的图像区域 gen_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) else NumFlipped := 0 endif * dev_set_window (GraphicsWindow) dev_clear_window () plot_funct_1d (GraphicsWindow, Function) 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' else String := 'Not OK' Color := 'red' endif String[1] := 'Number of fish sticks:'+NumFish$'3' if (NumFish # 15) Color := [Color,'red'] else Color := [Color,'white'] endif if (NumFlipped # 0) String[2] := 'Flipped fishsticks: '+NumFlipped$'3' Color := [Color,'red'] endif disp_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
---------------------
作者:不系之舟913
来源:CSDN
原文:https://blog.csdn.net/lixianjun913/article/details/11761829
版权声明:本文为博主原创文章,转载请附上博文链接!
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:


