Page 476 - AI Ver 3.0 class 10_Flipbook
P. 476
It means that the above image has a minimum pixel value as 0 and maximum pixel value as 255.
• The pixel value by its row and column coordinates of an image can be accessed by using img[y
coordinate,x,coordinate,channels]. For the BGR image, it returns an array of Blue, Green, Red values.
For grayscale images, just corresponding intensity is returned. For example:
✶ [B,G,R]= img[25, 40]
print(‘R=’, R, ‘G=’, G, ‘B=’,B)
th
It will display the Blue, Green, Red colour code between 0 to 255 of pixel at 25 row and 40 column of
an image taken with imread().
✶ blue=img[25,40,0] #traps only blue code
green=img[25,40,1] #traps only green code
red=img[25,40,2] #traps only red code
print(“Blue colour code at this pixel :”,blue)
print(“Green colour code at this pixel :”,green)
print(“Red colour code at this pixel :”,red)
th
th
It will display the Blue, Green, Red colour code between 0 to 255 pixel at 25 row and 40 column of an
rd
image using 3 parameter as 0,1,2.
Let us now see how the above discussed functions will affect the code of displaying an image:
[1]: import cv2 # import OpenCV
from matplotlib import pyplot as plt # import matplotlib
import numpy as np # import numpy
img = cv2. imread( 'D:/RGB.png' )
plt. imshow(cv2.cvtColor(img, cv2. COLOR_BGR2RGB) );
plt. axis( 'on');
plt. title( 'RGB - Display' )
plt. show()
print (img. shape)
[B,G,R]= img[25, 40]
print( 'Red=', R, 'Green=', G, 'Blue=', B)
Cropping an Image
Sometimes we need to remove the unwanted parts of the image to make it appear in a frame and help us focus
on certain areas of the image. This can be done by cropping an image.
474 Touchpad Artificial Intelligence (Ver. 3.0)-X

