0

What is my issue?

I am working on a project that takes an image as an input which is then converted into a depth map using DenseDepth then I want to create a mask of this depth map by taking a colour range and then generating a mask, this data is then stored into an array by splitting the mask up into an even grid of nine and if it contains over 50% masked area (black) then it is saved as a 1 in the array otherwise it is saved as a 0. I have put together a small illustration below to better explain what I would like to achieve. Where I have labelled completed this is what I have already done. As this is my first question I cannot embed an image directly so I have linked it below!

Illustration explaining my issues and what I want to achieve clearly

How have I tried to solve this?

Here is an article that I followed on Image segmentation to try to create the mask, however, I'm sure it can be improved here - this is in Spanish (I think) but on Chrome it auto-translates

Here is my code for the masking:

import cv2
import numpy as np

image = cv2.imread('Depth Map 1.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)

To find the upper and lower values I ran this code - press R to get the values.

As for dividing the masked image into a grid and then creating an 9 bit array based of this, I am completely at a loss.

If anyone is interested I generated the depth map using DenseDepth and then adjusted it to fit my needs ie. create a grey map instead and output multiple single images.

If there is any extra information that I may have left out or things that could help you help me solve the issue then I will definitely try to help

For testing purposes I have included some depth maps that I have generated:

Depth Map 1

Depth Map 2

Depth Map 3

Depth Map 4

EDITS In the code I have adjusted '6.png' to 'Depth Map 1.png' to make this clearer.

I have realised that this question would be better suited split of into two separate questions. Which I have/am asking.

Here is the first one:

How to mask a Depth Map to select grey values in-between two specified hex-codes?

However, help here would definitely still be appreciated!

Aiyush
  • 170
  • 13
  • So your question is: how to divide a depth map image (e.g. `Depth Map 1` in your question) into nine patches? Or is your question how to determine if each of the nine patches has majority black or majority white *pixels*? – wwii Jan 03 '21 at 16:40
  • @wwii essentially it is both, If you look here: https://i.stack.imgur.com/QJJBP.png I have tried to explain the issue as best as possible, firstly I need to divide into nine patches and then determine the majority black/white (depending upon how the mask is done) and then generate a nine indicie array. I hope this makes sense? – Aiyush Jan 03 '21 at 16:50
  • Your code - `cv2.imread('6.png')` - uses a *picture* on your computer which we do not have access to. Please read [mre]. Any data needed for your question should be in your question. Is `'6.png'` meant to be `Depth Map 1`? – wwii Jan 03 '21 at 16:52
  • @wwii Hi, yes, I apologise any of the depth maps that I have generated will work. I have adjusted this in the code now. EDIT: This exact image that I used when testing the code is here: https://i.ibb.co/W6hDgXz/6.png – Aiyush Jan 03 '21 at 16:53
  • Does [Extracting patches of a certain size from the image in python efficiently](https://stackoverflow.com/questions/21878868/extracting-patches-of-a-certain-size-from-the-image-in-python-efficiently) answer your question? quite a few SO Q&A's regarding operating on patches or windows of an array/image. Looks like you don't want the windows to overlap - if the image size doesn't divide evenly by 3 in either dimension, you have to decide what to do about the edges. – wwii Jan 03 '21 at 17:07
  • @wwii I don't think that it does. Did you manage to look at the illustration? I still need help with the mask as the same upper / lower on different images varies and need to find a way to account for this. So help would be much appreciated. Also, I was hoping that this is possible in Open-CV – Aiyush Jan 03 '21 at 17:12
  • 1
    when you talk about a depth map, do *not* call the depth values "color". please clarify what you call "color" in your context and why, or call it something else, such as "depth", if that is what you actually mean. your question contains multiple questions. you should split these off into separate questions. – Christoph Rackwitz Jan 03 '21 at 17:14
  • Typically we ask for only one question per question - not "help me implement this idea". If you are asking for a generalized algorithmic approach/answer to your problem you should state that and make it clear. ..... Here is another for segmenting the image: [Implement MATLAB's im2col 'sliding' in Python](https://stackoverflow.com/questions/30109068/implement-matlabs-im2col-sliding-in-python) - The user that posted the accepted answer has other sliding window/patches answers. – wwii Jan 03 '21 at 17:17
  • 1
    [“Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). – wwii Jan 03 '21 at 17:17
  • Hi all (@wwii and @Chrtistoph) for taking the time to help. I realise that this question would be better of being split into two separate ones so I will do that. Thanks Again I have asked a new question here: https://stackoverflow.com/questions/65553353/how-to-mask-a-depth-map-to-select-grey-values-in-between-two-specified-hex-codes# – Aiyush Jan 03 '21 at 20:01

0 Answers0