Page 305 - AI Computer 10
P. 305
where,
path refers to the location from where the image is to be read, and
flag refers to the way how our image should be read.
The value of ‘flag’ parameter can be:
1: This is the default value. This value reads the image in BGR format where it has a bluish appearance.
0: This value helps to read the image in Grayscale format.
–1: This value helps to read the image in its original format.
Writing a Program
To write a program in OpenCV, follow the given steps:
Step 1: Import the required libraries such as cv2, matplotlib, and numpy.
Step 2: Display a 2D image using the imread( ) function of cv2 library. Output
Example: Code to display a 2D image in Jupyter notebook using openCV.
import cv2 from matplotlib
import pyplot as plt
import numpy as np
img = cv2.imread(‘E:\Project\images.jpg’)
plt.imshow(img)
plt.title(‘My Dear Friend’)
plt.axis(‘off’)
plt.show()
Here, a blue-coloured image is displayed because OpenCV represents images in BGR (Blue, Green, Red) colour
instead of RGB (Red, Green, Blue) colour.
To convert BGR image to RGB image, use imshow( ) function.
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
So, the new code will be:
import cv2 from matplotlib Output
import pyplot as plt
import numpy as np
img = cv2.imread(‘E:\Project\images.jpg’)
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
plt.title(‘My Dear Friend’)
plt.axis(‘off’)
plt.show()
Image Processing Operations
In general the image processing operations are cropping an image, copying and pasting an image, resizing an
image, etc.
Copying and Pasting an Image
In your daily life, you have performed cut, copy, and paste operations on your computer several times. But, now
we are going to learn these operations with the help of programming using Computer Vision technology.
171
171