Page 258 - Ai Book - 10
P. 258
The imread() Function
The imread() function reads the image and loads it into the computer’s memory. The basic syntax of imread()
function is:
cv2.imread(path,flag)
Where,
Path refers to the path of the image to be read and flag refers to the way how our image should be read.
The value of ‘flag’ parameter can be:
1: It is the default value. This value reads the image in BGR format where it has 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 Open CV, follow the given steps:
Step 1: Importing the required libraries such as cv2, matplotlib and numpy.
Step 2: Display a 2D image using the imread() function of cv2. Output
Example: Code to display a 2D image in Jupyter notebook using.
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, blue colour image is displayed because OpenCV represents images in BGR(Blue,Green,Red) colour instead
of RGB (Red,Green,Blue) color.
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 Output
from matplotlib 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
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, in this
class, we are going to learn these operations with the help of programming using computer vision technology.
132
132