Skip to content Skip to sidebar Skip to footer

How To Mask A Depth Map To Select Darkest Values In Image?

What is My Issue? I have generated depth maps from monocular images using DenseDepth. Some of my results are below. I need help masking the darkest shades/ the darkest value of gre

Solution 1:

To better solve my issue and allow it to be generalised across a wide range of depth maps I took a different approach to reach the same objective.

Instead of selecting upper and lower colour ranges which seemed to give inconsistent results when tested on various images. I chose to use global thresholding instead.

https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html

I further adapted my code to take the average (not dominant) shade of grey aka brightness, and divided this by 2. Thus giving me the 25% darkest areas of grey being highlighted and used this as my threshold value. This effectively completed my objective.

Here is my code.

import cv2
import numpy as np

# Only for the threshold displayfrom matplotlib import pyplot as plt

# The Image to be used
image = 'DepthMap.png'# Finding the average greyscale value
image_bgr = cv2.imread(image, cv2.IMREAD_GRAYSCALE)

# Calculate the mean of each channel
channels = cv2.mean(image_bgr)
# Type Float
thresh = channels[0]/2#print (thresh)# Displaying the threshold value

img = cv2.imread(image,0)
img = cv2.medianBlur(img,5)

# If below then black else white 
ret,th1 = cv2.threshold(img,thresh,255,cv2.THRESH_BINARY)


titles = ['Original Image', 'Global Thresholding']
images = [img, th1]

for i inrange(2):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])

# Shows single image on its' own'''
plt.imshow(images[1], 'gray')
plt.xticks([]),plt.yticks([])
'''
plt.show()

DepthMap.png:

DepthMap

Output:

Global Thresholding

To prove that this works on other images I also tested the same code on another Depth Map:

Input:

Depth Map Generalisation

Output:

Depth Map Generalisation Output

Evaluation Although you can see in both images the darkest value is different, the algorithm has adapted and still works, this means that this could be used on video depth maps also and not require constant tweaking.

Post a Comment for "How To Mask A Depth Map To Select Darkest Values In Image?"