Knowing image properties helps us understand the image's size, color format, and data type. This is important before using images in machine learning.
0
0
Image properties (shape, dtype, size) in Computer Vision
Introduction
Checking the size of images before feeding them into a model.
Verifying the color channels (like RGB or grayscale) of images.
Ensuring the data type matches what the model expects (e.g., integers or floats).
Debugging image loading issues by inspecting image properties.
Preparing images for resizing or normalization steps.
Syntax
Computer Vision
image.shape image.dtype image.size
shape shows the dimensions of the image (height, width, channels).
dtype tells the type of data stored (like uint8 for 0-255 pixel values).
size gives the total number of elements (height x width x channels).
Examples
This prints the size and color channels of the image.
Computer Vision
print(image.shape) # (height, width, channels)
This shows the type of numbers used to store pixel values.
Computer Vision
print(image.dtype) # e.g., uint8
This gives the total count of all elements in the image array.
Computer Vision
print(image.size) # total number of elements
Sample Model
This code creates a small random color image and prints its array, shape, data type, and size.
Computer Vision
import numpy as np import cv2 # Create a simple 3x3 RGB image with random colors image = np.random.randint(0, 256, (3, 3, 3), dtype=np.uint8) print('Image array:') print(image) print('Shape:', image.shape) print('Data type:', image.dtype) print('Size:', image.size)
OutputSuccess
Important Notes
Images usually have 3 channels for color (Red, Green, Blue) or 1 for grayscale.
Data type uint8 means pixel values range from 0 to 255.
Shape order is usually (height, width, channels) in most libraries like OpenCV and NumPy.
Summary
Shape tells the image dimensions and color channels.
Data type shows how pixel values are stored.
Size is the total number of elements in the image array.