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 4) - BLOB presence or absence detection.
Video Tutorial: Quick Start Guide to the VPLC Series Machine Vision Motion Control All-in-One Machine (Part 4)
In the last lesson, we discussed shape-matching-based visual positioning, a commonly used feature in machine vision solutions. Through this lesson, we learned how to implement shape matching using ZDevelop software.
In this course, we will share another detection function of machine vision with you---using BLOB to detect the presence or absence of products.
A BLOB, also known as a blob, refers to a connected bright area on a dark background or a connected dark area on a bright background in a binarized image.
BLOB detection uses morphological methods (such as binarization, dilation, erosion, etc.) to convert grayscale images into binary images. During the conversion, the detection features are processed into BLOB spots and the interfering factors are processed into the image background, so that the detection features can be accurately processed.
1. Relying on morphological processing
The detection image needs to be converted into a binary image, and morphological processing should be used to highlight the detection features.
2. Ignoring shape features
It can detect any connected area, regardless of the shape of the product.
3. Suitable for high contrast products
BLOB detection requires good contrast between the detected features and the background; otherwise, the features and background cannot be distinguished.
4. Fast execution speed
Searching for BLOB spots after converting to a binary image does not take much time.
Morphological processing refers to the manipulation of local pixels in an image to extract local features and details of interest during the detection process. Commonly used morphological processing methods include binarization, dilation, erosion, and hole filling.
1 Binarization
Convert an 8-bit grayscale image (grayscale value 0~255) into a binary image (an image composed of pure black and pure white values) that is either 0 or 1.
2. Fill the holes
In a binarized image, filling in small areas of black or white spots can filter out noise.
3. Expansion
The white areas in the binarized image are enlarged, the black areas are reduced, and the interference of small black spots is removed.
4. Corrosion
Enlarging the black areas and shrinking the white areas in a binarized image can remove interference from small white spots.
BLOB testing flowchart
// Example Demonstration //
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 auto-run tasks → Create a new detectParam.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 take a table (the system's data structure) as a parameter.
Here, tables are used as intermediate variables.
Table 21-22, Mouse Buttons, Control Coordinate System
Tables 31-35, ROI parameters for rotation moments: cx, cy, width, height, angle, control coordinate system.
Tables 41-45 show the image coordinates after the coordinate transformation of the rotated rectangle control, and the image coordinate system.
Table 51-56, ROI parameters for the torus: cx, cy, radius r of the torus centerline, half-width ann_R of the torus, starting angle stAngle, angle range entAngle, control coordinate system.
Tables 61-66 show the image coordinates corresponding to the circular control after coordinate transformation, and the image coordinate system.
'Main task status'
'0 - Uninitialized
'1 - Stop
'2 - Running
'3 - Stopping
GLOBAL DIM main_task_state
main_task_state = 1
'Data Acquisition Switch'
'0 - Stop collecting data
'1 - Request to collect
GLOBAL DIM grab_switch
grab_switch = 0
Number of cameras
GLOBAL cam_num
cam_num = 0
Camera types, zmotion; mvision; basler; mindvision; huaray
GLOBAL DIM CAMERA_TYPE(16)
CAMERA_TYPE = "zmotion"
Define the main task ID - 10
GLOBAL DIM main_task_id
main_task_id = 10
Define the continuous data collection task ID - 9
GLOBAL DIM grab_task_id
grab_task_id = 9
Define global image variables
GLOBAL ZVOBJECT grabImg 'Capture Image'
GLOBAL ZVOBJECT binImg 'Binarized image'
GLOBAL ZVOBJECT disImg 'Display image'
Error message
GLOBAL DIM error_msg(256)
Define commonly used color variables
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)
GLOBAL DIM d_roi_arc_flag 'Defines ROI type flags: 0-rectangle, 1-circle
GLOBAL DIM d_rlt_area 'Defines the BLOB area result
GLOBAL DIM d_rlt_state 'Defines the state result'
'Rotated Rectangle ROI Parameters: cx, cy, width, height, angle'
GLOBAL DIM d_roi_rect2(5) 'The 'd' prefix indicates a data structure
'Circular ROI parameters: cx, cy, radius of the annulus centerline r, half-width of the annulus ann_R, starting angle stAngle, angle range entAngle'
GLOBAL DIM d_roi_arc(6) 'The 'd' prefix indicates a data structure
Detection parameters: Threshold mode (automatic or manual threshold), low threshold, high threshold, polarity (black or white), minimum area, maximum area, inverse (i.e., invert the result, success becomes failure, failure becomes success).
GLOBAL DIM d_detect_param(7) 'The 'd' prefix indicates a data structure
'Open/Close Operation Parameters'
GLOBal DIM d_deal_value(2)
The detection results are, in order, state result and pixel area.
GLOBAL DIM d_detect_rst(2)
'Display the printed characters
GLOBAL ShowString(64)
'Run HMI file
RUN "Hmi.hmi",1
4
Initialize the measurement parameters in the detectParam.bas file.
end
GLOBAL SUB init_detect_param() 'Initialize measurement parameters
d_roi_arc_flag = 0 'Default is rectangle
d_rlt_area = 0
d_rlt_state = 0
d_deal_value(0)=1
d_deal_value(1)=1
'Initialize roi parameters
d_roi_rect2(0) = 320.0 'roi center x
d_roi_rect2(1) = 240.0 'roi center y
d_roi_rect2(2) = 160.0 'roi width'
d_roi_rect2(3) = 120.0 'roi height'
d_roi_rect2(4) = 0.0 'roi angle'
TABLE(31) = d_roi_rect2(0)
TABLE(32) = d_roi_rect2(1)
TABLE(33) = d_roi_rect2(2)
TABLE(34) = d_roi_rect2(3)
TABLE(35) = d_roi_rect2(4)
d_roi_arc(0) = 320.0 'roi center x
d_roi_arc(1) = 240.0 'roi center y
d_roi_arc(2) = 60.0 'Radius of the annulus's centerline
d_roi_arc(3) = 20.0 'Ring half-width'
d_roi_arc(4) = 0.0 'Starting angle
d_roi_arc(5) = 360.0 'Termination angle
TABLE(51) = d_roi_arc(0)
TABLE(52) = d_roi_arc(1)
TABLE(53) = d_roi_arc(2)
TABLE(54) = d_roi_arc(3)
TABLE(55) = d_roi_arc(4)
TABLE(56) = d_roi_arc(5)
Initialize detection parameters: Threshold mode (automatic threshold = 1 or manual threshold = 0), low threshold, high threshold, polarity (black or white), maximum, minimum, inverse (i.e., invert the result, success becomes failure, failure becomes success).
d_detect_param(0) = 0 'Manual threshold'
d_detect_param(1) = 128 'Low threshold'
d_detect_param(2) = 255 'High threshold'
d_detect_param(3) = 1 'Polar white, i.e., detect the area of white pixels.
d_detect_param(4) = 60000 'Minimum area, number of pixels
d_detect_param(5) = 90000 'Maximum area
d_detect_param(6) = 0 'Results are not reversed'
END SUB
5
Associate HMI interface value display control variables.
6
Add an HMI interface initialization function to the main.bas file and associate the initialization function in the HMI system settings.
HMI interface initialization function
GLOBAL SUB hmi_init()
grab_switch = 0
main_task_state = 1
ZV_RESETCLIPSIZE(1280, 1024) 'Sets the cropping size of the region based on the image resolution during initialization; here, the image resolution is 1280x1024.
ZV_LATCHSETSIZE(0, HMI_CONTROLSIZEX(10, 7), HMI_CONTROLSIZEY(10, 7)) 'Sets the latch size
init_detect_param() 'Initialize measurement parameters
ZV_SETSYSDBL("CamGetTimeout", 1000) 'Sets the data acquisition timeout
ZV_LATCHCLEAR(0)
ZV_LATCH(grabImg, 0)
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.
Camera operation related buttons
Preliminary lessons on specific function implementation:
VPLC Series Machine Vision Motion Control All-in-One Machine Quick Start Guide (Part 3): Shape Matching-Based Visual Positioning
VPLC Series Machine Vision Motion Control All-in-One Quick Start Guide (Part 2): Basic Camera Usage
VPLC Series Machine Vision Motion Control All-in-One Machine Quick Start Guide (Part 1): Hardware and Software Introduction and Counting Examples
The operation has already been demonstrated, so it will not be repeated here.
8
Add a function to detect the ROI and update the drawing in the draw.bas file, and associate the refresh function and the drawing function in the custom component properties window.
end
The functions for refreshing and drawing the interface related to drawing (i.e., selecting ROI) are placed in this .bas file.
DIM is_redraw
is_redraw = 0
DIM hit_pos
'Update ROI location region function based on mouse operation'
GLOBAL SUB update_roi()
if d_roi_arc_flag = 0 then
if mouse_scan(21) = 1 then 'Scan pressed operations'
hit_pos = ZV_HMIADJRECT2(table(21), table(22), 31, -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), 31, hit_pos)
is_redraw = 1
endif
if (MOUSE_state(21)) then
ZV_HMIADJRECT2(table(21), table(22), 31, hit_pos)
is_redraw = 1
endif
if (1 = is_redraw) then
is_redraw = 0
ZV_POSTOIMG(0, 1, 31, 41)
d_roi_rect2(0) = TABLE(41)
d_roi_rect2(1) = TABLE(42)
d_roi_rect2(2) = ZV_LENTOIMG(0, TABLE(33))
d_roi_rect2(3) = ZV_LENTOIMG(0, TABLE(34))
d_roi_rect2(4) = TABLE(35)
SET_REDRAW
endif
else
is_redraw = 0
if mouse_scan(21) = 1 then 'Scan pressed operations'
hit_pos = ZV_HMIADJARC(table(21), table(22), 51, -1) 'The hit position can only be changed when pressed.'
is_redraw = 1
endif
if mouse_scan(21) = -1 then 'Scan release operation'
ZV_HMIADJARC(table(21), table(22), 51, hit_pos)
is_redraw = 1
endif
if (MOUSE_state(21)) then
ZV_HMIADJARC(table(21), table(22), 51, hit_pos)
is_redraw = 1
endif
if (1 = is_redraw) then
is_redraw = 0
'Control coordinates to image coordinates'
ZV_POSTOIMG(0, 1, 51, 61)
TABLE(63) = ZV_LENTOIMG(0, TABLE(53))
TABLE(64) = ZV_LENTOIMG(0, TABLE(54))
TABLE(65, TABLE(55), TABLE(56))
d_roi_arc(0) = TABLE(61)
d_roi_arc(1) = TABLE(62)
d_roi_arc(2) = TABLE(63)
d_roi_arc(3) = TABLE(64)
d_roi_arc(4) = TABLE(65)
d_roi_arc(5) = TABLE(66)
SET_REDRAW
endif
end if
END SUB
'Real-time drawing of ROI region after update'
GLOBAL SUB draw_roi()
if d_roi_arc_flag = 0 then
SET_COLOR(C_BLUE)
ZV_HMIRECT2(31, 300) 'Decomposes the rotated rectangle ROI into HMI-supported drawing primitives and adds control parameters to facilitate HMI drawing display.
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))
else
SET_COLOR(C_BLUE)
TABLE(57) = 1 'Number of sub-regions'
TABLE(58) = 5 'Sub-region width
ZV_HMIARC(51, 400) 'Draw a torus
'Draw an arc'
DRAWARC(TABLE(400), TABLE(401), TABLE(402), TABLE(404), TABLE(405)) 'Inner radius
DRAWARC(TABLE(400), TABLE(401), TABLE(403), TABLE(404), TABLE(405)) 'Outer radius
Draw boundary lines
DIM idx
for idx = 0 to TABLE(406)-1
DRAWLINE(TABLE(407+idx*4), TABLE(408+idx*4), TABLE(409+idx*4), TABLE(410+idx*4))
next
endif
END SUB
↓
9
Add a function to the main.bas file that responds when the "Test" button is pressed on the HMI interface, and associate it with an action function name.
↓
'Function that responds when the test button is pressed on the HMI interface'
GLOBAL SUB btn_test()
ZVOBJECT regionWhite, regionMask, regionBlack,re_connecte
'Generate ROI region'
if d_roi_arc_flag = 0 then
ZV_REGENRECT2(regionMask, d_roi_rect2(0), d_roi_rect2(1), d_roi_rect2(2), d_roi_rect2(3), d_roi_rect2(4))
else
ZV_REGENANNULAR(regionMask, d_roi_arc(0), d_roi_arc(1), d_roi_arc(2) - d_roi_arc(3), d_roi_arc(2) + d_roi_arc(3))
endif
Binarization
If d_detect_param(0) = 0, then 'Manual threshold processing'
ZV_RETHRESH(grabImg, regionMask, regionWhite, d_detect_param(1), d_detect_param(2))
ZV_REOPENING(regionWhite,regionWhite,d_deal_value(0),d_deal_value(0))
ZV_RECLOSING(regionWhite,regionWhite,d_deal_value(1),d_deal_value(1))
else
Dim autoThresh 'Automatic Threshold Processing'
ZV_REAUTOTHRESH(grabImg, regionMask, regionWhite, 500)
ZV_REOPENING(regionWhite,regionWhite,d_deal_value(0),d_deal_value(0))
ZV_RECLOSING(regionWhite,regionWhite,d_deal_value(1),d_deal_value(1))
autoThresh = TABLE(500)
'? "autoThresh = " autoThresh
endif
'Calculate the area of a BLOB'
If d_detect_param(3) = 1 then 'Polarity is white'
ZV_REAREA(regionWhite, 500) 'Calculates the area of the bright region White.
d_rlt_area = TABLE(500) 'Assign the obtained pixel area to the d_rlt_area variable.
ZV_RECONNECT(regionWhite,re_connecte) 'Calculates the connected components of the region White'
`zv_refilter(re_connecte,0,60000,90000,0)` filters the regions in the region list, keeping regions with an area between 60,000 and 90,000, and filtering out regions with an area outside this range.
ZV_LISTCOUNT(re_connecte,100) 'Gets the number of connected components in the list.
'Number of products' TABLE(100)
else 'Polarity is black'
ZV_REDIFF (regionMask, regionWhite, regionBlack)
ZV_REAREA(regionBlack, 500)
d_rlt_area = TABLE(500)
ZV_RECONNECT(regionBlack, re_connecte) 'Calculates the connected regions of the region'
`zv_refilter(re_connecte,0,60000,90000,0)` filters the regions in the region list, keeping regions with an area between 60,000 and 90,000, and filtering out regions with an area outside this range.
ZV_LISTCOUNT(re_connecte,100) 'Gets the number of connected components in the list.
'Number of products' TABLE(100)
endif
'Judgment of the results'
If d_rlt_area > d_detect_param(4) and d_rlt_area < d_detect_param(5), then 'When the area result is within the set upper and lower limits of the area.'
d_rlt_state = 1
else
d_rlt_state = 0
endif
'Determining whether it is reversed'
if d_detect_param(6) then
d_rlt_state = 1 - d_rlt_state
endif
'Draw the rendering'
Dim width, height
ZV_IMGINFO (grabImg, 500)
width = TABLE(500)
height = TABLE(501)
ZV_GRAYTORGB(grabImg,disImg)
ShowString=TOSTR(TABLE(100),1,0) 'Converts the number of BLOBs into a string variable.
ZV_TEXT(disImg,"Count of Parts: "ShowString,10,80,80,ZV_COLOR(0,0,0)) 'Displays the result text
ZV_REGION(disImg, regionMask, 0, ZV_COLOR(0,0,0)) 'Draws the black regionMask in disImg.
ZV_REGION(disImg, regionWhite, 0, ZV_COLOR(255,255,255)) 'Draws the white region "regionWhite" in disImg.
ZV_LATCH(disImg, 0)
end sub
↓
10
Add a function to respond to the "Run" button in the main.bas file and associate it with an action function.
↓
'Function that responds when the run button is pressed in the HMI interface'
GLOBAL SUB btn_run()
if (2 = main_task_state) then
"Continuous execution has been started. Please do not repeat the operation!"
return
endif
if (1 = main_task_state) then
if (0 = PROC_STATUS(main_task_id)) then
main_task_state = 2
RUNTASK main_task_id, main_task
endif
endif
end sub
main_task:
while(1)
if (3 = main_task_state) then
main_task_state = 1
exit while
endif
'Execute acquisition and detection functions'
CAM_SETPARAM("TriggerSoftware", 0)
CAM_GET(grabImg, 0)
btn_test()
wend
END
↓
11
Add a function to respond to the "Stop" button in the main.bas file and associate it with an action function.
↓
'Function to respond when the stop button is pressed on the HMI interface'
GLOBAL SUB btn_stop()
if (2 = main_task_state) then
main_task_state = 3
endif
end sub
↓
This concludes our quick introduction to the VPLC series machine vision motion control all-in-one machine from Zheng Motion Technology (Part 4) – BLOB detection. 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.