Understanding of Contours in OpenCV

Contours in OpenCV represent the boundaries of objects in images and are a fundamental concept in computer vision for shape analysis, object detection, and recognition. Essentially, contours are continuous curves that outline the shape of an object by connecting points along its boundary. They are extracted through a series of image processing operations such as edge detection, thresholding, and morphological transformations. The importance of contours lies in their ability to provide valuable information about the structure, size, and orientation of objects within an image. By analyzing contours, it becomes possible to perform tasks like object detection, recognition, and measurement, which are essential in various applications. 

Example Program to Create a Bounding Box using OpenCV: 

import cv2 

import numpy as np 

print (cv2. __version__) 

 

def onTrack1(val): 

   global hueLow 

   hueLow = val 

   print (‘Hue Low’, hueLow) 

 

def onTrack2(val): 

   global hueHigh 

   hueHigh = val 

   print (‘Hue High’, hueHigh) 

 

def onTrack3(val): 

   global satLow 

   satLow = val 

   print (‘Sat Low’, satLow) 

 

def onTrack4(val): 

   global satHigh 

   satHigh = val 

   print (‘Sat High’, satHigh) 

 

def onTrack5(val): 

   global valLow 

   valLow = val 

   print (‘Val Low’, valLow) 

 

def onTrack6(val): 

   global valHigh 

   valHigh = val 

   print (‘Val High’, valHigh) 

 

def onTrack7(val): 

   global hueLow2 

   hueLow2 = val 

   print (‘Hue Low2’, hueLow2) 

 

def onTrack8(val): 

   global hueHigh2 

   hueHigh2 = val 

   print (‘Hue High2’, hueHigh2) 

 

width = 640 

height = 360 

 

cam = cv2.VideoCapture(0, cv2. CAP_DSHOW) 

cam.set(cv2.CAP_PROP_FRAME_WIDTH, width) 

cam.set(cv2.CAP_PROP_FRAME_HEIGHT, height) 

cam.set(cv2.CAP_PROP_FPS, 30

 

cv2.namedWindow(‘myTracker’

cv2.moveWindow(‘myTracker’, width, 0

 

cv2.createTrackbar(‘Hue Low’, ‘myTracker’, 10, 179, onTrack1) 

cv2.createTrackbar(‘Hue High’, ‘myTracker’, 20, 179, onTrack2) 

cv2.createTrackbar(‘Hue Low2’, ‘myTracker’, 10, 179, onTrack7) 

cv2.createTrackbar(‘Hue High2’, ‘myTracker’, 20, 179, onTrack8) 

cv2.createTrackbar(‘Sat Low’, ‘myTracker’, 20, 255, onTrack3) 

cv2.createTrackbar(‘Sat High’, ‘myTracker’, 250, 255, onTrack4) 

cv2.createTrackbar(‘Val Low’, ‘myTracker’, 10, 255, onTrack5) 

cv2.createTrackbar(‘Val High’, ‘myTracker’, 250, 255, onTrack6) 

 

while True: 

   ignore, frame = cam.read() 

   frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 

   lowerBound = np.array([hueLow, satLow, valLow]) 

   upperBound = np.array([hueHigh, satHigh, valHigh]) 

   lowerBound2 = np.array([hueLow2, satLow, valLow]) 

   upperBound2 = np.array([hueHigh2, satHigh, valHigh]) 

   

   myMask = cv2.inRange(frameHSV, lowerBound, upperBound) 

   myMask2 = cv2.inRange(frameHSV, lowerBound2, upperBound2) 

   myMaskComp = myMask | myMask2 

    

   contours,junk=cv2.findContours(myMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 

         

   for contour in contours: 

       area=cv2.contourArea(contour) 

       if area>=300

           x,y,w,h=cv2.boundingRect(contour) 

           cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255),2

    

   myObject = cv2.bitwise_and (frame, frame, mask=myMaskComp) 

   myObjectSmall = cv2.resize(myObject, (int(width/2), int(height/2))) 

   cv2.imshow(‘My Object’, myObjectSmall) 

   cv2.moveWindow(‘My Object’, int(width/2), int(height))  

    

   myMaskSmall = cv2.resize(myMask, (int(width/2), int(height/2))) 

   cv2.imshow(‘My Mask’, myMaskSmall) 

   cv2.moveWindow(‘My Mask’, 0, height) 

 

   myMaskSmall2 = cv2.resize(myMask2, (int(width/2), int(height/2))) 

   cv2.imshow(‘My Mask2’, myMaskSmall2) 

   cv2.moveWindow(‘My Mask2’, 0, height+int(height/2) +20

    

   cv2.imshow(‘my WEBcam’, frame) 

   cv2.moveWindow(‘my WEBcam’, 0, 0

   if cv2.waitKey(1) & 0xff == ord(‘q’): 

       break 

cam.release 

Leave a comment

Your email address will not be published. Required fields are marked *