Obtain Image Information such as Size, Width, Height, Pixels in OpenCV
It is sometimes required to obtain information about the properties of the image such as number of rows, columns and channels, its data type, total number of pixels etc. to do some kind of process on the input image. Such information can be easily obtained using OpenCV and numpy’s built-in functions. In this section, we will see how to derive such information using python and OpenCV.
Get Shape Information of Image
Shape property of the image object can be used to obtain the number of rows, columns as well as the number of channels present in the image. As you know, that for a color image we have three channels while a grayscale image has just one channel. In OpenCV a grayscale image does not even store the information about channels. Let us see how to get shape.
Code 1-1
1
2
3
4
5 | import cv2
path = r'F:\img\coins.jpg'
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
rows, columns= img.shape
print(rows, columns)
|
Output:
246 300
Line 3: We read a grayscale image and put it in img object.
Line 4: We use shape property of the img object to return rows and columns in the same order as shown.
Line 5: Print the rows and columns
Line 6: The output of the code. It prints number of rows and columns in the image. The output prints 246 and 300 which is number of rows and columns, respectively.
Similarly, we can also use shape property with the color image. For color images it returns three values: number of rows, columns and channels in sequence. The code in listing Code 1-2 shows how to get shape information for a color image.
Shape property returns the number of rows, columns and channels in sequence for a color image. The code in listing Code 1-1 shows show to get shape information for a color image.
Code 1-2
1
2
3
4
5 | import cv2
path = r'F:\img\coins.jpg'
img = cv2.imread(path, cv2.IMREAD_COLOR)
rows, columns, channel= img.shape
print(rows, columns, channel)
|
Output:
246 300 3
The output shows that for a color image we receive three values in sequence, rows, columns and the number of channels. You can see that for the current image there are 246 rows, 300 columns and 3 channels.
Note
In OpenCV, we can refer to number of rows as height and number of columns as width of the image. These terms are interchangeable.
Since, the shape will return a list, so we also use an array notation to access the shape information. Code 1-3 shows how to use the array notation to get the shape information of an image.
Code 1-3
1
2
3
4
5 | import cv2
path = r'F:\img\coins.jpg'
img = cv2.imread(path, cv2.IMREAD_COLOR)
info= img.shape
print(info[0], info[1], info[2])
|
Output:
246 300 3
It will produce exactly the same output as in code 1-2. But note that for a color image the array will return three values ( rows, columns and number of channels). On the other hand, for a grayscale image, it will return just two values (rows and columns).
How to find the total number of pixels and data type of an image?
We can obtain the total number of pixels in an image using the size property. Similarly, the data type of the image object can be obtained using dtype property. Code 1-4 shows how to get these two properties and display them in OpenCV.
Code 1-4
1
2
3
4 | import cv2
path = r'F:\img\coins.jpg'
img = cv2.imread(path, cv2.IMREAD_COLOR)
print('Size = ', img.size, ' Data Type = ', img.dtype)
|
The only things that we change in code 1-4 is the line 4. We use img.size to obtain the number of pixels in the image and img.dtype to obtain the data type of the img object. The img.dtype returned uint8 which stands for unsigned integer 8. This means that the image pixels value will be only positive numbers. Here 8 indicates that the number of bits used to represent each pixel. So the image is of 8 bits (means each pixel is represented using 8 bits).
.img.size returns 221400 which is the total number of pixels in the image. This information can also be obtained using following formula:
Total Pixels = width x height x channels
Total pixels = 246 x 300 x 3 = 221400