Read time : 3 minutes , complexity : intermediate
We continue from our previous blog on contours and try to run the basic contour functions on a Python code. This program comes with the sample bundle and takes input a binary image which has a sketch of multiple faces and hence contours.
The image of the right is the input image and what your robot would see after contour processing is at left. As you have noticed that it can detect the curves in contours and then draw up the contours as explained. There is also a software panel in this code to increase the levels. You get all the contours at the +3 level. The code calls the function as below for findContours() as -
cv2.findContours( img.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
The code then uses the drawContours() taking input the levels users sets from track bar minus 3 to positive 3 as below -
vis = np.zeros((h, w, 3), np.uint8)
levels = levels - 3
cv2.drawContours( vis, contours, (-1, 2)[levels <= 0], (128,255,255),
3, cv2.LINE_AA, hierarchy, abs(levels) )
The code can be found under your installation folder of OpenCV2 as \Downloads\opencv\sources\samples\python
In case you are new to OpenCV2 please read this blog.
Hope this simple example lets you get the basics of contours and you may apply more aspects of gradients to read bar code, read hand written numbers or alphabets.
Please like, share and comment.
Read next how to make you robot identify circles here at -