Page 474 - AI Ver 3.0 class 10_Flipbook
P. 474
The value can be:
1-(default) It reads image in BGR format where it has bluish appearance.
0-It reads image in Grayscale format.
-1-It reads image in original format.
How to Write a Program in OpenCV
Following are the steps to write a program in Open CV:
Step 1 Importing the required libraries:
• We import cv2 to start with Computer Vision Application.
• We import matplotlib to display our images on the Jupyter Notebook.
• We import numpy to store the pixel values in the NumPy array.
Step 2 Display a 2D image using the imread()function of cv2. Thus, the complete code that appears in
Jupyter Notebook will be:
[1]: import cv2 # import OpenCV
from matplotlib import pyplot as plt # import matplotlib
import numpy as np # import numpy
img = cv2.imread('D:/Flower.jpg')
plt.imshow(img)
plt.title('Flowers') #Display this title on the top of the image
plt.axis('off')
plt.show()
Did you notice that the colour of the image above is not presented properly? This is because OpenCV represents
the images in BGR (Blue Green Red) combination instead of RGB (Red Green Blue) so the blue color in images
dominates. We will use the following code in imshow()function for converting from BGR to RGB:
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
So, the new code with the new display will be:
[1]: import cv2 # import OpenCV
from matplotlib import pyplot as plt # import matplotlib
import numpy as np # import numpy
img = cv2.imread('D:/Flower.jpg')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('Flowers') #Display this title on the top of the image
plt.axis('off')
plt.show()
472 Touchpad Artificial Intelligence (Ver. 3.0)-X

