No free Lunch - Computer Vision 4

No free Lunch - Computer Vision 4

We will be learning one by one the followings tasks with relevant to the computer vision using open-cv;

  1. How to detect objects?
  2. How to track objects?
  3. How to do semantic segmentation of objects?
  4. How to do instance segmentation of objects?

Object Detection Technique using Open-CV:

Object Detection using HAAR cascades

Haar cascades are faster and this method is one of the brilliant ways to detect objects.

Object Detection is a computer technology related to computer vision, image processing, and deep learning that deals with detecting instances of objects in images and videos.

We will do object detection in this article using something known as?haar cascades.

Detecting objects with Haar Cascade classifiers is a good idea.

Paul Viola and Michael Jones developed this approach in their work "Rapid Object Detection Using a Boosted Cascade of Simple Features ".

Haar Cascade is a machine learning-based strategy that involves training the classifier using a large number of positive and negative pictures. Positive pictures - These photos include the images that our classifier is supposed to recognise. Negative Images - Images of everything else that isn't the object we're looking for.

Here you refer to how to get the custom Haar cascade file (xml) for detecting your own objects.

You can download the GUI here .

Once you have the Haar cascade file, It is very simple to do the detection.

import numpy as np
import cv2

custom_cascade = cv2.CascadeClassifier('customhaarcascade_frontalface_default.xml')

cap = cv2.VideoCapture(0)

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    objects = custom_cascade.detectMultiScale(gray, 1.3, 5)
    
    for (x,y,w,h) in objects:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]


    cv2.imshow('img',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()        

Object detection using Contour segmentation.

At a high level, here is the 5-step process for contour detection in OpenCV:

  1. Read a colour image
  2. Convert the image to grayscale
  3. Convert the image to binary (i.e. black and white only) using Otsu’s method or a fixed threshold that you choose.
  4. If the objects in the image are black, and the background is white, we need to invert the image so that white pixels become?
  5. black and black pixels become white. Otherwise, we leave the image as-is.
  6. Detect the contours


import cv2

cap = cv2.VideoCapture(0)


while True:
? ? _, frame = cap.read()
? ? # convert to grayscale
? ? gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
? ? # create a binary thresholded image
? ? _, binary = cv2.threshold(gray, 255 // 2, 255, cv2.THRESH_BINARY_INV)
? ? # find the contours from the thresholded image
? ? contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
? ? # draw all contours
? ? image = cv2.drawContours(frame, contours, -1, (0, 255, 0), 2)
? ? # show the images
? ? cv2.imshow("gray", gray)
? ? cv2.imshow("image", image)
? ? cv2.imshow("binary", binary)
? ? if cv2.waitKey(1) == ord("q"):
? ? ? ? break


cap.release()
cv2.destroyAllWindows()        

This is about the third argument in cv.findContours function. What does it denote actually?

Above, we told that contours are the boundaries of a shape with the same intensity. It stores the (x,y) coordinates of the boundary of a shape. But does it store all the coordinates? That is specified by this contour approximation method.

If you pass cv.CHAIN_APPROX_NONE, all the boundary points are stored. But actually, do we need all the points? For eg, you found the contour of a straight line. Do you need all the points on the line to represent that line? No, we need just two endpoints of that line. This is what cv.CHAIN_APPROX_SIMPLE does. It removes all redundant points and compresses the contour, thereby saving memory.

The above code snipper will detect the pixels which are background in black and foreground in white after converting it into the black and white image, then detecting all the cells that have been a non-black pixel. This way we can extract all the images or images of our interest by applying a threshold.

No alt text provided for this image

Otsu's thresholding is a pretty simple way we could segment the objects by applying a threshold to separate the region of interest(ROI) from the whole image.

Here you can read further about the thresholding technique.

Tomorrow we will see the object tracking. Hope you have been enjoying learning the opencv along the way this whole week. If you have any queries, please reach out to the author.

The author has taken extreme care and effort on testing sufficiently the accuracy of the content of this post. The readers can experiment with them carefully at their own risk. For any queries, please contact [email protected].

要查看或添加评论,请登录

社区洞察

其他会员也浏览了