Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if an image is partially occluded?

I have a large number of aerial images. Some of them have the lens partially occluded. For example:

enter image description here

and

enter image description here

I'm trying to automatically detect which of the images have this using OpenCV. My initial though was to check how much of the image is black across multiple images. But hopefully there is a smarted way to do it for images in isolation.

like image 213
nickponline Avatar asked Aug 20 '14 03:08

nickponline


Video Answer


1 Answers

An idea is to determine how many black pixels are on the image. We can do this by creating a blank mask and then coloring all detected black pixels white on the mask using np.where. From here we can count the number of white pixels on the mask with cv2.countNonZero then calculate the pixel percentage. If the calculated percentage is greater than some threshold (say 2%) then the image is partially occluded. Here's the results:

Input image -> mask

Pixel Percentage: 3.33%
Occluded: True

Pixel Percentage: 2.54%
Occluded: True

Code

import cv2
import numpy as np

def detect_occluded(image, threshold=2):
    """Determines occlusion percentage and returns 
       True for occluded or False for not occluded"""

    # Create mask and find black pixels on image
    # Color all found pixels to white on the mask
    mask = np.zeros(image.shape, dtype=np.uint8)
    mask[np.where((image <= [15,15,15]).all(axis=2))] = [255,255,255]

    # Count number of white pixels on mask and calculate percentage
    mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    h, w = image.shape[:2]
    percentage = (cv2.countNonZero(mask)/ (w * h)) * 100
    if percentage < threshold:
        return (percentage, False)
    else:
        return (percentage, True)

image = cv2.imread('2.jpg')
percentage, occluded = detect_occluded(image)
print('Pixel Percentage: {:.2f}%'.format(percentage))
print('Occluded:', occluded)
like image 77
nathancy Avatar answered Nov 09 '22 23:11

nathancy