Program of the Week 04 - Color

This has been a long overdue post. Thanks to my move and a lot of other things, despite trying my level best into putting out a post, I was not able to do so. This post would've possibly come in next week, but since you are reading this, it is coming out today. I just wanted to get one out so that I don't break my flow or more like I go back into the vicious cycle of okay maybe next week. With that said, this week again I'll be doing something with opencv. Not because I have planned it or anything, I simply had a small program ready.

When I first got into opencv, after going through the basic open image, open video phase, I wanted to do some very basic color based object detection. It did start with basic detection of red color objects, green color objects and so forth. Slowly I started modifying it to allow me to select which color I wanted to detect the intensity levels and such. It had a whole bunch of controls on it. That time, I used to do it in C++ and EmguCV which was basically a .Net wrapper for OpenCV, which allowed me to utilize windows forms in Visual Studio and make the entire thing more presentable. We are not doing that today. This post instead will be a very basic color based detection from a camera feed and programmed in python instead of C++.


Here is the code:

# Importing the required libraries
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
while(1):

    # Take each frame
    _, frame = cap.read()

    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of blue color in HSV
    lower_blue = np.array([98,50,50])
    upper_blue = np.array([112,255,255])

    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower_blue, upper_blue)

    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame,frame, mask= mask)

    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()
cap.release()

If you have gone through the previous article on motion detection, this one is pretty similar to that. We first initialize the capture device, my webcam in this case. Next we read each frame and do some initial processing on it. Here we are converting RGB into HSV. The reason being, HSV is more robust and can perform well in scenarios where intensity of colors change. I'll probably do an article on this in the future. Color space is an import aspect of image processing and I would only spoil it, if I were to cover it in my lazy week. Getting back on track, we then threshold the image using the inRange method. The ranges are defined before it. You can modify them to get ranges for differnt colors. Here I have used blue because I had plenty of blue objects around me to test the program. Next, we use this thresholded image as a mask and extract the blues from the original frame. Once this is done we display the three frames with imshow.

I'll try to post a video of the process soon. I'm currently trying to make it and upload it. Right now, my internet is pretty much dead and I'm out of internet balance on cellphone. I'll try to post it soon. Thanks a lot for reading.

Four down forty eight to go!

Video Coming Soon...

Comments

Popular posts from this blog

My Experiments with Pi (Part 2 of N)

Certified

My Experiments with Pi (Part 1 of N)