Python OpenCV library basics

I am using Win10 64 bit OS, and using pip, I installed opencv-python from CMD.

The library name is called ‘opencv-python’ for Windows.

Using jupyter notebook, the code to open an image and change the file format. This example changed .jpg to .png.

import numpy
import cv2

img = cv2.imread(r'C:\Users\L\Documents\ACS\OpenCV\Me.jpg')
cv2.namedWindow("My Image", cv2.WINDOW_NORMAL)
cv2.imshow("My Image", img)
cv2.waitKey(0)

#change the image format to png
#new img file saved in users-OpenCV folder
cv2.imwrite("Output.png", img)

Displaying image as pixels, the result will be an array.

import numpy
import cv2

#argument 1 means use default colours and channels for the image
#argument 0 means use black and white for the image
img = cv2.imread(r'C:\Users\Linawati\Documents\ACCS\OpenCV\Me.jpg', 1)

#running img will display the pixels found in the image as a long array
img

Output is an array which contain the pixels values (R,G,B)

array([[[253, 255, 243],
        [253, 255, 243],
        [253, 255, 243],
        ...,
        [156, 160, 161],
        [155, 160, 159],
        [155, 160, 159]],

       [[253, 255, 243],
        [253, 255, 243],
        [253, 255, 243],
        ...,
        [162, 166, 167],
        [160, 165, 164],
        [156, 161, 160]],

       [[253, 255, 243],
        [253, 255, 243],
        [253, 255, 243],
        ...,
        [167, 172, 173],
        [167, 172, 173],
        [166, 171, 172]],

       ...,

       [[146, 172, 186],
        [146, 172, 186],
        [146, 172, 186],
        ...,
        [145, 169, 181],
        [142, 166, 178],
        [143, 167, 179]],

       [[144, 170, 184],
        [144, 170, 184],
        [144, 170, 184],
        ...,
        [147, 169, 181],
        [146, 168, 180],
        [146, 168, 180]],

       [[146, 172, 186],
        [144, 170, 184],
        [143, 169, 183],
        ...,
        [146, 168, 180],
        [145, 167, 179],
        [145, 167, 179]]], dtype=uint8)
type(img)
numpy.ndarray
len(img)
2592
len(img[0])
1944
len(img[0][0])
3
img.shape
(2592, 1944, 3)
img.dtype
dtype('uint8') 

Line10: the image type is a numpy ndarray ——- Line11: the number of rows in the image ——— Line12: number of columns in the image ——— Line13: the number of channels (R,G,B), if the image got layering, then number of channels will be 4. ——– Line14: using img.shape will display the row, column and channel all at one shot.———Line15: the dtype is ‘uint8’, unsigned integer 8, means there are 28 values for each pixel of the image, which is 256. Since the count start at 0, the maximum value is 255.

Below is the pixel RGB at row1 column 1 . Comparing with the top ndarray, indeed, it is 253,255,243. If the pixel at the position is maximum value of 255,255,255, it means it is a white colour pixel.

img[1,1]
array([253, 255, 243], dtype=uint8)

To get the number of pixels for the image:

img.size
15116544 
import numpy as np
import cv2

#array of all zeros, 150x200 pixels , 1 channel only, 256 values
black = np.zeros([150,200,1], 'uint8')
cv2.imshow("Black Window", black)
#1st pixel, all values 
print(black[0,0,:])

#array of all ones, 150x200 pixels , 3 channel only, 65536 values
stillBlack = np.ones([150,200,3], 'uint16')
cv2.imshow("Still Black Window",stillBlack)
#1st pixel, all values 
print(stillBlack[0,0,:])

white = np.ones([150,200,3], 'uint16')
white *= (2**16-1)
cv2.imshow("White Window",white)
#1st pixel, all values 
print(white[0,0,:])


colour = np.ones([150,200,3], 'uint8')
#Assign 255,0,0 to all the pixels
colour[:,:] = (255,0,0)
cv2.imshow("Blue Window",colour)
#1st pixel, all values 
print(colour[0,0,:])

#keep displaying image until Enter key is pressed
cv2.waitKey(0)
cv2.destroyAllWindows()

Results of above code:

[0]
[1 1 1]
[65535 65535 65535]
[255   0   0]
The 4 windows

Image Types and Colour Channels: