Knowing image properties helps us understand the image's size, color format, and data type. This is important before using images in machine learning.
Image properties (shape, dtype, size) in 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).
print(image.shape) # (height, width, channels)
print(image.dtype) # e.g., uint8
print(image.size) # total number of elements
This code creates a small random color image and prints its array, shape, data type, and size.
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)
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.
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.
Practice
shape property of an image represent?Solution
Step 1: Understand what shape means in images
The shape of an image is a tuple that shows its height, width, and number of color channels.Step 2: Differentiate shape from other properties
File size and data type are different properties; shape specifically refers to dimensions and channels.Final Answer:
The dimensions and number of color channels of the image -> Option CQuick Check:
Shape = dimensions + channels [OK]
- Confusing shape with file size
- Mixing up data type with shape
- Thinking shape shows compression
img?Solution
Step 1: Recall NumPy syntax for data type
In NumPy, the data type of an array is accessed using thedtypeattribute.Step 2: Check each option
Onlyimg.dtypeis valid syntax; others are incorrect or do not exist.Final Answer:
img.dtype -> Option AQuick Check:
Use .dtype to get data type [OK]
- Using parentheses like a function
- Trying non-existent attributes
- Confusing dtype with type() function
import numpy as np img = np.zeros((100, 200, 3), dtype=np.uint8) print(img.size)
What will be the output?
Solution
Step 1: Understand the shape and size
The image shape is (100, 200, 3). Size is total number of elements = 100 * 200 * 3 = 60000.Step 2: Confirm what .size returns
Thesizeattribute returns total pixels including all channels.Final Answer:
60000 -> Option BQuick Check:
Size = height * width * channels = 60000 [OK]
- Using only height or width as size
- Ignoring color channels in size
- Confusing size with shape
import numpy as np img = np.array([[255, 128], [64, 0]]) print(img.shape) print(img.dtype)
What is the error in this code if the goal is to represent a color image?
Solution
Step 1: Check the array shape
The array shape is (2, 2), meaning 2 rows and 2 columns, no color channels.Step 2: Understand color image requirements
A color image needs 3 dimensions: height, width, and channels (usually 3 for RGB).Final Answer:
The array shape lacks a color channel dimension -> Option DQuick Check:
Color images need 3D shape [OK]
- Thinking dtype must be float for images
- Assuming shape attribute is wrong
- Believing pixel values are out of range
(256, 256) and dtype float32. You want to convert it to an 8-bit unsigned integer image suitable for display. Which code snippet correctly does this?Solution
Step 1: Understand dtype conversion needs
Converting from float32 (0 to 1 range) to uint8 (0 to 255) requires scaling by 255.Step 2: Check each option
img_uint8 = (img * 255).astype(np.uint8) scales and converts correctly. img_uint8 = img.astype(np.uint8) converts without scaling, causing wrong values. Options A, B, and D do not convert to uint8 properly.Final Answer:
img_uint8 = (img * 255).astype(np.uint8) -> Option AQuick Check:
Scale float to 255 then convert to uint8 [OK]
- Skipping scaling before type conversion
- Using wrong dtype conversion
- Dividing instead of multiplying
Ready to prove it?
Pass the test and this pattern moves unknown
