Image.shape:
It returns a tuple of number of rows, columns and channels of the image.
Image.size:
It returns the total number of pixel which can be accessed in the image.
Image.dtype:
It returns the datatype of the image.
Python code:
#import openCV library import cv2 img = cv2.imread('test.jpg',1); #img.shape returns a tuple (numbers of rows, number of columns, number of channels) in the image print(img.shape); #img.size returns the total number of pixels in the image print(img.size); #img.dtype returns the datatype of image print(img.dtype);
Console:
(183, 275, 3)
150975
uint8
cv2.split():
cv2.split(image) is used to split the given image into its different channels. It has only one argument i.e., the image which we want to split.
cv2.merge():
cv2.merge() is used to merge the images into one image. If we merge the different channel of an image together then we will obtain the original image again.
Python code:
#import openCV library import cv2 img = cv2.imread('test.jpg',1); #original image cv2.imshow('image1',img); b,g,r = cv2.split(img); #splitted image( channel b is shown) cv2.imshow('image2',b); img2 = cv2.merge((b,g,r)); #image after merging all the channels again cv2.imshow('image3',img2); cv2.waitKey(10000); cv2.destroyAllWindows();
Output:

How to crop an image:
As the image is represented as a multidimensional matrix, hence we can use slicing to crop an image.
Python code:
#import openCV library import cv2 img = cv2.imread('test.jpg',1); #original image cv2.imshow('image1',img); img2 = img[0:100,50:200]; #image after slicing cv2.imshow('image2',img2); cv2.waitKey(30000); cv2.destroyAllWindows();
Output:

Thank you Rishika Gupta for this article.
0 Comments