Home » Tutorial: Accessing Image Properties Using OpenCV in Python

Tutorial: Accessing Image Properties Using OpenCV in Python

1 Year Subscription
Java SE 11 Programmer I [1Z0-815] Practice Tests
Spring Framework Basics Video Course
Oracle Java Certification
Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer II [1Z0-816] Practice Tests

OpenCV allows you to work with image properties such as dimensions, size, type, and color information.

Understanding these properties is essential when processing images for various computer vision tasks.

What You’ll Learn

1. Reading Image Properties

OpenCV provides the cv2.imread() function to read an image, after which you can access its properties using NumPy-like operations since OpenCV represents images as multidimensional arrays.

2. Accessing Dimensions and Shape

Example: Reading and Printing Dimensions

import cv2

# Read an image
image = cv2.imread("input.jpg")

# Access dimensions
height, width, channels = image.shape
print(f"Width: {width}, Height: {height}, Channels: {channels}")

Explanation

  • image.shape:
    • Returns a tuple (height, width, channels) for color images.
    • Returns (height, width) for grayscale images.

3. Checking Image Channels

Example: Grayscale vs Color Image

import cv2

# Read images
color_image = cv2.imread("input.jpg", cv2.IMREAD_COLOR)  # Color
gray_image = cv2.imread("input.jpg", cv2.IMREAD_GRAYSCALE)  # Grayscale

# Check dimensions
print("Color Image Shape:", color_image.shape)
print("Grayscale Image Shape:", gray_image.shape)

Output Example

Color Image Shape: (480, 640, 3)
Grayscale Image Shape: (480, 640)

4. Determining Image Data Type

The datatype of the image array indicates how pixel values are stored. OpenCV uses uint8 (unsigned 8-bit integers) by default, where pixel values range from 0 to 255.

Example: Checking Data Type

import cv2

# Read an image
image = cv2.imread("input.jpg")

# Check data type
print(f"Data type: {image.dtype}")

Explanation

  • image.dtype:
    • Returns the data type of the array elements, e.g., uint8.

5. Accessing Pixel Values

You can directly access and modify pixel values using NumPy-style indexing.

Example: Access and Modify Pixel Values

import cv2

# Read an image
image = cv2.imread("input.jpg")

# Access pixel at (100, 200)
pixel_value = image[100, 200]
print(f"Pixel value at (100, 200): {pixel_value}")

# Modify pixel value at (100, 200)
image[100, 200] = [255, 0, 0]  # Set to blue
print(f"Modified pixel value at (100, 200): {image[100, 200]}")

Explanation

  • For color images:
    • image[y, x] returns [B, G, R] (Blue, Green, Red).
  • For grayscale images:
    • image[y, x] returns a single intensity value.

Example: Access Individual Color Channels

import cv2

# Read a color image
image = cv2.imread("input.jpg")

# Access the blue channel of pixel at (100, 200)
blue = image[100, 200, 0]
print(f"Blue value at (100, 200): {blue}")

# Access the green channel
green = image[100, 200, 1]
print(f"Green value at (100, 200): {green}")

# Access the red channel
red = image[100, 200, 2]
print(f"Red value at (100, 200): {red}")

6. Practical Examples

6.1 Calculate Total Pixels

import cv2

# Read an image
image = cv2.imread("input.jpg")

# Calculate total number of pixels
total_pixels = image.size
print(f"Total pixels: {total_pixels}")

Explanation

  • image.size:
    • Returns the total number of elements in the image array.

6.2 Calculate Image Size in Memory

import cv2

# Read an image
image = cv2.imread("input.jpg")

# Calculate image size in memory (bytes)
image_memory = image.size * image.itemsize
print(f"Image size in memory: {image_memory} bytes")

Explanation

  • image.itemsize:
    • Returns the size of each element in bytes.

6.3 Extract and Save a Region of Interest (ROI)

import cv2

# Read an image
image = cv2.imread("input.jpg")

# Extract a region of interest (ROI)
roi = image[50:200, 100:300]  # y1:y2, x1:x2

# Save the extracted ROI
cv2.imwrite("roi.jpg", roi)
print("ROI saved as roi.jpg")

6.4 Convert Color Image to Grayscale

import cv2

# Read a color image
image = cv2.imread("input.jpg")

# Convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Save grayscale image
cv2.imwrite("gray_image.jpg", gray_image)
print("Grayscale image saved as gray_image.jpg")

6.5 Split and Merge Color Channels

import cv2

# Read a color image
image = cv2.imread("input.jpg")

# Split color channels
b, g, r = cv2.split(image)

# Save individual channels
cv2.imwrite("blue_channel.jpg", b)
cv2.imwrite("green_channel.jpg", g)
cv2.imwrite("red_channel.jpg", r)

# Merge channels back
merged_image = cv2.merge((b, g, r))
cv2.imwrite("merged_image.jpg", merged_image)

6.6 Change Image Resolution

import cv2

# Read an image
image = cv2.imread("input.jpg")

# Resize the image
resized_image = cv2.resize(image, (300, 200))  # Width x Height

# Save the resized image
cv2.imwrite("resized_image.jpg", resized_image)
print("Resized image saved as resized_image.jpg")

7. Summary

Key Properties

  • shape: Returns dimensions of the image.
  • size: Total number of elements (pixels).
  • dtype: Data type of the image array.
  • itemsize: Size of each array element in bytes.

Best Practices

  1. Use shape to determine dimensions and channels.
  2. Access and modify pixel values with care, especially for large images.
  3. Leverage slicing for efficient processing of specific regions (ROI).
  4. Understand memory requirements when working with high-resolution images.

By mastering image properties, you’ll be better equipped to process and analyze images effectively using OpenCV

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More