Updated April 6, 2023
Introduction to OpenCV Get Image Size
The following article provides an outline for OpenCV Get Image Size. While working with applications of image processing, it is very important to know the dimensions of a given image like the height of the given image, width of the given image and number of channels in the given image, which are generally stored in numpy ndarray and in order to find the dimensions of a given image like the height of the given image, width of the given image and number of channels in the given image, we make use of a function in OpenCV called shape() function and the shape() function returns the dimensions of a given image like the height, width and number of channels in the image.
Syntax to define shape() function in OpenCV:
dimensions = input_image.shape
height = input_image.shape[0]
width = input_image.shape[1]
number_of_channels = input_image.shape[2]
Where,
- input_image represents the image whose dimensions are to be found.
- dimensions represent the dimensions of the image.
- height represents the height of the input image.
- width represents the width of the input image.
- number_of_channels represents the number of channels in the image.
Working of shape() Function in OpenCV
- The dimensions of a given image like the height of the image, width of the image and number of channels in the image are called the shape of the image.
- The shape of the image is stored in numpy.ndarray.
- In order to find the shape of a given image, we make use of a function in OpenCV called shape() function.
- The shape() function can provide the dimension of a given image.
- The shape() function stores each of the dimension of the image like the height of the image, width of the image and number of channels in the image at different indices.
- The height of the image is stored at the index 0.
- The width of the image is stored at index 1.
- The number of channels in the image is stored at index 2.
Examples of OpenCV Get Image Size
Given below are the examples of OpenCV Get Image Size:
Example #1
OpenCV program in python determines the dimension of a given image and displays the height, width, and number of channels in the given image as the output on the screen.
Code:
#importing the module cv2
import cv2
#reading the image whose dimensions are to be found using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/Images/educba.jpg', cv2.IMREAD_UNCHANGED)
#using shape() function to get the dimensions of the image
dimensions = imageread.shape
#using the indices 0, 1 and 2 in shape function to get the height, width and number of channels in the image
height = imageread.shape[0]
width = imageread.shape[1]
channels = imageread.shape[2]
#displaying the dimensions of the image as the output on the screen
print('The dimension of the input image is : ', dimensions)
print('The height of the input image is : ', height)
print('The width of the input image is : ', width)
print('The Number of Channels in the input image are : ', channels)
Output:
In the above program, the required module cv2 is imported. Then we are reading the image whose dimensions are to be detected using the imread() function. Then we are using the shape() function to get the dimensions of the image. Then we are using the indices 0, 1 and 2 in the shape function to get the height, width and number of channels in the image. Then we are displaying the dimensions of the image as the output on the screen.
Example #2
OpenCV program in python determines the dimension of a given image and displays the height, width, and number of channels in the given image as the output on the screen.
Code:
#importing the module cv2
import cv2
#reading the image whose dimensions are to be found using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/Images/logo.png', cv2.IMREAD_UNCHANGED)
#using shape() function to get the dimensions of the image
dimensions = imageread.shape
#using the indices 0, 1 and 2 in shape function to get the height, width and number of channels in the image
height = imageread.shape[0]
width = imageread.shape[1]
channels = imageread.shape[2]
#displaying the dimensions of the image as the output on the screen
print('The dimension of the input image is : ', dimensions)
print('The height of the input image is : ', height)
print('The width of the input image is : ', width)
print('The Number of Channels in the input image are : ', channels)
Output:
In the above program, the required module cv2 is imported. Then we are reading the image whose dimensions are to be detected using the imread() function. Then we are using the shape() function to get the dimensions of the image. Then we are using the indices 0, 1 and 2 in the shape function to get the height, width and number of channels in the image. Then we are displaying the dimensions of the image as the output on the screen.
Example #3
OpenCV program in python determines the dimension of a given image and displays the height, width, and number of channels in the given image as the output on the screen.
Code:
#importing the module cv2
import cv2
#reading the image whose dimensions are to be found using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/Images/car.jpg', cv2.IMREAD_UNCHANGED)
#using shape() function to get the dimensions of the image
dimensions = imageread.shape
#using the indices 0, 1 and 2 in shape function to get the height, width and number of channels in the image
height = imageread.shape[0]
width = imageread.shape[1]
channels = imageread.shape[2]
#displaying the dimensions of the image as the output on the screen
print('The dimension of the input image is : ', dimensions)
print('The height of the input image is : ', height)
print('The width of the input image is : ', width)
print('The Number of Channels in the input image are : ', channels)
Output:
In the above program, the required module cv2 is imported. Then we are reading the image whose dimensions are to be detected using the imread() function. Then we are using the shape() function to get the dimensions of the image. Then we are using the indices 0, 1 and 2 in the shape function to get the height, width and number of channels in the image. Then we are displaying the dimensions of the image as the output on the screen.
Conclusion
In this article, we have seen the concept of shape() function in OpenCV to find an image’s size with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “OpenCV Get Image Size” was beneficial to you. You can view EDUCBA’s recommended articles for more information.