如何遮盖深度图以选择图像中最暗的值? [英] How to mask a Depth Map to select darkest values in image?

查看:85
本文介绍了如何遮盖深度图以选择图像中最暗的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是什么?

我已经使用,然后生成一个黑白蒙版.

我尝试了什么?

经过研究,我在这里遇到了这个问题:

这是我使用的python代码:

  import cv2将numpy导入为np图片= cv2.imread('DepthMap.png')hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)模糊= cv2.medianBlur(hsv,11)较低= np.array([0,0,35])upper = np.array([0,0,44])遮罩= cv2.inRange(模糊,较低,较高)res = cv2.bitwise_and(image,image,mask = mask)cv2.imshow("mask",mask)cv2.imshow('stack',np.hstack([image,res]))cv2.waitKey(0) 

这是代码的输出:

但是,这不允许我给出两个十六进制值,然后选择介于两者之间的所有内容,但是我需要指定上限值和下限值.为此,我在这里使用了此脚本:

输出:

为了证明它可以在其他图像上使用,我还在另一个深度图上测试了相同的代码:

输入:

输出:

评估尽管您可以在两个图像中看到最暗的值是不同的,但是该算法已经过调整并且仍然有效,这意味着它也可以在视频深度图上使用,而无需进行持续的调整.

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 greys in a given range in the depth map.

I would like to be able to give two hex values ie. #6E6E6E and #000000 and for the mask to select all the values in-between and then generate a black and white mask.

What Have I Tried?

After some research I came across this here: https://medium.com/@offsouza/segmentando-objetos-pela-cor-opencv-487d5181b473 (Use on Chrome for Google Translate as it is in Spanish I think)

Here is the depth map which I tried to use in this specific example:

Here is the python code which I used:

import cv2
import numpy as np

image = cv2.imread('DepthMap.png')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
blur = cv2.medianBlur(hsv ,11)

lower = np.array([0,0,35])
upper = np.array([0,0,44])

mask = cv2.inRange(blur, lower, upper)
res = cv2.bitwise_and(image,image, mask= mask)            

cv2.imshow("mask ",mask)
cv2.imshow('stack', np.hstack([image, res]))
cv2.waitKey(0)

This is the output of the code:

This doesn't ,however, allow me to give two hex values and then select everything in-between but instead I need to specify the upper and lower values. To do this I used this script here: https://github.com/offsouza/color-segmentation/blob/master/get_color.py.

As you can probably tell, this isn't the best result- at all- to the extent that it doesn't work. I have very little knowledge in this field so I would really appreciate if you could help me!

Edit: this is a hand drawn representation of an expected outcome:

https://i.ibb.co/n8JkSf3/C051-EAE8-C114-4-E90-8938-25-A8-C79-A4-A7-E.jpg

解决方案

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 display
from 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 in range(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:

Output:

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

Input:

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.

这篇关于如何遮盖深度图以选择图像中最暗的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆