Shape matching is a contour similarity search and matching function based on edge gradients. It is often used for the positioning and detection of products with fixed shapes in mass production, or to assist other visual detection algorithms in position correction. However, it can only recognize products of one shape.
In industrial production processes, when the same inspection station may classify and pick up different materials of various shapes, it is necessary to identify and locate them. At this time, it is necessary to support the function of matching multiple template contours at the same time. Therefore, we have introduced the multi-contour matching function into motion vision commands.
In the last lesson, we discussed an application example of gear tooth missing detection in machine vision solutions. In this lesson, we will share with you how to implement multi-contour matching.
Instructional Videos
Introduction to the ladder diagram of the detection principle
Multi-contour matching detection algorithms build upon shape matching by adding a list storing various template contour information. After template creation, the corresponding template information is added to the template list, and a unique ID is generated for each template (the default ID starts from 0 and increments sequentially). During multi-contour matching, all target features in the current image are compared with the template list for similarity, and all matching results are returned, including score, position X, position Y, angle, scale, and template ID.
Introduction to Software Implementation of Ladder Diagrams
(I) Software Implementation
1. Open ZDevelop software: Create a new project → Create a new "HMI" file → Create a new "main.bas" file to write the interface response function → Create a new "global_variable.bas" file to store global variables and enable HMI automatic task execution → Create a new "InitLocator.bas" file to initialize measurement parameters → Create a new "camera.bas" file to implement camera acquisition function → Create a new "draw.bas" file to update the drawing graphics and refresh the interface → Add the files to the project.
2. Design the HMI interface.
3. Define global variables in the "global_variable.bas" file, and then run the "Hmi.hmi" file after the definition is complete.
'Global variables mostly use array structures.'''''''Note: In BASIC programming, many functions use TABLE (system data structure) as parameters.''table Description''0~1 Image width and height 15~18 locator ROI image coordinate data''0~1 Number of rows and columns in the matching result matrix 25~28 locator ROI control and image data''53~62 Matching result 30~31 Eraser ROI coordinates''11~12 Coordinates obtained during mouse operation 40~46 Matching parameters''
'***********Define program task-related variables**********************
'Main Task Status' 0 - Uninitialized '1 - Stopped '2 - Running '3 - Stopping GLOBAL DIM main_task_state main_task_state = 1
'Run task switch GLOBAL DIM run_switch run_switch = 0'
'Collection Task Switch' 0 - Stop Collection '1 - Request Collection of GLOBAL DIM grab_switchgrab_switch = 0
'Location detection main task ID - 10GLOBAL DIM main_task_id main_task_id = 10'
'Camera continuous acquisition thread ID - 7GLOBAL DIM grab_task_idgrab_task_id = 7'
'***********End of program task related variables******************'
'***********Define camera acquisition related variables**********************'
'Camera type, Hikvision camera used here - "mvision"GLOBAL DIM CAMERA_TYPE(100)'CAMERA_TYPE = "mindvision;basler;mvision;huaray;zmotion"CAMERA_TYPE = "mvision"
Number of cameras: GLOBAL cam_num cam_num = 0
Camera mode, -1 for continuous acquisition, 0 for software-triggered acquisition. GLOBAL cam_mode cam_mode = 0
'***********End of definition of camera acquisition related variables******************'
Define a flag to return to the main interface: 1 - returned, 0 - not returned. GLOBAL DIM d_is_rtn_loc d_is_rtn_loc = 1
'***********Define template-related variables************************' Defines a mouse down flag: 1 - mouse button pressed, 0 - mouse button not pressed. GLOBAL DIM is_set_roi_m_down is_set_roi_m_down = 0
Define a template creation flag: 1 - template created, 0 - template not created. GLOBAL DIM d_is_creModel d_is_creModel = 0
'Learn template parameters, starAngle, endAngle, minScale, maxScale, thresh, numlevel, reduce, angleStep, scaleStepGLOBAL DIM d_mod_param(9)
'***********End of template definition related variables**********************'
'***********Define related variables for editing templates*********************'
Define the template editing flag: 0 indicates no template editing, 1 indicates template editing. GLOBAL DIM d_edit_md_edit_m = 0
Define the eraser function flags: 0 - indicates the erased area to be restored, 1 - indicates the erased area to be removed. GLOBAL DIM d_isMask_m d_isMask_m = 1
'Define the roi parameters of the eraser, which are the image coordinates x, y, x, y of the top left and bottom right corners of the rectangle respectively. GLOBAL DIM d_locator_roi(4), d_eraser_roi(4)
Define the width of the square eraser: GLOBAL DIM d_eraser_sized_eraser_size = 5
'Define the rectangular area of the eraser on the interface control GLOBAL DIM c_rect(4) 'Define the mouse state flags, 0 - indicates the mouse is released, 1 - indicates the mouse is pressed GLOBAL DIM d_mouse_s d_mouse_s = 0
'***********End of template definition editing related variables******************'
'***********Define matching and detection related variables*********************'
Define the ROI parameters for the learning template and the eraser, which are the image coordinates x, y, x, y of the top left and bottom right corners of the rectangle, respectively. GLOBAL DIM d_locator_roi(4), d_eraser_roi(4)
'Matching results, score, x, y, angle, scale. Currently, for multi-target matching, only the first target is stored. GLOBAL DIM d_match_rst(5)
GLOBAL DIM d_match_time 'Defines the time variable consumed for matching and positioning d_match_time = 0
'***********End of definition, matching and detection related variables******************'
Define variables for caching intermediate and final images during program execution: GLOBAL ZVOBJECT grabImg, GLOBAL ZVOBJECT subImg, copy_subImg, colorSubImg, s_mod, modList, mat_param, and GLOBAL ZVOBJECT modRe.
GLOBAL DIM C_RED, C_GREEN, C_BLUE, C_YELLOWC_RED = ZV_COLOR(255, 0, 0)C_GREEN = ZV_COLOR( 0,255, 0)C_BLUE = ZV_COLOR( 0, 0,255)C_YELLOW= ZV_COLOR(255,255, 0)
Display the printed characters GLOBAL. ShowString(64), ShowString1(64)
'***********Defines variables related to the local file reading function**************''Note: This function is only valid when using the emulator.'Defines whether to use the local image flag GLOBAL DIM d_use_imgfile
Define the local image index GLOBAL DIM d_index
Define the path to read the image: GLOBAL DIM File_Name(100)
'***********End of definition of variables related to reading local files**********'
After initializing global variables, start the HMI file using `RUN "Hmi1.hmi",1`.
4. Initialize the measurement parameters in the "InitLocator.bas" file.
end
GLOBAL SUB init_meas_param() 'Initialize measurement parameters
'Initialize locator roi parameters d_locator_roi(0) = 240 'Top left corner x d_locator_roi(1) = 180 'Top left corner y d_locator_roi(2) = 400 'Bottom right corner x d_locator_roi(3) = 300 'Bottom right corner y 'Initialize template parameters d_mod_param(0) = -180 'Starting angle d_mod_param(1) = 180 'Ending angle d_mod_param(2) = 1 'Minimum scaling d_mod_param(3) = 1 'Maximum scaling d_mod_param(4) = 80 'Threshold d_mod_param(5) = 0 'Default pyramid level d_mod_param(6) = 0 'Default reduced feature points d_mod_param(7) = 0 'Default angle step size d_mod_param(8) = 0 'Default scaling step size' Initialize matching measurement parameters TABLE(40) = 70 'Minimum score TABLE(41) = 1 'Number of matches TABLE(42) = 10 'Default minimum spacing TABLE(43) = 40 'Minimum threshold TABLE(44) = 0 'Accuracy TABLE(45) = 9 'Speed TABLE(46) = 0 'Polarity' Initialize matching positioning result d_match_rst(0) = 0 'Score d_match_rst(1) = 0 'Position X d_match_rst(2) = 0 'Position Y d_match_rst(3) = 0 'Angle d_match_rst(4) = 0 'Scale' Initialize matching positioning time d_match_time = 0 d_use_imgfile = 1 'Default use local image d_index = 0
END SUB
5. Associate HMI interface control variables.
6. Add an HMI interface initialization function to the "main.bas" file and associate the initialization function in the HMI system settings.
The HMI interface initialization function executes once upon power-up: GLOBAL SUB hmi_init()
ZV_ENVINIT() ZV_SETSYSSTR("DataDir","") 'Set the default data directory ZV_SETSYSINT("LineWidth",6) 'Set the line width to 10 grab_switch = 0 'Initialize the acquisition task switch, disabling the acquisition task main_task_state = 1 'Initialize the positioning and detection main task status to stopped state 1 ZV_RESETCLIPSIZE(1280, 960) 'Set the image region cropping size according to the image resolution, here the image resolution is 1280x960 ZV_LATCHSETSIZE(0, HMI_CONTROLSIZEX(10, 2), HMI_CONTROLSIZEY(10, 2)) 'Set the latch size init_meas_param() 'Initialize the measurement parameters ZV_IMGGENCONST(subImg,40,30,1,0,0) 'Initialize the template sub-image' 'Initialize the intermediate variable used for matching ZVOBJECT contlist1, tsContlist1, mat_rigid1 ZVOBJECT contlist2, tsContlist2, mat_rigid2 ZV_LATCHCLEAR(0) 'Clear the latch channel ZV_MATGENDATA(mat_param, 1, 7, 40) 'Generate the data in row 1, column 9, and column 40 of the TABLE into matrix mat_param END SUB
7. Add functions to the "camera.bas" file to capture the responses of relevant buttons in the HMI interface and associate them with action functions. (Note: The specific implementation of these functions has already been demonstrated in the previous two lessons and will not be repeated here.)
8. Click [Component] → [New Window] to create a new learning template window, design the window layout, and associate variables.
9. Add the function that responds to the "Learning Template" button on the main interface to the "draw.bas" file and associate it with the action function.
The function GLOBAL SUB btn_sel_loc() that responds when the learning template button is pressed on the main interface.
ZV_LATCHSETSIZE(0, HMI_CONTROLSIZEX(11, 60), HMI_CONTROLSIZEY(11, 60)) 'Sets the latch size of latch channel 0 for creating the template window SET_COLOR(RGB(0,255,0)) 'Specifies the color used by the draw command ZV_LATCHCLEAR(0) 'Clears latch channel 0 ZV_LATCH(grabImg, 0) 'Displays the captured image in latch channel 0 ZV_LATCH(colorSubImg, 1) 'Displays the template image in latch channel 1 is_redraw = 0 'Sets the drawing flag to 0 d_is_rtn_loc = 0 'Sets the return to interface flag to 0 'Stores the image coordinates of the template area into the table array starting at address 25 TABLE(25, d_locator_roi(0), d_locator_roi(1),d_locator_roi(2),d_locator_roi(3)) ZV_POSFROMIMG(0, 2, 25, 25) 'Convert image coordinates to HMI control coordinates HMI_SHOWWINDOW(11) 'Create template window with pop-up window number 11
END SUB
10. Add a template area update drawing function to the "draw.bas" file, and associate the refresh function name and the drawing function name on the custom control of the learning template interface.
'Update the locator area based on mouse operations, i.e., the effective area of the learning template GLOBAL SUB update_locator()
if mouse_scan(11) = 1 then 'Scan mouse down operation is_set_roi_m_down = 1 'Set mouse down flag to 1 sr_mpos_x = table(11) 'Assign the coordinates of the current mouse down position to the variable sr_mpos_y = table(12) 'Only when the mouse is pressed can the hit position be changed, get the hit area number corresponding to the mouse click position hit_pos = ZV_HMIADJRECT(sr_mpos_x, sr_mpos_y, 25, -1) is_redraw = 1 'Set the drawing flag to 1 endif if mouse_scan(11) = -1 then 'Scan mouse release operation is_set_roi_m_down = 0 'Set mouse down flag to 0 sr_mpos_x = table(11) 'Assign the coordinates of the current mouse release position to the variable sr_mpos_y = table(12) 'Adjust the locator region position according to the region number ZV_HMIADJRECT(sr_mpos_x, sr_mpos_y, 25, hit_pos) is_redraw = 1 'Set the drawing flag to 1 endif 'If the mouse is pressed if (is_set_roi_m_down and MOUSE_state(11)) then sr_mpos_x = table(11) 'Assign the coordinates of the current mouse pressed position to the variable sr_mpos_y = table(12) 'Adjust the locator region position according to the region number ZV_HMIADJRECT(sr_mpos_x, sr_mpos_y, 25, hit_pos) is_redraw = 1 'Set the drawing flag to 1 endif if (1 = is_redraw) then 'If the drawing flag = 1 is_redraw = 0 'Set the drawing flag to 0 'Convert control ROI coordinates to image ROI coordinates. Control coordinates are stored in an array starting at address 25, and image coordinates are stored in an array starting at address 15. ZV_POSTOIMG(0, 2, 25, 15) 'Assign the image coordinate data to the ROI variable d_locator_roi(0) = TABLE(15) d_locator_roi(1) = TABLE(16) d_locator_roi(2) = TABLE(17) d_locator_roi(3) = TABLE(18) SET_REDRAW 'Redraw the entire area endif END SUB
11. Add the function for the "Template Capture" button response and associate it with the action function in the "main.bas" file.
The function `GLOBAL SUB btn_getSubImg()` responds after the "Capture Template" button is pressed on the template creation interface.
LOCAL mod_w,mod_h 'Generate template sub-image based on ROI data ZV_IMGGETSUB(grabImg, subImg, d_locator_roi(0), d_locator_roi(1), d_locator_roi(2)-d_locator_roi(0)+1, d_locator_roi(3)-d_locator_roi(1)+1) 'Get image information and store it in the table array starting at address 0 ZV_IMGINFO(subImg,0) mod_w = TABLE(0) mod_h = TABLE(1) 'Generate template region based on image information ZV_REGENRECT(modRe,0,0,mod_w, mod_h) 'Clear latch channel 1 ZV_LATCHCLEAR(1) 'Display the template sub-image in latch channel 1 ZV_LATCH(subImg, 1) END SUB
12. Click [Component] → [New Window] to create a new editing template window, design the window layout, and associate variables.
13. Add a function to the "draw.bas" file to create the response of the "Eraser" button in the template interface and associate it with an action function.
The function `GLOBAL SUB btn_sel_erase()` responds when the eraser button is pressed in the template interface.
ZV_LATCHSETSIZE(1, HMI_CONTROLSIZEX(12, 1), HMI_CONTROLSIZEY(12, 1)) 'Sets the size of latch channel 1 SET_COLOR(RGB(0,255,0)) 'Sets the color used by the pen when drawing ZV_LATCHCLEAR(1) 'Clears latch channel 1 ZV_IMGCOPY(subImg, copy_subImg) 'Copies the template sub-image to the image variable copy_subImg ZV_REGION(copy_subImg, modRe, 1, 0) 'Draws the invalid area of the modRe image on the template image, with the drawing color being black, used for masking ZV_LATCH(copy_subImg, 1) 'Displays the copied template image HMI_SHOWWINDOW(12) 'Opens the template editing window END SUB
14. Add an eraser function to the "draw.bas" file and associate the refresh function name and the drawing function name with the custom control in the editing template interface.
'Update the eraser erase/restore area position according to mouse operation GLOBAL SUB update_eraser() DIM c_size_eraser 'The size of the eraser on the control DIM eraser_pos_x,eraser_pos_y d_mouse_s = MOUSE_STATE(11) 'When the mouse is pressed, eraser_pos_x = TABLE(11) 'Assign the coordinates of the current mouse pressed position to the eraser control coordinate variable eraser_pos_y = TABLE(12) c_size_eraser = ZV_LENFROMIMG(0, d_eraser_size) 'Convert the eraser image size to the control size' Generate a square eraser area centered at (eraser_pos_x, eraser_pos_y) with a side length of 2*c_size_eraser c_rect(0, eraser_pos_x - c_size_eraser, eraser_pos_y - c_size_eraser, eraser_pos_x + c_size_eraser, eraser_pos_y + c_size_eraser) DIM hmi_w,hmi_h 'Restrict eraser coordinates within the image element area if (eraser_pos_x >= c_size_eraser) and (eraser_pos_y >= c_size_eraser) and (eraser_pos_x <= HMI_CONTROLSIZEX(12, 1) - c_size_eraser) and (eraser_pos_y <= HMI_CONTROLSIZEy(12, 1) - c_size_eraser) THEN 'Redraw latch channel 0 area on the edit template window SET_REDRAW(0,0, HMI_CONTROLSIZEX(12, 1), HMI_CONTROLSIZEY(12, 1)) endif if d_mouse_s = 1 and d_edit_m = 1 then 'If the mouse is pressed and the edit template flag = 1, then btn_pro_eraser() 'Execute the eraser function endif END SUB
'The eraser function GLOBAL SUB btn_pro_eraser() ZVOBJECT tmp_re 'Stores the x and y coordinates of the top left corner of the eraser area into an array starting at address 30 TABLE(30, c_rect(0), c_rect(1)) 'Converts the control coordinates to image coordinates ZV_POSTOIMG(1, 1, 30, 30) 'Generates a square eraser area based on the data in the image coordinate system and stores it in the tmp_re variable ZV_REGENRECT(tmp_re, TABLE(30), TABLE(31), 2 * d_eraser_size + 1, 2 * d_eraser_size + 1) if (d_isMask_m = 1) then 'If the masking function is selected ZV_REDIFF(modRe, tmp_re, modRe) 'Calculate the difference between modRe and tmp_re and store it in modRe, i.e., take the template sub-image region outside the eraser area else 'If the restore function is selected ZV_REUNION(modRe, tmp_re, modRe) 'Calculate the union of modRe and tmp_re and store it in modRe, i.e., take the current template sub-image region endif ZV_IMGCOPY(subImg, copy_subImg) 'Copy the template sub-image to the image variable copy_subImg ZV_REGION(copy_subImg, modRe, 1, 0) 'Draw the invalid region of the modRe image on the template image, the drawing color is black, used for masking ZV_LATCH(copy_subImg, 1) 'Display the copied template image END SUB
'Update the eraser region GLOBAL SUB draw_eraser() if d_edit_m = 0 then 'If the edit template flag is 0 return 'Return to the sub-function and do not continue execution endif 'Draw the square eraser region DRAWRECT(c_rect(0), c_rect(1), c_rect(2), c_rect(3)) END SUB
15. Add a function to the "Create Template" button in the "main.bas" file to respond to the template editing interface and associate it with an action function.
The function GLOBAL SUB btn_loc_creModel() that responds when the "Create Template" button is pressed in the template editing interface.
d_is_creModel = 1 'Set the template creation flag to 1 'Create a template based on the template parameters and template sub-image, and store the template result in the s_mod variable ZV_SHAPECREATERE(subImg, modRe,s_mod, d_mod_param(0), d_mod_param(1), d_mod_param(2), d_mod_param(3), d_mod_param(4), d_mod_param(5), d_mod_param(6), d_mod_param(7), d_mod_param(8)) ZV_SHAPECONTOURS(s_mod, contlist1, 0) 'Get the template outline on the 0th layer pyramid ZV_GRAYTORGB(subImg, colorSubImg) 'Convert the grayscale image to an RGB image ZV_IMGINFO(colorSubImg, 0) 'Get the image information of colorSubImg and store it in table0 ZV_GETRIGIDVECTOR(mat_rigid1, 0, 0, 0, TABLE(0)/2, TABLE(1)/2, 0)' Calculate the rigid transformation matrix ZV_CONTAFFINE(contlist1, mat_rigid1, tsContlist1)' Perform affine transformation on the contour or contour sequence ZV_CONTLIST(colorSubImg, tsContlist1, ZV_COLOR(0, 255, 0), 0)' Draw the green contour sequence on the colorSubImg image ZV_LATCHCLEAR(2)' Clear latch channel 2 ZV_LATCH(colorSubImg, 2)' Display the image result in latch channel 2 END SUB
16. Add a function to the "Add Template" button in the "main.bas" file to respond to the template editing interface and associate it with an action function.
The function `GLOBAL SUB btn_loc_addModel()` that responds when the "Add Template" button is pressed in the template editing interface.
DIM count if d_is_creModel = 1 then zv_listinsert(s_mod, modList) count = ZV_LISTCOUNT(modList) 'Get the number of items in the list ShowString="Added template"+TOSTR(count) ?ShowString endif END SUB
17. Add a function to the "Clear Template" button in the "main.bas" file to handle the editing template interface and associate it with an action function.
The function that responds when the "Clear Template List" button is pressed in the template editing interface is: GLOBAL SUB btn_loc_clrMods() ZV_CLEAR(modList) "Template list cleared!"END SUB
18. Add a function to the "draw.bas" file to respond to the "OK" button in the editing template interface and associate it with an action function.
The function executed when the OK button is pressed in the template editing interface is GLOBAL SUB btn_erase_cfm().
ZV_LATCHCLEAR(0) 'Clear latch channel 0' ZV_LATCH(grabImg, 0) 'Display image on latch channel 0' HMI_CLOSEWINDOW(12) 'Close the edit template window' END SUB
19. Add a function to the "main.bas" file to create the response of the "Test" button in the template interface and associate it with an action function.
'When the test button is pressed on the template creation interface, the function GLOBAL SUB btn_loc_test() if (d_is_creModel = 0 OR ZV_LISTCOUNT(modList)=0 ) then 'No template created!' 'Prompts an error message that no template has been created and returns the sub-function without proceeding. return endif 'Start matching TICKS = 0 'Start timing ZVOBJECT match_rst, sImg, colorImg,contours ZV_CLEAR(match_rst) ZV_MATINFO(match_rst, 0) 'Get matrix result information.' And store it in the table array starting at address 0. Perform 3*3 Gaussian smoothing on the image, and output the processed image to the sImg variable. ZV_GAUSSBLUR(grabImg, sImg, 3) ZV_SHAPEFINDS(modlist,mat_param,sImg,match_rst,3,0) Multi-target matching ZV_MATINFO(match_rst, 0) Get matrix result information. And store it in the table array starting at address 0. ZV_GRAYTORGB(sImg, colorImg) ' Convert the grayscale image to an RGB image and use it as the matching result image if TABLE(0) > 0 then ' If the target is matched, local rowr for rowr = 0 to TABLE(0)-1 ' Get the data of rowr in the match_rst matrix and store it in the table array starting at address 53. The maximum length of table is 6. ZV_MATGETROW(match_rst, rowr, 6, 53) zv_listget(modList, s_mod, table(58)) ZV_SHAPECONTOURS(s_mod, contours, 0)' Get the template contours ZV_GETRIGIDVECTOR(mat_rigid1, 0, 0, 0, TABLE(54), TABLE(55), TABLE(56)) ZV_CONTAFFINE(contours, mat_rigid1, contours) ZV_CONTLIST(colorImg, contours, C_GREEN, 0) if TABLE(58)=0 then ShowString="target1:" elseif TABLE(58)=1 then ShowString="target2:" else ShowString="target3:" endif ShowString1=TOSTR(TABLE(54),1,2)+","+TOSTR(TABLE(55),1,2)+","+TOSTR(TABLE(56),1,2) TABLE(54)=TABLE(54)-40 ZV_TEXT(colorImg,ShowString,TABLE(54), TABLE(55),50,ZV_COLOR(255,0,255) ) TABLE(55)=TABLE(55)+50 ZV_TEXT(colorImg,ShowString1,TABLE(54), TABLE(55),50,ZV_COLOR(255,0,255) ) next else 'If no match is found, assign the result to -1 d_match_rst(0) = -1 d_match_rst(1) = -1 d_match_rst(2) = -1 d_match_rst(3) = -1 d_match_rst(4) = -1 endif d_match_time = abs(TICKS) 'Calculate the matching time ZV_LATCH(colorImg, 0) 'Display the matching result image END SUB
20. Add a function to the "draw.bas" file to respond to the "OK" button in the template interface and associate it with an action function.
The function `GLOBAL SUB btn_loc_cfm()` responds when the "OK" button is pressed during template creation.
grab_switch = 0 'Disable continuous acquisition of the correction source d_is_rtn_loc = 1 'Return to main interface flag set to 1 'Set the size of latch channel 0 ZV_LATCHSETSIZE(0, HMI_CONTROLSIZEX(10, 2), HMI_CONTROLSIZEY(10, 2)) ZV_LATCHCLEAR(0) 'Clear latch channel 0 ZV_LATCH(grabImg, 0) 'Display image on latch channel 0 HMI_CLOSEWINDOW(11) 'Close the template creation window END SUB
21. Add a function to the "Single Execution" button on the main interface to the "main.bas" file and associate it with an action function.
The function GLOBAL SUB btn_test() that responds when the execute button is pressed once on the main interface.
'Execute the single-capture response function to obtain one frame of image btn_grab() 'Execute the test button response function to perform matching and localization detection btn_loc_test() END SUB
22. Add a function to the "Continuous Run" button on the main interface to the "main.bas" file and associate it with an action function.
'When the continuous run button is clicked on the main interface, the function responds to GLOBAL SUB btn_run() if(run_switch = 1) then 'If continuous run is already enabled? "Continuous run is enabled, please do not repeat the operation!" 'Prompt a message and exit the sub-function without proceeding to the next step return endif run_switch = 1 'Set the main task switch to 1 if (1 = run_switch) then 'If the main task switch = 1 if (0 = PROC_STATUS(main_task_id)) then 'If the main_task_id task is not enabled RUNTASK main_task_id, main_task 'Enable the main_task task endif endifEND SUB
'Main task execution content main_task: while(1) if (0 = run_switch) then 'If the main task switch = 0, i.e., the stop button is pressed, exit while 'Exit the loop endif 'Otherwise, repeat the following operations if (d_use_imgfile=1) then 'If d_use_imgfile=1, use the local image reading function if (d_index=3) then d_index=0 endif File_Name="Multi-outline/"+TOSTR(d_index,1,0)+".bmp" 'The path name of the image when reading the local image ZV_IMGREAD(grabImg,File_Name,0) d_index=d_index+1 else 'If the number of cameras is 0, prompt to scan the camera first, and exit the sub-function without continuing execution if cam_num = 0 then 'Please scan the camera first!' return endif CAM_SETPARAM("TriggerSoftware", 0) 'Send the trigger command CAM_GET(grabImg, 0) endif 'Execute the test button response function to perform matching and location detection btn_loc_test() wend END
23. Add a function to the "Stop Running" button on the main interface and associate it with an action function in the "main.bas" file.
The function GLOBAL SUB btn_stop() that responds when the stop execution button is clicked on the main interface.
if(run_switch = 0) then 'If the main task switch = 0, 'Continuous running is not enabled!' 'Prompt that the loop task is not enabled and exit the sub-function without further execution return endif run_switch = 0 'Set the main task switch to 0 and exit the loop END SUB
III. Operation Demonstration and Ladder Diagram Introduction
(I) Operating Procedures
To check the running effect: Download the project to the simulator → Use local images → Single capture → Learn templates → Capture templates → Use the eraser to create the required templates one by one → Add the templates one by one → Click "Test" to view the effect, then click "OK" to return to the main interface. → Click "Run Continuously" to view the running effect → End.
(II) Effect Demonstration
This concludes our quick start guide to the VPLC series machine vision motion control all-in-one machine from Zhengdong Technology (Part 11: Multi-contour matching).
This article is original content from Zheng Motion Technology. We welcome everyone to reprint it for mutual learning and to jointly improve China's intelligent manufacturing level. Copyright belongs to Zheng Motion Technology. Please indicate the source if you reprint this article.