Wa0005
Wa0005
Wa0005
BY
M NAGASAI PRAVEEN
B V SAIKUMAR
1
CERTIFICATE
This is to certify that the report entitled ” Color Detection Using OpenCV ”
submitted by M NAGASAI PRAVEEN (R151878)
B V SAIKUMAR (R151437)
in partial fulfillment of the requirements for the award of Bachelor of Technology in
Computer Science is a bonafide work carried out by them under my supervision and
guidance.The report has not been submitted previously in part or in full to this or any
other University or Institution for the award of any degree or diploma.
2
DECLARATION
We hereby declare that this report entitled “Color Detection Using OpenCV”
submitted by us under the guidance and supervision of P Siva Lakshmi is a bonafide
work. We also declare that it has not been submitted previously in part or in full to
this University or other University or Institution for the award of any degree or
diploma.
3
Acknowledgement
We would like to express our sincere gratitude to P Siva Lakshmi,our project
internal guide for valuable suggestions and keen interest through out the progress of
our project.We are grateful to N ChandraSekhar,HOD CSE, for providing excellent
computing facilities and a congenial atmosphere for progressing with my project.
4
Table of Contents
Topic Page No
1. Introduction 7
1.1 What is Computer Vision ?
2. OpenCV 8
2.1 Why use OpenCV for Computer Vision Tasks?
3. Reading, Writing and Displaying Images 8
4. Context Diagram 9
5. Code 10
6. Explanation of Code 11-14
7. Advantages 15
8. Applications 15
9. Output 15
10. References 16
5
Abstract
With the advancement of modern technologies areas related to robotics and computer
vision, real time image processing has become a major technology under
consideration. So we tried a novel approach for capturing images from the computer
web cam in real time environment and process them as we are required. By using
open source computer vision library (OpenCV for short), an image can be captured
on the basis of its hue, saturation and color value (HSV) range.Basic library functions
are used for loading an image, creating windows to hold image at run time, and to
differentiate images based on their color values. We have also applied function to
threshold the output image in order to decrease the distortion in it. While processing,
the images are converted from their basic scheme Red, Green, and Blue (RGB) to a
more suitable one that is HSV.
KeyWords:
OpenCV, CV2 library, Numpy, Image Processing, RGB to HSV Conversion,
Color Extraction, Color Detection.
6
1. INTRODUCTION
The purpose of computer vision aims to simulate the manner of human eyes
directly by using computer. Computer vision is such kind of research field which
tries to percept and represent the 3D information for world objects.
Computer Vision is among the hottest fields in any industry right now. It is
thriving thanks to the rapid advances in technology and research. But it can be a
daunting space for newcomers. There are some common challenges data scientists
face when transitioning into computer vision, including:
1. How do we clean image datasets? Images come in different shapes and sizes
2. The ever-present problem of acquiring data. Should we collect more images
before building our computer vision model?
3. Is learning deep learning compulsory for building computer vision models?
Can we not use machine learning techniques?
4. Can we build a computer vsiion model on our own machine? Not everyone has
access to GPUs and TPUs!
2. Introduction to Open CV
7
2.1 Why use OpenCV for Computer Vision Tasks?
Every number represents the pixel intensity at that particular location. In the above
image, I have shown the pixel values for a gray scale image where every pixel
contains only one value i.e. the intensity of the black color at that location.
Note that color images will have multiple values for a single pixel. These values
represent the intensity of respective channels – Red, Green and Blue channels for
RGB images, for instance.
8
Reading and writing images is essential to any computer vision project. And the
OpenCV library makes this function a whole lot easier.
4. Context Diagram
9
5. CODE:
10
6. Explanation of Code:
The easiest way to detect and segment an object from an image is the color based
methods . The object and the background should have a significant color difference in
order to successfully segment objects using color based methods.
11
• RGB to HSV Conversion:OpenCV usually captures images and videos in 8-
bit, unsigned integer, BGR format. In other words, captured images can be
considered as 3 matrices; BLUE, GREEN and RED (hence the name BGR)
with integer values ranges from 0 to 255.
The following image shows how a color image is represented using 3 matrices.
In the above image, each small box represents a pixel of the image. In real
images, these pixels are so small that human eye cannot differentiate.
Usually, one can think that BGR color space is more suitable for color based
segmentation. But HSV color space is the most suitable color space for color
based image segmentation. So, in the above application, I have converted the
color space of original image of the video from BGR to HSV image.
12
HSV color space is also consists of 3 matrices, HUE, SATURATION and
VALUE. In OpenCV, value range for HUE, SATURATION and VALUE are
respectively 0-179, 0-255 and 0-255. HUE represents the color,
SATURATION represents the amount to which that respective color is
mixed with white and VALUE represents the amount to which that
respective color is mixed with black.
In the above application, I have considered that the red object has HUE,
SATURATION and VALUE in between 170-180, 160-255, 60-255
respectively. Here the HUE is unique for that specific color distribution of that
object. But SATURATION and VALUE may be vary according to the
lighting condition of that environment.
These are approximate values. You have to find the exact range of HUE values
according to the color of the object. I found that the range of 170-179 is perfect
for the range of hue values of my object. The SATURATION and VALUE is
depend on the lighting condition of the environment as well as the surface of
the object.
13
• Masking technique: The mask is basically creating some specific region of the
image following certain rules. Here we are creating a mask that comprises of
an object in blue color. After that I have used a bitwise_and on the input image
and the threshold image so that only the blue coloured objects are highlighted
and stored in res.We then display the frame, res and mask on 3 separate
windows using imshow function.
• Display the frame: As imshow() is a function of HighGui it is required to call
waitKey regularly, in order to process its event loop.The function waitKey()
waits for key event for a “delay” (here, 5 milliseconds). If you don’t call
waitKey, HighGui cannot process windows events like redraw, resizing, input
event etc. So just call it, even with a 1ms delay .
14
7. Advantages
8. Applications
• People counting.
• Vehicle detection.
• Manufacturing industry applications.
• Tracking objects.
9. Output
We have specified the ranges of RED color, so in the below image it has only
extracted the specified color into the resultant window by ignoring remaining colors
15
10. References
• https://www.pyimagesearch.com/2014/08/04/opencv-python-color-detection/
• https://www.geeksforgeeks.org/detection-specific-colorblue-using-opencv-
python/
• https://www.analyticsvidhya.com/blog/2019/03/opencv-functions-computer-
vision-python/
• https://www.opencv-srf.com/2010/09/object-detection-using-color-
seperation.html
16