OpenCV(Open Source Computer Vision Library) is an open source computer vision and machine learning software library generally used to interpret data in the form of image or video.
It works in C,C++ and python. In this tutorial we will use OpenCV with python.
As it is an open source library, hence you can find it’s complete official documentation at the given https://docs.opencv.org/2.4/
How Images are represented in Computers:
Typically, Digital images are stored in the form of multi-dimensional matrix where the information of every pixel of that image is kept.
Based on the information that we are using to describe any pixel, we can categorize the digital images into two types:
- Grey-scale image: Only one channel of color is used.
- Colored image: 3-channels are used (RGB color combination).
Now let’s starts with the practical:
How to read an image with OpenCV:
cv2.imread(path, flag) method is used to read the image from the given path. It has two arguments:
- path: path of the image
- flag: it is used to specify the way in which the image should be read. It has three variants.
-1: cv2.IMREAD.UNCHANGED, load an image with alpha channel.
0: cv2.IMREAD_COLOR, load a colored image.
1: cv2.IMREAD_COLOR, load an image in grayscale mode.
Default value of flag is 0.
How to display an image with OpenCV:
cv2.imshow(window_name, image) method is used to display the image on a window. It has two arguments:
- window_name: it specifies the name of the window where the image is going to be displayed.
- image: it is the image which has to be displayed.
Implemented code in python:
#import openCV library import cv2 #imread() is a function in cv2 to read images img = cv2.imread('test.jpg',0); #imshow() is a function in cv2 to display images cv2.imshow('image',img); #waitKey() function to create a delay for 5 sec cv2.waitKey(5000);
That’s all I have and thanks a lot for reading. Please let us know if any corrections/suggestions. Please do share and comments if you like the post. Thanks in advance…
Thanks Rishika Gupta for helping us to grow day by day. She is an expert in Machine Learning and Image processing tools.