As we already discussed that the most important thing in morphological image processing is the Structuring element. This is used to probe an image for finding the region of interest. Different shapes and sizes of SE will produce a different result. Thus it becomes vital to have a good grasp on this for better understanding of morphological image processing. In this blog, let’s create trackbars which makes it really easy to visualize the result for different values. So, let’s get started.
Steps:
- Load the image and create a window to attach trackbars
- Specify the morphological operations and Structuring elements
- Create the trackbars and the callback function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import cv2 import numpy as np # Load the image src = cv2.imread('D:/downloads/g1.png') # Create a window to attach trackbars title_window = 'Morphology Operations' cv2.namedWindow(title_window) # Specify the morphological operations and Structuring elements morph_op_dic = {0: 'ERODE', 1: 'DILATE', 2: 'OPEN', 3: 'CLOSE', \ 4: 'GRADIENT', 5: 'TOPHAT', 6: 'BLACKHAT', 7: 'HITMISS'} element_dic = {0: 'RECT', 1: 'CROSS', 2: 'ELLIPSE'} # Define callback function def morphology_operations(val): # get current trackbar position morph_operator = cv2.getTrackbarPos('Operator', title_window) kernel_size = cv2.getTrackbarPos('kernel', title_window) val_type = cv2.getTrackbarPos('SE', title_window) iter_num = cv2.getTrackbarPos('iterations', title_window) # create structuring element element = cv2.getStructuringElement(val_type, (2*kernel_size + 1, 2*kernel_size+1)) # apply morphological operation dst = cv2.morphologyEx(src, morph_operator, element,iterations=iter_num) # Display the operation and structuring element font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(dst,'Op:{}, SE: {}'.format(morph_op_dic[morph_operator],element_dic[val_type]),(10,40), font, 0.5,(0,255,255),1,cv2.LINE_AA) cv2.imshow(title_window, dst) # Create Trackbars cv2.createTrackbar('Operator', title_window , 0, 7, morphology_operations) cv2.createTrackbar('SE', title_window , 0, 2, morphology_operations) cv2.createTrackbar('kernel', title_window , 0, 10, morphology_operations) cv2.createTrackbar('iterations', title_window , 0, 10, morphology_operations) # Instantiate the function morphology_operations(0) cv2.waitKey(0) |
This will produce the following output
Play with the trackbars to get a feel about the morphological operations. That’s all for this blog. Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time