Wa0005

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

A Report submitted to the Rajiv Gandhi University of Knowledge Technologies in

partial fulfillment of the degree of Bachelor of Technology in Computer Science.

BY
M NAGASAI PRAVEEN
B V SAIKUMAR

Under the supervision of


P Siva Lakshmi
Assistant Professor
Computer Science Engineering

Idupulapaya, Vempalli, Kadapa - 516 330


Andhra Pradesh, India

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.

P Siva Lakshmi, N Chandra Sekhar,


Project internal guide, Project Coordinator, Head of the Department,
CSE, CSE, CSE,
RGUKT, R.K Valley. RGUKT, R.K Valley. RGUKT, R.K Valley.

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.

Date: 19th Nov 2019 M NAGASAI PRAVEEN (R151878)


Place: R K Valley B V SAIKUMAR (R151437)

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.

At the outset, we would like to thank Rajiv Gandhi university of Knowledge


and Technologies,R.K Valley for providing all the necessary resources for the
successful completion of our project.

With Sincere Regards,


M NAGASAI PRAVEEN(R151878),
B V SAIKUMAR (R151437).

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.

Its essence is to reconstruct the visual aspects of 3D object by analyzing the


2D information extracted accordingly. Real life 3D objects are represented by 2D
images.

1.1 What is Computer Vision ?

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

Open CV is an open source computer vision library. The library is written in C


and C++and runs under Linux, Windows and provides interfaces for Python, Ruby,
Mat lab and other languages.

Open CV library contains abundant advanced math functions,image processing


functions, and computer vision functions that span many areas in vision.

7
2.1 Why use OpenCV for Computer Vision Tasks?

OpenCV, or Open Source Computer Vision library, started out as a research


project at Intel. It’s currently the largest computer vision library in terms of the sheer
number of functions it holds.
OpenCV contains implementations of more than 2500 algorithms! It is freely
available for commercial as well as academic purposes. And the joy doesn’t end
there! The library has interfaces for multiple languages, including Python, Java, and
C++.
The first OpenCV version, 1.0, was released in 2006 and the OpenCV community has
grown leaps and bounds since then.

3. Reading, Writing and Displaying Images


Machines see and process everything using numbers, including images and
text. How do you convert images to numbers – I can hear you wondering. Two words
– pixel values:

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:

Object detection and segmentation is the most important and challenging


fundamental task of computer vision. It is a critical part in many applications such as
image search, scene understanding, etc. However it is still an open problem due to the
variety and complexity of object classes and backgrounds.

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.

• Camera Settings: In order to perform runtime operations, the device’s web


camera is used. To capture a video, we need to create a Video Capture object.
Its argument can be either the device index or the name of a video file. Device
index is just the number to specify which camera. Normally one camera will be
connected, so we simply pass 0. You can select the second camera by passing 1
and so on. After that, you can capture frame-by-frame. But at the end, don’t
forget to release the capture. Moreover if anyone wants to apply this color
detection technique on any image it can be done with little modifications in the
code which I’ll discuss later.
• Capturing frames: The infinite loop is used so that the webcamera captures
the frames in every instance and is open during the entire course of the
program.After capturing the live stream frame by frame we are converting each
frame in BGR color space(the default one) to HSV color space. There are more
than 150 color-space conversion methods available in OpenCV. But we will
look into only two which are most widely used ones, BGR to Gray and BGR to
HSV. For color conversion, we use the function cv2.cvtColor(input_image,
flag) where flag determines the type of conversion. For BGR to HSV, we use
the flag cv2.COLOR_BGR2HSV. Now we know how to convert BGR image
to HSV, we can use this to extract a colored object. In HSV, it is more easier to
represent a color than RGB color-space.In specifying the range , we have
specified the range of blue color. Whereas you can enter the range of any
colour you wish.

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.

Hue values of basic colors


• Orange 0-22
• Yellow 22- 38
• Green 38-75
• Blue 75-130
• Violet 130-160
• Red 160-179

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 .

• Summarizing the process:


1. Take each frame of the video.
2. Convert each frame from BGR to HSV color-space.
3. Threshold the HSV image for a range of blue color.

14
7. Advantages

• Better accuracy in segmentation under various illuminations.


• Less time consuming process.
• It is less sensitive to background noise.

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

You might also like