Basic Operations on Images using openCV in Python


In this article, you learn to:
  • Load, display and save the image.
  • Access Image properties.
  • Access and Modify Pixel Values.
  • Resize The Image

OpenCV (Open Source Computer Vision Library) is a computer vision and machine learning software library used most widely for image manipulation and processing.  

Load, Display and Save the Image:

Load An Image:

To read/ load the image imread() function is used. Let's load the image.

import cv2
img = cv2.imread('lena.png')

Either if you have image in a different file other than where you are running this Python code then use below code.

import cv2
image_path = r'D:\Binary Study\Pictures\lena.png'
img = image.imread(image_path)


OpenCV reads the image as BGR Image. To convert from BGR to RGB you can use below code:

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

You may be interested in the below post to learn different ways to load image.

Display The Image:

imshow() function is used to display the image. Let's display the image.
 
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


Save The Image:

To save the image use imwrite() . Let's save the image.

cv2.imwrite('lena_new.png', img)

This python code will save the image 'img' as named "lena_new" in the same file where you are running this python code. Don't forget to give image type, as here .png.  


Accessing Image Properties:

Image properties are shape of image (which includes number of rows, number of columns and number of color channels), size of image (which is total number of pixels in the image),data type of image etc.

Accessing Shape of an Image:

To access shape of image use img.shape. This returns a tuple of number of rows, columns, and channels( if color image). 

print(img.shape)

output:
(794, 600, 3)

Here number of rows, columns and color channels are 794, 600, and 3 respectively. 

Note: If image is gray-scale then it will return only number of rows and columns. So you can use this to check an image if it is colored or not.


Accessing Size of The Image:

Total number pixels in an image is accessed by img.size: 

print(img.size)
output:
1429200

Note: If shape is (794, 600, 3)  then size = 794*600*3 = 1429200

Accessing Image data type:

Use img.dtype to obtain data type of an image:

print(img.dtype)
output:
dtype('uint8')

What is Type of An Image:

All Images are numpy ndarray. You can access what is the type of an image using type(image_name).  

print(type(img))
output:
numpy.ndarray


Accessing And Modifying Pixel Values:

We can access a pixel value by its row and column values. If we have a BGR image it returns an array of Blue, Green and Red values. For grayscale image it returns corresponding intensity value.

print(img[250,250])
output:
[143 149 190]

To modify a pixel value, just do assignment:

img[250250] = [150,149,100]
print(img[250,250])
output:
[150 149 100]

We can also apply indexing and slicing to modify the pixel values of an image. 

img[0:500:100] = 0

We can use image indexing and slicing to trim the images. One of the best uses of image indexing and slicing is adding or subtracting images of different sizes.  

How To Resize An Image:

To resize an image you can use cv2.resize(img, (w, h)). where img is original image, w and h are image width(column) and  height(row) to resize.

import cv2
impot matplotlib.pyplot as plt
# read the original image
img = cv2.imread('lena.png')
# get the shape of original image
print(img.shape)
# display the original image
plt.imshow(img)
# here plt.imshow() is used for only to display the scale

out put:
(794, 600, 3)
OpenCV logo Image Original
Original Image

import cv2
impot matplotlib.pyplot as plt
# Now resize the image
img_r = cv2.resize(img, (400,500))
# get the shape of resized image
print(img_r)
# display the resized image
plt.imshow(img_r)
# here plt.imshow() is used for only to display the scale. You should use cv2.imshow()

out put:
(500, 400, 3)

Comments

Post a Comment