* This example program shows how to use lines_color and the differences
* between the output of lines_color and lines_gauss using an image in which
* some lines can only be extracted from a color image.
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 640, 480, 'black', WindowHandle)
dev_set_part (0, 0, 479, 639)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
NumImages := 2
for J := 1 to NumImages by 1
read_image (Image, 'cable' + J)
dev_display (Image)
disp_message (WindowHandle, 'Color image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Extract color lines from the image. Note that since the color line extractor
* cannot discern between bright or dark lines (in fact, this criterion does not
* even make sense for color lines), the lines corresponding to the colored
* cables are simply selected based on their length.
lines_color (Image, Lines, 3.5, 0, 12, 'true', 'false')
select_contours_xld (Lines, LongLines, 'contour_length', 450, 100000, 0, 0)
dev_display (Image)
dev_set_line_width (2)
dev_set_color ('yellow')
dev_display (LongLines)
disp_message (WindowHandle, 'Color lines', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Now construct XLD contours that are used to visualize the line width. One
* contour for the left and right edge of the line is constructed based on the
* line position, angle, and width_* attributes.
* Note that when the width is visualized you will see several abruptly changing
* line widths. This happens because the edges of the line merge with the
* edges of adjacent lines because of the scale-space effects induced by the
* smoothing that is used in lines_color to extract the lines.
count_obj (LongLines, Number)
gen_empty_obj (EdgesL)
gen_empty_obj (EdgesR)
for K := 1 to Number by 1
select_obj (LongLines, Line, K)
get_contour_xld (Line, Row, Col)
get_contour_attrib_xld (Line, 'angle', Angle)
get_contour_attrib_xld (Line, 'width_right', WidthR)
get_contour_attrib_xld (Line, 'width_left', WidthL)
EdgeRR := Row + cos(Angle) * WidthR
EdgeRC := Col + sin(Angle) * WidthR
EdgeLR := Row - cos(Angle) * WidthL
EdgeLC := Col - sin(Angle) * WidthL
gen_contour_polygon_xld (EdgeR, EdgeRR, EdgeRC)
gen_contour_polygon_xld (EdgeL, EdgeLR, EdgeLC)
concat_obj (EdgesL, EdgeL, EdgesL)
concat_obj (EdgesR, EdgeR, EdgesR)
endfor
dev_display (Image)
dev_set_line_width (2)
dev_set_color ('yellow')
dev_display (LongLines)
dev_set_color ('slate blue')
dev_display (EdgesR)
dev_display (EdgesL)
disp_message (WindowHandle, 'Color lines and their width', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Now convert the image into a gray value image.
rgb1_to_gray (Image, GrayImage)
dev_display (GrayImage)
disp_message (WindowHandle, 'Gray value image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Extract dark lines corresponding to the cables from the gray value image.
* Note that the conversion to a gray value image causes several adjacent
* lines to have almost identical contrasts. Therefore, it is impossible to
* extract the lines corresponding to the cables completely.
lines_gauss (GrayImage, LinesGray, 3.5, 0.0, 0.7, 'dark', 'true', 'bar-shaped', 'false')
select_contours_xld (LinesGray, LongGrayLines, 'contour_length', 50, 100000, 0, 0)
dev_display (GrayImage)
dev_set_line_width (2)
dev_set_color ('yellow')
dev_display (LongGrayLines)
disp_message (WindowHandle, 'Gray value lines', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Now construct XLD contours that are used to visualize the line width. One
* contour for the left and right edge of the line is constructed based on the
* line position, angle, and width_* attributes.
* Note that because of the very low contrast in the gray value image many of
* the line edges are very poorly defined, leading to wrongly extracted widths
* in several places.
count_obj (LongGrayLines, Number)
gen_empty_obj (EdgesGrayL)
gen_empty_obj (EdgesGrayR)
for K := 1 to Number by 1
select_obj (LongGrayLines, Line, K)
get_contour_xld (Line, Row, Col)
get_contour_attrib_xld (Line, 'angle', Angle)
get_contour_attrib_xld (Line, 'width_right', WidthR)
get_contour_attrib_xld (Line, 'width_left', WidthL)
EdgeRR := Row + cos(Angle) * WidthR
EdgeRC := Col + sin(Angle) * WidthR
EdgeLR := Row - cos(Angle) * WidthL
EdgeLC := Col - sin(Angle) * WidthL
gen_contour_polygon_xld (EdgeR, EdgeRR, EdgeRC)
gen_contour_polygon_xld (EdgeL, EdgeLR, EdgeLC)
concat_obj (EdgesGrayL, EdgeL, EdgesGrayL)
concat_obj (EdgesGrayR, EdgeR, EdgesGrayR)
endfor
dev_display (GrayImage)
dev_set_line_width (2)
dev_set_color ('yellow')
dev_display (LongGrayLines)
dev_set_color ('slate blue')
dev_display (EdgesGrayL)
dev_display (EdgesGrayR)
disp_message (WindowHandle, 'Gray value lines and their width', 'window', 12, 12, 'black', 'true')
if (J < NumImages)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
halcon_line_color
于 2022-04-26 16:27:05 首次发布