Visualizing depth map

Hi!

I’m having some problems with something that probably is not that hard.

I have a depth map in cv_32fc1 Mat. Each pixel is the distance in meters, 0 represents unknown values.

I’m trying to generate an image like this, white is closer and black is farther (0/unknown is also rendered as black):

I would like the range for the visual to be )0,16) meters.

I have tried normalizing to 0,255 and converting to CV_8UC1 but that did not seem to work.

This code gives me something close but it’s all black or white:
depth.convertTo(depth, CV_8UC1);
cv::equalizeHist(depth, depth);
cv::imshow(“q”, depth);
cv::waitKey(1);

Welcome,

Have you tried colormap?

Thanks!

Yes, I’ve tried normalizing and then applying the jet colormap, but the everything just turns different shades of blue. So the range seems to be incorrect. I tried to fix it by removing outliers so every pixel is in range 0,16 in the depth map. But that did not work either.

//Find automatically the max and the min
double Min, Max;
cv::minMaxLoc(input, &Min, &Max);
int max_int = ceil(Max);
//create a window complete black
cv::Mat win_mat(cv::Size(input.cols, input.rows), CV_8UC3, cv::Scalar(0, 0, 0));
input.convertTo(input, CV_8UC3, 255 / (Max - Min), -255 * Min / (Max - Min));
input.convertTo(input, CV_8UC3);
cv::Mat M;
cv::applyColorMap(input, M, COLORMAP_JET);

I think that will make the colors dispersion better.
Also if you want to change the ground color from JET to any other you can do this:

Mat mask;
inRange(dest, Vec3b(128, 0, 0), Vec3b(128, 0, 0), mask);
M.setTo(Vec3b(255, 255, 255), mask);
imshow(“ColorMap”, M);
waitKey(0);

The example above the blue ground of JET (128, 0 ,0 ) is converted to white.

depth = ... # depth in meters, 0 = invalid
valid = (depth != 0) # one shouldn't use exact equality on floats but for synthetic values it's ok
dmin, dmax = 0.0, 16.0
ranged = (depth - dmin) / (dmax - dmin) # dmin -> 0.0, dmax -> 1.0
ranged[ranged < 0] = 0 # saturate
ranged[ranged > 1] = 1
output = 1.0 - ranged # 0 -> white, 1 -> black
output[~valid] = 0 # black out invalid
output **= 1/2.2 # most picture data is gamma-compressed

(equivalent math in C++)

if you want a linear mapping. if you don’t…

you could do something like 1 / x, which has infinite range but a natural cutoff at below 1.0 distance.

image

or you can do something like x / (x+1) (vary the 1):

image

1 Like

Thanks! This works perfectly!