Share this

Quick Start Guide to the VPLC Series Machine Vision Motion Control All-in-One Machine (Part 5)

2026-04-06 05:45:14 · · #1

Previously, we explained the hardware and software introduction and counting examples, the basic use of the camera, shape matching-based visual positioning, and BLOB presence/absence detection.

Today, Zheng Motion Technology will share with you a quick start guide to the VPLC series machine vision motion control all-in-one machine (Part 5), and share with you the commonly used detection function of machine vision---measurement of dimensions.

A video tutorial on the commonly used machine vision inspection function "Dimension Measurement": 《Video Tutorial: Quick Start Guide to VPLC Series Machine Vision Motion Control All-in-One Machine (Part 5)》

Dimensional measurement: The principle of dimensional measurement commonly used in machine vision is to use two linear measuring instruments to detect the points on the two dimensions of the product, fit them into a straight line, and then take the distance between the two straight lines.

Size measurement is essentially based on edge detection. It requires first detecting edge points where the transition between light and dark meets a certain threshold, and then extracting the edge point data for processing and calculation before outputting the final result.

Measurement dimensional characteristics

1. High accuracy

Choosing the right hardware can achieve high detection accuracy. Telecentric lenses are commonly used to reduce distortion, increase depth of field, and minimize measurement errors.

2. Less image interference

For simple dimensional measurement projects, backlighting is often used to highlight the product's edge dimensions and filter out surface interference.

3. Simple to implement

The algorithm for measuring dimensions is based on edge detection and is easy to implement.

4. Position following is required.

The measuring instrument itself does not have a positioning function. If the position of the product being measured is not fixed, the position of the measuring instrument cannot be determined. In this case, positioning functions such as matching are required to perform position tracking.

Calibration

Calibration refers to converting the pixel results (unit: pixels) processed by machine vision into actual results (unit: millimeters) used in reality, or converting the image coordinates used in machine vision into world coordinates.

When measuring dimensions, we typically use international standard units such as meters, centimeters, and millimeters. Therefore, in machine vision measurement projects, it is necessary to convert the acquired pixel dimensions into actual dimensions (millimeters) and output them to the user for direct use.

Measurement Dimension Calibration Method

1. Measurement Calibration

Using standard modules of known dimensions, such as circles, small squares, or rulers, the pixel size is detected in the image. Dividing the known actual size value by the pixel value yields the pixel ratio (unit: mm/pixel). This is a commonly used measurement calibration method in dimensional measurement projects.

2. Coordinate calibration

Input several sets of image coordinates (at least 9 sets), and then input the corresponding world coordinates. The matrix transformation coefficients are calculated using a formula, thus converting the image coordinates to actual coordinates. Coordinate calibration can be considered for dimensional measurement projects with high accuracy requirements; we will explain the specific implementation in detail in the next lesson.

1. Open ZDevelop software: Open the project "Vision Positioning Based on Shape Matching" → Define the global variables needed for measuring dimensions in the global_variable.bas file.


'----------------------------Divider-----------------------------------------'

The line 1 measurement parameter array consists of the center (cx), cy, w, h, angle, interp, sub_num, sub_width, filter_size, thresh, poll, and select, all of which are image coordinates.

GLOBAL DIM d_meas_param1(12) 'The 'd' prefix indicates a data structure

The line 2 measurement parameter array consists of the center cx, cy, w, h, angle, interp, sub_num, sub_width, filter_size, thresh, poll, and select, all of which are image coordinates.

GLOBAL DIM d_meas_param2(12) 'The 'd' prefix indicates a data structure

Define commonly used color variables for drawing graphics.

GLOBAL C_RED, C_GREEN, C_BLUE, C_YELLOW

C_RED = RGB(255, 0, 0)

C_GREEN = RGB( 0,255, 0 )

C_BLUE = RGB( 0, 0, 255)

C_YELLOW = RGB(255, 255, 0)

The reference region position vector 1 for line 1, x, y, and angle, saved when the template was created.

GLOBAL DIM d_meas_base_v1(3)

d_meas_base_v1(0) = 0

d_meas_base_v1(1) = 0

d_meas_base_v1(2) = 0

The reference region position vector 2 of line 2, x, y, and angle, saved when the template was created.

GLOBAL DIM d_meas_base_v2(3)

d_meas_base_v2(0) = 0

d_meas_base_v2(1) = 0

d_meas_base_v2(2) = 0

The measurement results for line 1 are, in order, the result points stx, sty, endx, and endy.

GLOBAL DIM d_meas_rst1(4)

The measurement results for line 2 are, in order, the result points stx, sty, endx, and endy.

GLOBAL DIM d_meas_rst2(4)

Define the length variable to be displayed on the interface.

GLOBAL DIM d_show_rst

Template baseline points saved when creating the template, including score, x, y, angle, and scale.

GLOBAL DIM d_match_base_rst(5)

global dim d_meas_param(12)

Define the variables used during measurement calibration.

GLOBAL PixLength,WorldLength,CalibParam

2. Added design to the main interface.

3. Create a new main interface with a measurement parameter setting window "Set_Select" that pops up when the "Measurement Settings" button is pressed, and design the interface layout.

4. Add a function to the main.bas file that responds when the "Measurement Settings" button is pressed on the main interface and associate it with an action function name.

'Function that responds when the measurement settings button is pressed on the main interface'

GLOBAL SUB Goto_Meas()

HMI_SHOWWINDOW(13)

END SUB

5. Initialize the measurement parameters in the InitLocator.bas file.

'---------------------Divider------------------'

'Initialize measurement parameters 1'

d_meas_param1(0) = 320.0 'roi center x

d_meas_param1(1) = 240.0 'roi center y

d_meas_param1(2) = 160 'roi width'

d_meas_param1(3) = 120.0 'roi high

d_meas_param1(4) = 0.0 'roi angle'

d_meas_param1(5) = 1 'Interpolation method

d_meas_param1(6) = 20 'Number of subregions'

d_meas_param1(7) = 5 'Size of the subregion

d_meas_param1(8) = 3 'Filter size

d_meas_param1(9) = 50 'Threshold

d_meas_param1(10) = 0 'Polarity

d_meas_param1(11) = 0 'Select edge position

'Initialize measurement parameters 2'

d_meas_param2(0) = 320.0 'roi center x

d_meas_param2(1) = 240.0 'roi center y

d_meas_param2(2) = 160 'roi width'

d_meas_param2(3) = 120.0 'roi high'

d_meas_param2(4) = 0.0 'roi angle'

d_meas_param2(5) = 1 'Interpolation method

d_meas_param2(6) = 20 'Number of subregions'

d_meas_param2(7) = 5 'Size of the subregion

d_meas_param2(8) = 3 'Filter size

d_meas_param2(9) = 50 'Threshold

d_meas_param2(10) = 0 'Polarity

d_meas_param2(11) = 0 'Select edge position

The result value displayed on the initialization interface

d_meas_rst1(0) = 0

d_meas_rst1(1) = 0

d_meas_rst1(2) = 0

d_meas_rst1(3) = 0

d_meas_rst2(0) = 0

d_meas_rst2(1) = 0

d_meas_rst2(2) = 0

d_meas_rst2(3) = 0

d_show_rst = 0

'Initialize calibration parameters and pixel ratio'

CalibParam = 0

WorldLength = 0

PixLength = 0

6. Create a new “Set_Roi1” window that pops up when you press the “Select Measurement Area 1” button in the “Set_Select” window interface. This window is used to create the first straight line measurement tool for the dimension edge and set the parameters for detecting the straight line.

7. Add a function to the draw.bas file that responds when the "Select Measurement Area 1" button is pressed in the "Set_Select" window and associate it with an action function name.

'-----------------Divider-------------------------'

'Measuring Instrument Drawing'

DIM is_redraw

is_redraw = 0

DIM set_roi_open_init

set_roi_open_init = 0

DIM sr_mpos_x, sr_mpos_y, hit_pos

'''''''''''''''''''''''''''''''''''

The function that responds when the button to select measurement area 1 is pressed.

GLOBAL SUB btn_sel_roi1()

ZV_LATCHCLEAR(1) 'Clear latches

ZV_LATCHSETSIZE(1, HMI_CONTROLSIZEX(14, 1), HMI_CONTROLSIZEY(14,1)) 'Sets the latch size

ZV_LATCH(grabImg, 1) 'Displays the image on latch.'

SET_COLOR(RGB(0,255,0))

'Image roi to control roi'

is_redraw = 0

TABLE(11, d_meas_param1(0), d_meas_param1(1))

ZV_POSFROMIMG(1, 1, 11, 11) 'Converts image coordinates to HMI control coordinates'

TABLE(13) = ZV_LENFROMIMG(1, d_meas_param1(2))

TABLE(14) = ZV_LENFROMIMG(1, d_meas_param1(3))

TABLE(15) = d_meas_param1(4)

HMI_SHOWWINDOW(14)

END SUB

8. Add functions to the draw.bas file to update the ROI position based on mouse operations and to draw the ROI in real time.

Update Roi1's position based on mouse actions

GLOBAL SUB update_roi1()

if mouse_scan(21) = 1 then '

Scan Press Operation

hit_pos = ZV_HMIADJRECT2(table(21), table(22), 11, -1) 'The hit position can only be changed when pressed.'

?TABLE(21),TABLE(22)

is_redraw = 1

endif

if mouse_scan(21) = -1 then '

Scan release operation

if TABLE(21)<(table(11)-table(13) or="">(TABLE(11)+TABLE(13)/2) or TABLE(22)<(table(12)-table(14) or="">(TABLE(12)+TABLE(14)/2) then

hit_pos=-1

endif

ZV_HMIADJRECT2(table(21), table(22), 11, hit_pos)

?*TABLE(11,4)

is_redraw = 1

endif

if (MOUSE_state(21)) then

ZV_HMIADJRECT2(table(21), table(22), 11, hit_pos)

is_redraw = 1

endif

if (1 = is_redraw) then

is_redraw = 0

ZV_POSTOIMG(1, 1, 11, 31)

d_meas_param1(0) = TABLE(31)

d_meas_param1(1) = TABLE(32)

d_meas_param1(2) = ZV_LENTOIMG(1, TABLE(13))

d_meas_param1(3) = ZV_LENTOIMG(1, TABLE(14))

d_meas_param1(4) = TABLE(15)

If a correction source is selected, save the ROI benchmark.

if(TABLE(110) = 1) then

set_base_roi()

endif

SET_REDRAW

endif

END SUB

'Set the baseline roi for the measurement area'

GLoBAL SUB set_base_roi()

d_meas_base_v1(0) = d_meas_param1(0)

d_meas_base_v1(1) = d_meas_param1(1)

d_meas_base_v1(2) = d_meas_param1(4)

d_meas_base_v2(0) = d_meas_param2(0)

d_meas_base_v2(1) = d_meas_param2(1)

d_meas_base_v2(2) = d_meas_param2(4)

END SUB

'Draw graphics in real time after updating Roi1 position'

GLOBAL SUB draw_roi1()

SET_COLOR(C_BLUE)

TABLE(16, d_meas_param1(6), d_meas_param1(7)) 'Sets the number and width of sub-regions.

ZV_HMIRECT2(11, 300)

DRAWLINE(TABLE(300), TABLE(301), TABLE(302), TABLE(303)) 'Outer rectangle

DRAWLINE(TABLE(302), TABLE(303), TABLE(304), TABLE(305))

DRAWLINE(TABLE(304), TABLE(305), TABLE(306), TABLE(307))

DRAWLINE(TABLE(306), TABLE(307), TABLE(300), TABLE(301))

DRAWLINE(TABLE(308), TABLE(309), TABLE(310), TABLE(311)) 'Direction arrow'

DRAWLINE(TABLE(312), TABLE(313), TABLE(310), TABLE(311))

DRAWLINE(TABLE(314), TABLE(315), TABLE(310), TABLE(311))

if (0 = TABLE(316)) then return

SET_COLOR(C_GREEN)

DIM idx

for idx = 0 to TABLE(316)-1

DRAWLINE(TABLE(317+idx*4), TABLE(318+idx*4), TABLE(319+idx*4), TABLE(320+idx*4))

next

END SUB

9. Add a function to the draw.bas file that responds when the "OK" button is pressed in the "Set_Roi1" window and associate it with an action function name.

'Set the function to respond when the OK button is pressed in the Roi1 window

GLOBAL SUB btn_meas_confirm1()

HMI_CLOSEWINDOW(14)

END SUB

10. Create a new “Set_Roi2” window that pops up when you press the “Select Measurement Area 2” button in the “Set_Select” window interface. This window is used to create a straight line measuring tool for the second dimension edge and set the parameters for detecting the straight line.

11. Add a function to the draw.bas file that responds when the "Select Measurement Area 2" button is pressed in the "Set_Select" window and associate it with an action function name.

The function that responds when the button to select measurement area 2 is pressed.

GLOBAL SUB btn_sel_roi2()

ZV_LATCHCLEAR(1) 'Clear latches

ZV_LATCHSETSIZE(1, HMI_CONTROLSIZEX(15, 1), HMI_CONTROLSIZEY(15,1)) 'Sets the size of the latch.

ZV_LATCH(grabImg, 1) 'Displays the image on latch.'

SET_COLOR(RGB(0,255,0))

'Image ROI coordinates to ROI control'

is_redraw = 0

TABLE(11, d_meas_param2(0), d_meas_param2(1))

ZV_POSFROMIMG(1, 1, 11, 11) 'Converts image coordinates to HMI control coordinates'

TABLE(13) = ZV_LENFROMIMG(1, d_meas_param2(2))

TABLE(14) = ZV_LENFROMIMG(1, d_meas_param2(3))

TABLE(15) = d_meas_param2(4)

HMI_SHOWWINDOW(15)

END SUB

12. Add functions to the draw.bas file to update the ROI position of Roi2 based on mouse operations and to draw Roi1 in real time.

Update Roi2's position based on mouse actions

GLOBAL SUB update_roi2()

if mouse_scan(21) = 1 then 'Scan pressed operations'

hit_pos = ZV_HMIADJRECT2(table(21), table(22), 11, -1) 'The hit position can only be changed when pressed.'

is_redraw = 1

endif

if mouse_scan(21) = -1 then 'Scan release operation'

ZV_HMIADJRECT2(table(21), table(22), 11, hit_pos)

is_redraw = 1

endif

if (MOUSE_state(21)) then

ZV_HMIADJRECT2(table(21), table(22), 11, hit_pos)

is_redraw = 1

endif

if (1 = is_redraw) then

is_redraw = 0

ZV_POSTOIMG(1, 1, 11, 31)

d_meas_param2(0) = TABLE(31)

d_meas_param2(1) = TABLE(32)

d_meas_param2(2) = ZV_LENTOIMG(1, TABLE(13))

d_meas_param2(3) = ZV_LENTOIMG(1, TABLE(14))

d_meas_param2(4) = TABLE(15)

If you select a correction source, save the ROI reference position.

if(TABLE(110) = 1) then

set_base_roi()

endif

SET_REDRAW

endif

END SUB

'Draw graphics in real time after updating Roi2 position'

GLOBAL SUB draw_roi2()

SET_COLOR(C_BLUE)

TABLE(16, d_meas_param2(6), d_meas_param2(7)) 'Sets the number and width of sub-regions.

ZV_HMIRECT2(11, 300) 'Calculates the corresponding vertices and arrow anchors of the rectangle roi of the control.

DRAWLINE(TABLE(300), TABLE(301), TABLE(302), TABLE(303)) 'Outer rectangle

DRAWLINE(TABLE(302), TABLE(303), TABLE(304), TABLE(305))

DRAWLINE(TABLE(304), TABLE(305), TABLE(306), TABLE(307))

DRAWLINE(TABLE(306), TABLE(307), TABLE(300), TABLE(301))

DRAWLINE(TABLE(308), TABLE(309), TABLE(310), TABLE(311)) 'Direction arrow'

DRAWLINE(TABLE(312), TABLE(313), TABLE(310), TABLE(311))

DRAWLINE(TABLE(314), TABLE(315), TABLE(310), TABLE(311))

if (0 = TABLE(316)) then return

SET_COLOR(C_GREEN)

DIM idx

for idx = 0 to TABLE(316)-1

DRAWLINE(TABLE(317+idx*4), TABLE(318+idx*4), TABLE(319+idx*4), TABLE(320+idx*4))

next

END SUB

13. Add a function to the draw.bas file that responds when the "OK" button is pressed in the "Set_Roi2" window and associate it with an action function name.

'Set the function to respond when the OK button is pressed in the Roi2 window'

GLOBAL SUB btn_meas_confirm2()

HMI_CLOSEWINDOW(15)

END SUB

14. Create a new file named meas.bas to store measurement-related functions such as distance calculation function and calibration function for two straight lines, and add the implementation functions to the file.

'Calculate the distance and width between two straight lines'

global sub length1()

LOCAL dist,dist1,dist2

local dis_tmp,dst_tmp1

'Calculate the distance between the starting point of line 2 and line 1.'

dist = ZV_DISTPL(d_meas_rst2(0),d_meas_rst2(1),d_meas_rst1(0),d_meas_rst1(1),d_meas_rst1(2),d_meas_rst1(3))

'Calculate the distance between the endpoint of line 2 and line 1'

dist1 = ZV_DISTPL(d_meas_rst2(2),d_meas_rst2(3),d_meas_rst1(0),d_meas_rst1(1),d_meas_rst1(2),d_meas_rst1(3))

'Calculate the coordinates of the midpoint of line 2'

dis_tmp=(d_meas_rst2(0)+d_meas_rst2(2))/2

dst_tmp1=(d_meas_rst2(1)+d_meas_rst2(3))/2

'Calculate the distance between the midpoint of line 2 and line 1.'

dist2 = ZV_DISTPL(dis_tmp,dst_tmp1,d_meas_rst1(0),d_meas_rst1(1),d_meas_rst1(2),d_meas_rst1(3))

TABLE(500) = (dist + dist1 + dist2) / 3 'Distance between two lines

PixLength=TABLE(500)

TABLE(500)

end sub

'Calculate the actual size value'

global sub length2()

Actual size = pixel size * pixel ratio

TABLE(501)=TABLE(500)*CalibParam

end sub

'Calculate the pixel ratio, i.e., actual value / pixel value'

global subcalculation()

CalibParam=WorldLength/TABLE(500)

end sub

15. Add a function to the main.bas file that responds when the "Test" button is pressed in the "Set_Select" window interface and associate it with an action function name.

'The function that responds when the test button is pressed in the measurement settings interface'

GLOBAL SUB btn_mea_test()

When selecting a correction source, perform localization detection first.

if(TABLE(110) = 1) then

btn_loc_test()

endif

ZVOBJECT mr1,mr2,mr3,mr4,rst1,rst2,rst3,rst4,colorImg

ZVOBJECT contlist, tsContlist, mat_rigid

LOCAL show_rst

'Measurement area ROI correction'

if(TABLE(110) = 1 AND d_is_creModel = 1) then 'If the correction source is already enabled and the template has been created.'

'Calculate the rigid transformation matrix'

ZV_GETRIGIDVECTOR(mat_rigid, d_match_base_rst(1), d_match_base_rst(2), d_match_base_rst(3), d_match_rst(1), d_match_rst(2), d_match_rst(3))

The input Roi1 reference vector is corrected using the transformation matrix mat_rigid, and the corrected vector is stored in a TABLE starting at index 0.

ZV_VECTORCORRECT(mat_rigid, d_meas_base_v1(0), d_meas_base_v1(1),d_meas_base_v1(2), 0)

d_meas_param1(0) = TABLE(0)

d_meas_param1(1) = TABLE(1)

d_meas_param1(4) = TABLE(2)

The input Roi2 reference vector is corrected using the transformation matrix mat_rigid, and the corrected vector is stored in a TABLE starting at index 0.

ZV_VECTORCORRECT(mat_rigid, d_meas_base_v2(0), d_meas_base_v2(1),d_meas_base_v2(2), 0)

d_meas_param2(0) = TABLE(0)

d_meas_param2(1) = TABLE(1)

d_meas_param2(4) = TABLE(2)

endif

Measurement Area 1

'Generate the rotation area measured by line 1'

ZV_MRGENLINE(mr1, d_meas_param1(0), d_meas_param1(1), d_meas_param1(2), d_meas_param1(3), d_meas_param1(4), 1, d_meas_param1(6), d_meas_param1(7))

Set the detection parameters for line 1, including filter size, threshold, edge polarity, and edge position.

ZV_MRSETADV(mr1, d_meas_param1(8), d_meas_param1(9), d_meas_param1(10), d_meas_param1(11))

Measurement Area 2

'Generate the rotation area measured by line 2'

ZV_MRGENLINE(mr2, d_meas_param2(0), d_meas_param2(1), d_meas_param2(2), d_meas_param2(3), d_meas_param2(4), 1, d_meas_param2(6), d_meas_param2(7))

Set the detection parameters for line 2, including filter size, threshold, edge polarity, and edge position.

ZV_MRSETADV(mr2, d_meas_param2(8), d_meas_param2(9), d_meas_param2(10), d_meas_param2(11))

Store the endpoint of target line 1 measured in the rectangular measurement area into a TABLE with a starting index of 61.

ZV_MRLINE(mr1, grabImg, rst1, 61)

d_meas_rst1(0) = TABLE(61)

d_meas_rst1(1) = TABLE(62)

d_meas_rst1(2) = TABLE(63)

d_meas_rst1(3) = TABLE(64)

Store the endpoints of the target line 2 measured in the rectangular measurement area into a TABLE with a starting index of 71.

ZV_MRLINE(mr2, grabImg, rst2, 71)

ZV_MATINFO(rst2, 0) 'table(0) is used as a temporary variable'

d_meas_rst2(0) = TABLE(71)

d_meas_rst2(1) = TABLE(72)

d_meas_rst2(2) = TABLE(73)

d_meas_rst2(3) = TABLE(74)

'Calculate the distance and width between two straight lines'

length1()

d_show_rst=TABLE(500)

if (CalibParam > 0) then

'Calculate the actual length'

length2()

d_show_rst=TABLE(501)

endif

'Draw the result line

ZV_GRAYTORGB(grabImg, colorImg)

ZV_LINE(colorImg, TABLE(61), TABLE(62), TABLE(63), TABLE(64), C_GREEN)

ZV_LINE(colorImg, TABLE(65), TABLE(66), TABLE(67), TABLE(68), C_GREEN)

ZV_LINE(colorImg, TABLE(71), TABLE(72), TABLE(73), TABLE(74), C_GREEN)

ZV_LINE(colorImg, TABLE(75), TABLE(76), TABLE(77), TABLE(78), C_GREEN)

ZV_LATCH(colorImg,0)

END SUB

Add a simulation of the effect when the "Test" button is pressed in the "Set_Select" window interface to the main.bas file.

Test button screenshot

16. Set the action function name associated with the 【Calculation】 button in the “Set_Select” window interface.

The simulation result when the [Calculate] button is pressed in the "Set_Select" window.

Screenshot of the calculation button

17. Associate UI control variables in the “Set_Select” window.

18. Add a function to the main.bas file that responds when the "Set_Select" window interface presses the "Return to Main Interface" button, and associate it with the action function name.

The function that responds when the "Return to Main Interface" button is pressed.

GLOBAL SUB Goto_Main()

HMI_CLOSEWINDOW(13)

END SUB

19. Modify the function that responds when the "Single Execution" button is pressed on the main interface in the main.bas file and associate it with the action function name.

'Function that responds when the execute button is pressed once on the main interface'

GLOBAL SUB btn_test()

'Capture a frame of image'

btn_grab

'Execute measurement test section code'

btn_mea_test()

END SUB

The simulated effect when the "Single Execution" button is pressed on the main interface.

20. Modify the content of the task function executed in the function that responds when the "Continuous Execution" button is pressed on the main.bas file.

main_task:

while(1)

if (0 = run_switch) then

exit while

endif

The following location operations will be performed.

btn_grab()

btn_mea_test()

wend

END

The simulation effect is shown when the "Continuous Execution" button is pressed on the main interface.

Figure 1 shows the effect of continuous operation.

Continuous operation effect diagram 2

The corresponding size can be detected when placed in different positions and at different angles.

This concludes our quick introduction to the VPLC series machine vision motion control all-in-one machine from Zheng Motion Technology (Part 5) – Dimensional Measurement. For more exciting content, please follow the " Zheng Motion Assistant " WeChat official account.

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.

Read next

CATDOLL 138CM Tami TPE

Height: 138cm Weight: 26kg Shoulder Width: 30cm Bust/Waist/Hip: 65/61/76cm Oral Depth: 3-5cm Vaginal Depth: 3-15cm Anal...

Articles 2026-02-22