Check if the image is empty using OpenCV Python

OpenCV is an Open Source Computer Vision Library in Python. It is one of the most popular image processing libraries and uses NumPy arrays to represent images. In this article, we will explore different methods to check if an image is empty or blank using OpenCV Python.

Input-Output Scenarios

We'll work with a blank input image and demonstrate how to programmatically detect whether an image is empty ?

Input Image

Blank input image

Expected Output

Image is empty

Method 1: Using Unique Pixel Count

An empty image typically has very few unique pixel values. We can count unique pixels and compare against a threshold ?

import numpy as np
import cv2

def check_empty_by_unique_pixels():
    # Create a blank image for testing
    blank_image = np.zeros((100, 100), dtype=np.uint8)
    
    # Get unique pixel values and their counts
    unique_values, counts = np.unique(blank_image, return_counts=True)
    
    # Check if unique pixel count is below threshold
    if len(unique_values) < 10:
        return "Image is empty"
    else:
        return "Image is not empty"

result = check_empty_by_unique_pixels()
print(result)
Image is empty

Method 2: Using Standard Deviation

Empty images have uniform pixel values, resulting in low standard deviation. A standard deviation close to zero indicates a blank image ?

import numpy as np
import cv2

def check_empty_by_std():
    # Create a blank image for testing
    blank_image = np.zeros((100, 100), dtype=np.uint8)
    
    # Calculate standard deviation
    std_dev = np.std(blank_image)
    print(f"Standard deviation: {std_dev}")
    
    # Check if standard deviation is below threshold
    if std_dev < 1:
        return "Image is empty"
    else:
        return "Image is not empty"

result = check_empty_by_std()
print(result)
Standard deviation: 0.0
Image is empty

Method 3: Using Pixel Sum

The most straightforward approach is summing all pixel values. For a completely black image, the sum will be zero ?

import numpy as np
import cv2

def check_empty_by_sum():
    # Create a blank image for testing
    blank_image = np.zeros((100, 100, 3), dtype=np.uint8)
    
    # Calculate sum of all pixel values
    pixel_sum = np.sum(blank_image)
    print(f"Pixel sum: {pixel_sum}")
    
    # Check if sum equals zero
    if pixel_sum == 0:
        return "Image is empty"
    else:
        return "Image is not empty"

result = check_empty_by_sum()
print(result)
Pixel sum: 0
Image is empty

Complete Function for Image Files

Here's a comprehensive function that can check empty images from files using all three methods ?

import numpy as np
import cv2

def is_image_empty(image_path, method='sum'):
    """
    Check if an image is empty using different methods
    
    Parameters:
    image_path: Path to the image file
    method: 'sum', 'std', or 'unique'
    """
    try:
        # Read image in grayscale for consistency
        image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
        
        if image is None:
            return "Could not load image"
        
        if method == 'sum':
            return "Empty" if np.sum(image) == 0 else "Not empty"
        
        elif method == 'std':
            return "Empty" if np.std(image) < 1 else "Not empty"
        
        elif method == 'unique':
            unique_values = np.unique(image)
            return "Empty" if len(unique_values) < 10 else "Not empty"
        
    except Exception as e:
        return f"Error: {str(e)}"

# Example usage would be:
# print(is_image_empty('blank_image.jpg', 'sum'))

Comparison of Methods

Method Best For Threshold Speed
Pixel Sum Completely black images 0 Fast
Standard Deviation Near-uniform images < 1 Fast
Unique Pixels Images with few colors < 10 Medium

Conclusion

Use pixel sum for detecting completely black images, standard deviation for near-uniform images, and unique pixel count for images with limited color variation. The pixel sum method is typically the fastest and most reliable for truly empty images.

Updated on: 2026-03-27T06:50:41+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements