当前位置:网站首页>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. !
边栏推荐
- 2021-10-28
- [professional literacy] core conferences and periodicals in the field of integrated circuits
- Altium designer 19.1.18 - hide the fly line of a network
- mysql 盲注常见函数
- Use of orbbec Astra depth camera of OBI Zhongguang in ROS melody
- Can't find real-time chat software? Recommend to you what e-commerce enterprises are using!
- QT excellent articles
- Consul installation
- Process communication mode between different hosts -- socket
- assert_ Usage of param function
猜你喜欢
Record the opening ceremony of Beijing Winter Olympics with display equipment
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
Shell脚本基本语法
Consul installation
Create inf module in AMI code
Factors affecting the quality of slip rings in production
Embedded AI intelligent technology liquid particle counter
万字详解八大排序 必读(代码+动图演示)
Win10 shortcut key
Communication standard -- communication protocol
随机推荐
UEFI development learning 6 - creation of protocol
1089 Insert or Merge 含测试点5
Day06 class variables instance variables local variables constant variables naming conventions
Distinction between heap and stack
Reasons for rapid wear of conductive slip rings
Record the visual shock of the Winter Olympics and the introduction of the screen 2
Global and Chinese markets for medical oxygen machines 2022-2028: Research Report on technology, participants, trends, market size and share
Use of orbbec Astra depth camera of OBI Zhongguang in ROS melody
Global and Chinese market of rammers 2022-2028: Research Report on technology, participants, trends, market size and share
.NET服务治理之限流中间件-FireflySoft.RateLimit
Basic embedded concepts
Global and Chinese market of core pallets 2022-2028: Research Report on technology, participants, trends, market size and share
Using C language to realize IIC driver in STM32 development
研究发现,跨境电商客服系统都有这五点功能!
L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!
1-stm32 operation environment construction
STM32 knowledge points
Extended application of single chip microcomputer-06 independent key
Drive LED -- GPIO control
UEFI development learning 3 - create UEFI program