The document provides an introduction to OpenCV, an open-source computer vision library used for image and video processing tasks, available in Python and C++. It outlines the installation process, basic functions like imread and imshow for image reading and displaying, and various image processing techniques such as color conversions and creating negative images. Additionally, it mentions common tasks and future topics related to OpenCV including colormaps and video processing.
OpenCV
OpenCV standsfor open-source computer vision.
It is basically a powerful and reliable library for performing image
processing and computer vision tasks.
A library in programming refers to a set/package of pre-defined functions.
This library is popular for its easiness and performance.
This library is available in both python and C++ with functions having
different syntaxes.
Some of the common tasks done using OpenCV includes face detection,
image processing, video processing and so on…
3.
OpenCV-prerequisites
The keyfeature in python version of OpenCV is that the functions are
obvious. This means that the meaning of the function is known from the
name of the function.
OpenCV in python language is known as cv2. This is the name of the
library we have use.
This library may not have been pre-installed in your IDE. Hence it is
essential to install the library on the prompt.
Windows-> Search bar-> Command Prompt-> Run as Administrator.
After this, you must type pip install opencv-python
Voila!! Opencv is installed in your system.
4.
Basic functions withimages
Since the library is installed in the system, we can use them by
simply importing them by the command import cv2
So now we can do a lot of interesting stuffs on images. But
before that it is essential to load/read the image in the IDE.
This is done using the imread function.
cv2.imread(image filename, mode)
This is the syntax for the imread function, and the role is
obvious, it is going to read the image from the system to the
IDE.
5.
Image extensions
Thereare three most used image extensions.
PNG- Portable Graphics
Format
JPG- Joint Photographic
Group
JPEG- Joint Photographic
Expert Group
6.
Image name
Thefirst argument required for the function is the filename.
It is the name of the image that the user has kept along with the proper
extension.
But for this, it is essential that the image must be in the IDE and this
method fails for most of the time.
7.
Alternative
Instead ofgiving the name of the file, we can give the location of the file.
This method works universally, and the advantage is that the image can be
anywhere in the system. So we don’t need to keep separate storage.
Right click on an image and search for properties, it that copy the location
of the image.
But the location will not include the name of the image.
So we have to append the name of the image file and its appropriate
dimension along with that of the copied location.
8.
Contd…
So nowwe can store this entire address in a temporary variable and then
use this variable in the imread function.
But this also throws up an error, this error is known as Unicode escape
error meaning that the compiler is unable to access the location of the
image.
9.
Solution for theerror
So to prevent this error, there is a simple solution.
Replace the single slashes present in between each address field with
double slashes.
By doing this, the compiler will be able to access the location and hence
the image will be successfully read.
So now we can do a lot of image processing on the read image.
10.
Modes
The secondargument present in the imread function is the mode.
This defines the way to read the image.
cv2.imread_GRAYSCALE is used to read image in black and white format.
cv2.imread_COLOR is used to read the image in color format.
There might be some difficulties in remembering these names…right?
So for this, there are indices in the mode, meaning that certain numbers
are associated with the modes.
The number 0 is to read in B&W and 1 to read in color.
Displaying an image
Assuming that we have applied a suited image processing technique over
our read image, it is essential to show the image. We have to display the
image on which we have performed our processing on.
For this, there is a function in OpenCV called as imshow.
cv2.imshow(“image name”, image)
The first argument is the name that we want to keep for the image. It is
essential that we give the name within single or double quotes.
The second argument is the image, this is the image which has been read
by the imread function.
We can store the read image in a temporary variable and then use it in the
imshow function.
13.
Contd…
An errorwill show up if we just write the imshow function.
So in order to prevent this, we must use the cv2.waitKey() function. This
provides a delay in time which is essential to display an image.
The argument is a positive number. Put 0 to get the image
instantaneously.
14.
cvtColor
Color spaceconversions can be achieved using the cvtColor function.
cv2.cvtColor(imread,mode)
There are two arguments for this function.
The first one is the image that is read by the imread function.
The second is the mode, like the conversions.
They have to be given as cv2.COLOR_mode which we wish.
Some examples are shown in the upcoming slides….
15.
BGR2GRAY
This functionis used to convert a colored image to a grayscale image.
It is an alternative to imread mode method.
16.
BGR2RGB
This methodis used to convert the color image to the RGB format.
In OpenCV, the color image is present in BGR format, hence the RGB
format in OpenCV looks different.
BGR2HSV
This modeis used to convert a colored image to an hsv format type image
The HSV stands for Hue Saturation Value.
This is the temperature measurement for the colors.
19.
Negative images
Thenext set of images we can develop using open-cv is the negatives.
The negative images are the images which are obtained by reversing the
image.
The literal meaning is to replace the pixel’s values or to do the inversion
operation.
This can be achieved by the bitwise_not() function.
This function takes in the read image and then produces a negative image.
This option is available for both grayscale and color images.
Future presentations….
Colormapsused in opencv
Video processing using opencv
Applying filters on input videos
Introducing shapes on images and videos
Haarcascades