halcon-measure_ic_leads.hdev
* Lead Measurement: Example for the application of the measure object * including a lot of visualization operators. * First, the length of the leads is measured, then, their width and distance from each other. * * * First, read in the image and initialize the program read_image (Image, 'ic_pin') get_image_pointer1 (Image, Pointer, Type, Width, Height) dev_close_window () dev_open_window_fit_image (Image, 0, 0, 509, 509, WindowHandle) set_display_font (WindowHandle, 14, 'mono', 'true', 'false') dev_display (Image) dev_set_draw ('margin') dev_set_line_width (3) * * Define and display the rectangular ROIs within which the edges will be detected Row := 55 RowBottom := 955 Column := 200 Phi := rad(-90) Length1 := 50 Length2 := 35 dev_set_color ('gray') gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2) gen_rectangle2 (Rectangle, RowBottom, Column, Phi, Length1, Length2) disp_continue_message (WindowHandle, 'black', 'true') stop () * * Create a measure object for the ROI at the top of the image. Interpolation := 'nearest_neighbor' gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2, Width, Height, Interpolation, MeasureHandle) * * Determine all edges and calculate the lead height at the top of the image Sigma := 1.0 Threshold := 30 Transition := 'all' Select := 'all' measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance) LeadLength1 := Distance * * Display the results dev_set_color ('white') disp_line (WindowHandle, RowEdge, ColumnEdge-Length2, RowEdge, ColumnEdge+Length2) disp_message (WindowHandle, 'Lead Length: '+LeadLength1$'.2f', 'window', RowEdge[1]+40, ColumnEdge[1]+100, 'yellow', 'false') * * Shift the measure object and determine the lead height at the bottom of the image translate_measure (MeasureHandle, RowBottom, Column) measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance) LeadLength2 := Distance * * Display the results dev_set_color ('white') disp_line (WindowHandle, RowEdge, ColumnEdge-Length2, RowEdge, ColumnEdge+Length2) disp_message (WindowHandle, 'Lead Length: '+LeadLength2$'.2f', 'window', RowEdge[1]-120, ColumnEdge[1]+100, 'yellow', 'false') * * Close the measure close_measure (MeasureHandle) disp_continue_message (WindowHandle, 'black', 'true') stop () * * Now, define and display a rectangular ROI perpendicular to the leads and create the measure dev_display (Image) Row := 47 Column := 485 Phi := 0 Length1 := 420 Length2 := 8 Interpolation := 'nearest_neighbor' dev_set_color ('black') gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2) gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2, Width, Height, Interpolation, MeasureHandle) * * Determine all edge pairs that have a negative transition, i.e., edge pairs * that enclose dark regions. Sigma := 1.0 Threshold := 30 Transition := 'negative' Select := 'all' measure_pairs (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance) disp_continue_message (WindowHandle, 'black', 'true') stop () dev_set_color ('white') disp_line (WindowHandle, RowEdgeFirst, ColumnEdgeFirst, RowEdgeSecond, ColumnEdgeSecond) avgLeadWidth := sum(IntraDistance)/|IntraDistance| avgLeadDistance := sum(InterDistance)/|InterDistance| numLeads := |IntraDistance| disp_message (WindowHandle, 'Number of Leads: '+numLeads, 'window', 200, 100, 'yellow', 'false') disp_message (WindowHandle, 'Average Lead Width: '+avgLeadWidth$'.2f', 'window', 260, 100, 'yellow', 'false') disp_message (WindowHandle, 'Average Lead Distance: '+avgLeadDistance$'.2f', 'window', 320, 100, 'yellow', 'false') disp_continue_message (WindowHandle, 'black', 'true') disp_continue_message (WindowHandle, 'black', 'true') stop () * * Zoom in to visualize the measurement results in more detail. * First, define and display the zoom window. Row1 := 0 Column1 := 600 Row2 := 100 Column2 := 700 dev_set_color ('blue') disp_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2) disp_continue_message (WindowHandle, 'black', 'true') stop () * * Then, zoom the image and display the results. dev_set_part (Row1, Column1, Row2, Column2) dev_display (Image) dev_set_color ('black') dev_display (Rectangle) p_disp_edge_marker (RowEdgeFirst, ColumnEdgeFirst, Phi, 5, 'white', 3, WindowHandle) p_disp_edge_marker (RowEdgeSecond, ColumnEdgeSecond, Phi, 5, 'white', 3, WindowHandle) * * Close the measure close_measure (MeasureHandle) * * Reset the display parameters. dev_set_part (0, 0, Height-1, Width-1) dev_set_draw ('fill') dev_set_line_width (1)
几个函数解析
measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance)
同measure_pairs类似(提取边缘对),这个是提取边缘直线即Extract straight edges perpendicular to a rectangle or annular arc
dev_set_part (Row1, Column1, Row2, Column2)
本例看似复杂,其实基本思想同fuse.hdev
1.输入灰度图像
2.gen_measure_rectangle2,划定ROI
3.measure_pairs在ROI内测量边缘对,或者使用measure_pos在ROI内测量边缘