halcon示例分析:通过网格分离小键盘键
By
admin
at 2018-02-03 • 0人收藏 • 2455人看过

* This program determines the junctions of
* the grid separating the keys of a keypad
*
dev_update_off ()
*
* step: acquire image
*
read_image (Image, 'keypad')
get_image_pointer1 (Image, Pointer, Type, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'white', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
*
* step: initialize visualization
*
dev_set_colored (3)
dev_set_draw ('fill')
*
* step:extract the region of the grid
*
mean_image (Image, ImageMean, 7, 7)
dyn_threshold (Image, ImageMean, RegionDynThresh, 4, 'dark')
connection (RegionDynThresh, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['max_diameter','contlength'], 'and', [200,800], [99999,99999])
closing_circle (SelectedRegions, RegionClosing, 1.5)
dev_display (Image)
dev_display (RegionClosing)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* step:extract the contours of the grid
*
skeleton (RegionClosing, Skeleton)
gen_contours_skeleton_xld (Skeleton, ContoursSkeleton, 1, 'filter')
segment_contours_xld (ContoursSkeleton, ContoursSplitSkeleton, 'lines', 5, 2, 1)
select_contours_xld (ContoursSplitSkeleton, SelectedContours, 'contour_length', 30, 1000, -0.5, 0.5)
union_collinear_contours_xld (SelectedContours, UnionCollinearContours, 100, 10, 20, rad(10), 'attr_keep')
dev_display (Image)
dev_display (UnionCollinearContours)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* step:fit lines to the contours
*
count_obj (UnionCollinearContours, NumberContours)
gen_empty_obj (LinesHorizontal)
gen_empty_obj (LinesVertical)
for i := 1 to NumberContours by 1
select_obj (UnionCollinearContours, ObjectSelected, i)
fit_line_contour_xld (ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
gen_contour_polygon_xld (Contour, [RowBegin,RowEnd], [ColBegin,ColEnd])
Phi := atan2(-Nr,Nc)
if (abs(Phi) < rad(5))
concat_obj (LinesVertical, Contour, LinesVertical)
endif
if (rad(85) < abs(Phi) and abs(Phi) < rad(95))
concat_obj (LinesHorizontal, Contour, LinesHorizontal)
endif
endfor
dev_display (Image)
dev_set_color ('red')
dev_display (LinesVertical)
dev_set_color ('yellow')
dev_display (LinesHorizontal)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* step:extract junction points of the grid
*
RowJunction := []
ColJunction := []
RowRealJunction := []
ColRealJunction := []
count_obj (LinesHorizontal, NumberLH)
count_obj (LinesVertical, NumberLV)
for i := 1 to NumberLH by 1
select_obj (LinesHorizontal, HorizontalLine, i)
get_contour_xld (HorizontalLine, RowHorizontal, ColHorizontal)
for j := 1 to NumberLV by 1
select_obj (LinesVertical, VerticalLine, j)
get_contour_xld (VerticalLine, RowVertical, ColVertical)
intersection_lines (RowHorizontal[0], ColHorizontal[0], RowHorizontal[1], ColHorizontal[1], RowVertical[0], ColVertical[0], RowVertical[1], ColVertical[1], Row, Column, IsOverlapping)
distance_ps (Row, Column, RowHorizontal[0], ColHorizontal[0], RowHorizontal[1], ColHorizontal[1], DistanceH, DistanceHMax)
distance_ps (Row, Column, RowVertical[0], ColVertical[0], RowVertical[1], ColVertical[1], DistanceV, DistanceVMax)
RowJunction := [RowJunction,Row]
ColJunction := [ColJunction,Column]
if ((DistanceH <= 30) and (DistanceV <= 30))
RowRealJunction := [RowRealJunction,Row]
ColRealJunction := [ColRealJunction,Column]
endif
endfor
endfor
dev_set_color ('white')
gen_cross_contour_xld (Cross, RowJunction, ColJunction, 12, 0.785398)
dev_display (Cross)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_display (Image)
dev_set_color ('gray')
dev_display (LinesHorizontal)
dev_display (LinesVertical)
dev_set_color ('white')
gen_cross_contour_xld (Cross, RowRealJunction, ColRealJunction, 12, 0.785398)
dev_display (Cross)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* or via region processing, starting with the
* skeleton obtained above
*
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_display (Image)
junctions_skeleton (Skeleton, EndPoints, JuncPoints)
get_region_points (JuncPoints, RowJunctionRegionProcessing, ColumnJunctionRegionProcessing)
gen_cross_contour_xld (CrossCenter, RowJunctionRegionProcessing, ColumnJunctionRegionProcessing, 12, 0.785398)
dev_set_color ('gray')
dev_display (Skeleton)
dev_set_color ('white')
dev_display (CrossCenter)
disp_message (WindowHandle, 'Result of corresponding', 'window', 40, 10, 'white', 'false')
disp_message (WindowHandle, 'region processing', 'window', 90, 10, 'white', 'false')登录后方可回帖