Opencv python object detection Tutorial (part 1)

OpenCV Python 
OpenCV Python hand Tracking Series(Part-1)
There are many reasons to detect object using camera which allows system to interact with the environment.
  • Detecting obstacles by self driving car(to avoid them of course:)
  • Can be used in gripping object with robotic arm
  • Servaliance Purpose
  • or may be your professor asked you to do it :(
we will use OpenCV python to detect human arm with basic image processing techniques. For this method we require OpenCV and numpy library. In this tutorial we will use color as feature and detect objects. For now we will detect a ball but shape does not matter as we are usingcolor as feature and not shape.
There are multiple ways to perform the Task such as
  • Color Feature
  • Background Subtraction
  • Shape Features
  • Sift Features
  • SURF Features
  • Deep learning

we will cover all the methods but for now we focus on color features. To start we take a good photo of object you want to Track like the image given below


Now let's Start Coding, First import numpy and OpenCV. It will be good to import matplotlib while working on images 




Then Read the image from the folder using 'cv2.imread' command and check the size of image using 'image.shape' command. This step is both useful and useless depending on the number of computation we want to make, if size of image is large and we want to work on video which increases computation the we would want to reduce computation by reducing size of image.




Now resize the image with same aspect ratio of image so that we can track the object in real image
convert image to gray scale using grayscale command and apply gaussian blur to remove noise in the image as shown below.




now check histogram of image and decide Threshold for binarization of image this is iterative task some times as value is to be set by humans. some time we need to use Erosion and then dilation steps after binarization which reduces a lot of effort as we can now sellect biggest white region which is our object instead on making it the only region.






We have to define a function to get center from binary image, I created a function called center_of_binary_image(binary) which takes binary image as input and gives us image with marked center we can also return the numerical value of center, now we get center of ball from binary image and we are good to go.
 
In this tutorial we have not used Erosion and dilation neither resize function as image is small but they are simple to implement and I will upload a code on the same in the link below. 

Comments

Popular posts from this blog

Background Subtraction OpenCv Python