当前位置:网站首页>Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
2022-07-05 07:56:00 【BoomBiuBiu】
This sample program shows the use of shape models for template matching to find an object . Besides , It also shows how to use the detected position and the rotation of the object , Measure and fit the chip pins
* close pc Update
dev_update_pc ('off')
* Close the update of the window
dev_update_window ('off')
* Close the update of variable window during program operation
dev_update_var ('off')
* Virtual image acquisition ,seq A file is a sequence of text , Read the text line by line , Open the image corresponding to the text path , Simulate a camera to collect images
open_framegrabber ('File', 1, 1, 0, 0, 0, 0, 'default', -1, 'default', -1, 'default', 'board/board.seq', 'default', -1, 1, FGHandle)
* Collect single frame image , Get the width and height of the image
grab_image (Image, FGHandle)
* Get the width and height of the image
get_image_size (Image, Width, Height)
dev_close_window ()
* Open two windows
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_open_window (Height + 70, 0, Width, 120, 'black', WindowHandleText)
dev_set_window (WindowHandle)
* Set the font of two windows
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
set_display_font (WindowHandleText, 16, 'mono', 'true', 'false')
dev_set_color ('red')
* Display images
dev_display (Image)
* Define some parameter drawings ROI
Row1 := 188
Column1 := 182
Row2 := 298
Column2 := 412
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
* get ROI Row and column coordinates
area_center (Rectangle, Area, Row, Column)
* Define some parameters of the measurement rectangle ROI
Rect1Row := -102
Rect1Col := 5
Rect2Row := 107
Rect2Col := 5
RectPhi := 0
* Here is the half width and half height of the rectangle
RectLength1 := 170
RectLength2 := 5
* Draw based on the row and column coordinates of the middle rectangle ROI
gen_rectangle2 (Rectangle1, Row + Rect1Row, Column + Rect1Col, RectPhi, RectLength1, RectLength2)
gen_rectangle2 (Rectangle2, Row + Rect2Row, Column + Rect2Col, RectPhi, RectLength1, RectLength2)
* Start creating template
reduce_domain (Image, Rectangle, ImageReduced)
* The first parameter creates the template ROI Area image
* The second parameter is the number of pyramid layers
* The third parameter is the starting angle of the template
* The fourth parameter is the total angle of the template
* The fifth parameter is the angle step of the template , I'm going to set it to 1°, In other words, a total of 360 Templates for finding images
* The sixth parameter is whether to optimize template points , Choose not to optimize here
* The seventh parameter polarity selection , If you choose to use polarity, you must select the object under the same background as the template , For example, in the template, black objects are selected against a white background , When searching, you are also looking for Black targets under a white background
* The eighth parameter is the template gray threshold , That is, the points with gray difference exceeding this value will be selected as template points ,
* The ninth parameter is the minimum gray value , Used to remove the influence of noise
* The last parameter template ID, You need to use the template later through this ID lookup
create_shape_model (ImageReduced, 4, 0, rad(360), rad(1), 'none', 'use_polarity', 30, 10, ModelID)
* Get the outline of the template 【 notes : The contour line is at the origin 】 Subpixel
get_shape_model_contours (ShapeModel, ModelID, 1)
* Affine transformation , Make the area return to its original position
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
affine_trans_contour_xld (ShapeModel, ShapeModelTrans, HomMat2DTranslate)
*------------------ The operator below can also be used to replace --------------------------
*vector_angle_to_rigid (0, 0, 0, Row, Column, 0, HomMat2DTranslate)
* display picture , Set a series of parameters
dev_display (Image)
dev_set_color ('green')
dev_display (ShapeModelTrans)
* Set the parameters of the measuring rectangle
dev_set_color ('blue')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Rectangle1)
dev_display (Rectangle2)
* Set reminders
dev_set_draw ('fill')
dev_set_line_width (1)
dev_set_color ('yellow')
disp_message (WindowHandle, ['Press left button to start','and stop the demo'], 'window', 12, 12, 'black', 'true')
* Wait for the mouse button to press , Return to the precise image coordinates of the mouse pointer and the pixel pressing the mouse button in the output window
get_mbutton (WindowHandle, Row3, Column3, Button1)
wait_seconds (0.5)
Button := 0
* Has been performed , When the left mouse button is pressed Button=1 The program exits the loop
while (Button != 1)
dev_set_window (WindowHandle)
dev_set_part (0, 0, Height - 1, Width - 1)
* Get a frame of image from the virtual camera and change the angle
grab_image (ImageCheck, FGHandle)
dev_display (ImageCheck)
* Start timing
count_seconds (S1)
* Begin to match
* Find template
* The first parameter is used to find the image
* The second parameter template ID
* The third parameter is the starting angle
* The fourth parameter is the total angle value found , Here is 360 Search in all directions
* The fifth parameter is the minimum score , The found image will have a similarity comparison with the original template , The closer the 1 The more similar the images are
* The sixth parameter is the number of searches , by 0 Time is to find out all the targets
* The seventh parameter is the maximum overlap , Indicates how much overlap can be found between the two targets
* The eighth parameter is sub-pixel accuracy selection
* The ninth parameter is the number of pyramid layers
* The tenth parameter is search greed , The bigger the search, the faster , It means that the less careful you search
* The eleventh parameter is the row coordinates of the target searched , When multiple targets are found, this parameter is an array
* The twelfth parameter is the column coordinates of the target searched , When multiple targets are found, this parameter is an array
* The angle value of the target searched by the thirteenth parameter , When multiple targets are found, this parameter is an array
* The score value of the target searched by the 14th parameter , The closer the 1 The more similar it is to the template , When multiple targets are found, this parameter is an array
find_shape_model (ImageCheck, ModelID, 0, rad(360), 0.7, 1, 0.5, 'least_squares', 4, 0.7, RowCheck, ColumnCheck, AngleCheck, Score)
count_seconds (S2)
dev_display (ImageCheck)
* Determine whether there is a picture in the window , Prevent error reporting
if (|Score| > 0)
dev_set_color ('green')
* Affine transform the template , Change to the location of the searched area , Convenient for visual display on the original drawing
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, RowCheck, ColumnCheck, HomMat2DTranslate)
hom_mat2d_rotate (HomMat2DTranslate, AngleCheck, RowCheck, ColumnCheck, HomMat2DRotate)
affine_trans_contour_xld (ShapeModel, ShapeModelTrans, HomMat2DRotate)
* Display the template outline after translation and rotation
dev_display (ShapeModelTrans)
* Apply arbitrary affine 2D Transform to pixel coordinates .
affine_trans_pixel (HomMat2DRotate, Rect1Row, Rect1Col, Rect1RowCheck, Rect1ColCheck)
affine_trans_pixel (HomMat2DRotate, Rect2Row, Rect2Col, Rect2RowCheck, Rect2ColCheck)
gen_rectangle2 (Rectangle1Check, Rect1RowCheck, Rect1ColCheck, AngleCheck, RectLength1, RectLength2)
gen_rectangle2 (Rectangle2Check, Rect2RowCheck, Rect2ColCheck, AngleCheck, RectLength1, RectLength2)
*------ The following operators can be used instead ---------*
**** The second method
*vector_angle_to_rigid (Row, Column, 0, RowCheck, ColumnCheck, AngleCheck, HomMat2D)
*affine_trans_pixel (HomMat2D, Rect1Row+Row, Rect1Col+Column, Rect11RowCheck, Rect11ColCheck)
*affine_trans_pixel (HomMat2D, Rect2Row+Row, Rect2Col+Column, Rect22RowCheck, Rect22ColCheck)
*** The third method
*vector_angle_to_rigid (0, 0, 0, RowCheck, ColumnCheck, AngleCheck, HomMat2D)
*affine_trans_pixel (HomMat2D, Rect1Row, Rect1Col, Rect1RowCheck, Rect1ColCheck)
*affine_trans_pixel (HomMat2D, Rect2Row, Rect2Col, Rect2RowCheck, Rect2ColCheck)
* Display the measurement rectangle
dev_set_color ('blue')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Rectangle1Check)
dev_display (Rectangle2Check)
* Generate a measurement rectangle
dev_set_draw ('fill')
count_seconds (S3)
gen_measure_rectangle2 (Rect1RowCheck, Rect1ColCheck, AngleCheck, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle1)
gen_measure_rectangle2 (Rect2RowCheck, Rect2ColCheck, AngleCheck, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle2)
* The following function is to extract the edge pairs perpendicular to the test rectangle , Imagine doing gray-scale difference in the direction of the long axis of the rectangle ,
* The obtained curve should have an upper sharp angle and a lower sharp angle for each distance
* The first one with a sharp corner X coordinate RowEdgeFirst, The first one with a sharp corner Y coordinate ColumnEdgeFirst
* The first one in the lower corner X coordinate RowEdgeSecond, The first one in the lower corner Y coordinate ColumnEdgeSecond
* The first parameter is the input image
* The second parameter measures the handle of the rectangle
* The third parameter (1.5) Is Gaussian smoothing sigma value
* Fourth parameter (30) Is the lowest threshold , The height value corresponding to the above sharp corner
* Fifth parameter ('negative') Is the black-and-white direction of the above difference value , by negative The first point is from white to black , If it's for positive From black to white is the first point
* The sixth parameter is ('all') Is to return all measured values , That is, return to all sharp corner positions
* The ninth parameter (AmplitudeFirst) Is the maximum amplitude of the upper sharp angle
*(PinwWidth) Is the distance between the upper corner and the lower corner
*(PinDistance) Is the distance between the lower corner and the upper corner
measure_pairs (ImageCheck, MeasureHandle1, 2, 90, 'positive', 'all', RowEdgeFirst1, ColumnEdgeFirst1, AmplitudeFirst1, RowEdgeSecond1, ColumnEdgeSecond1, AmplitudeSecond1, IntraDistance1, InterDistance1)
measure_pairs (ImageCheck, MeasureHandle2, 2, 90, 'positive', 'all', RowEdgeFirst2, ColumnEdgeFirst2, AmplitudeFirst2, RowEdgeSecond2, ColumnEdgeSecond2, AmplitudeSecond2, IntraDistance2, InterDistance2)
close_measure (MeasureHandle1)
close_measure (MeasureHandle2)
count_seconds (S4)
* Show lines
dev_set_color ('red')
disp_line (WindowHandle, RowEdgeFirst1 - RectLength2 * cos(AngleCheck), ColumnEdgeFirst1 - RectLength2 * sin(AngleCheck), RowEdgeFirst1 + RectLength2 * cos(AngleCheck), ColumnEdgeFirst1 + RectLength2 * sin(AngleCheck))
disp_line (WindowHandle, RowEdgeSecond1 - RectLength2 * cos(AngleCheck), ColumnEdgeSecond1 - RectLength2 * sin(AngleCheck), RowEdgeSecond1 + RectLength2 * cos(AngleCheck), ColumnEdgeSecond1 + RectLength2 * sin(AngleCheck))
disp_line (WindowHandle, RowEdgeFirst2 - RectLength2 * cos(AngleCheck), ColumnEdgeFirst2 - RectLength2 * sin(AngleCheck), RowEdgeFirst2 + RectLength2 * cos(AngleCheck), ColumnEdgeFirst2 + RectLength2 * sin(AngleCheck))
disp_line (WindowHandle, RowEdgeSecond2 - RectLength2 * cos(AngleCheck), ColumnEdgeSecond2 - RectLength2 * sin(AngleCheck), RowEdgeSecond2 + RectLength2 * cos(AngleCheck), ColumnEdgeSecond2 + RectLength2 * sin(AngleCheck))
dev_set_line_width (1)
* Displays the total number of pins , The total number is the sum of the number of pins on both sides
NumLeads := |IntraDistance1| + |IntraDistance2|
MinDistance := min([InterDistance1,InterDistance2])
dev_set_window (WindowHandleText)
dev_set_part (0, 0, 119, Width - 1)
dev_clear_window ()
* Show results
disp_message (WindowHandleText, 'Matching: Time: ' + ((S2 - S1) * 1000)$'5.2f' + 'ms , Score: ' + Score$'7.5f', 'image', 20, 20, 'green', 'false')
disp_message (WindowHandleText, 'Measure: Time: ' + ((S4 - S3) * 1000)$'5.2f' + ' ms, Num. leads: ' + NumLeads$'2d', 'image', 50, 20, 'red', 'false')
disp_message (WindowHandleText, ' Min. lead dist: ' + MinDistance$'6.3f', 'image', 80, 20, 'red', 'false')
endif
dev_error_var (Error, 1)
dev_set_check ('~give_error')
get_mposition (WindowHandle, R, C, Button)
dev_error_var (Error, 0)
dev_set_check ('give_error')
if (Error != H_MSG_TRUE)
Button := 0
endif
endwhile
dev_set_window (WindowHandleText)
dev_close_window ()
* Clear template
clear_shape_model (ModelID)
close_framegrabber (FGHandle)
This is a routine encountered in the learning process , Some Chinese explanations refer to other bloggers , If there is anything incorrect, please point out , Thank you. !
边栏推荐
- Practical application cases of digital Twins - fans
- Altium designer 19.1.18 - clear information generated by measuring distance
- Realization of binary relation of discrete mathematics with C language and its properties
- . Net service governance flow limiting middleware -fireflysoft RateLimit
- 1-stm32 operation environment construction
- Altium Designer 19.1.18 - 隐藏某一个网络的飞线
- ·Practical website·
- 1089 insert or merge, including test point 5
- Day08 ternary operator extension operator character connector symbol priority
- Global and Chinese market of blackbody calibration source 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Process communication mode between different hosts -- socket

Communication standard -- communication protocol

Scm-05 basis of independent keyboard

Realization of binary relation of discrete mathematics with C language and its properties

Significance and requirements of semiconductor particle control

C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,LU分解(LU Decomposition)源程序

万字详解八大排序 必读(代码+动图演示)

Altium designer 19.1.18 - Import frame

Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world

Beijing Winter Olympics opening ceremony display equipment record 3
随机推荐
Function and usage of function pointer
C language uses arrays to realize the intersection, union, difference and complement of sets
Can't find real-time chat software? Recommend to you what e-commerce enterprises are using!
How to select conductive slip ring
UEFI development learning 3 - create UEFI program
MySql——存储引擎
RF ride side door processing of prompt box
Use of orbbec Astra depth camera of OBI Zhongguang in ROS melody
Ten thousand words detailed eight sorting must read (code + dynamic diagram demonstration)
Global and Chinese markets for anesthesia, breathing and sleep apnea devices 2022-2028: Research Report on technology, participants, trends, market size and share
Gradle复合构建
UEFI development learning series
Altium designer learning (I)
Scm-05 basis of independent keyboard
Consul安装
Query the table name used by kettle in Oracle
Ads learning record (lna_atf54143)
Altium designer 19.1.18 - clear information generated by measuring distance
Define in and define out
UEFI development learning 5 - simple use of protocol