Depth Image in Matlab - matlab

I have a depth image taken from Kinect V2 which is given below. I want to extract the pixel value x at any specific coordinate and its depth value in Matlab. I used the follwing code in Matlab but it gives me 16-bit value. However, I'm not sure is it pixel value or depth value of pixel x.
im=imread('image_depth.png');
val=im(88,116);
display(val);
Result
val= (uint16) 2977
Would someone please help me that, how to extract both pixel and depth value in Matlab?

The image name hints it is a depth map. The color map is stored usually in separate file usually in different resolution and with some offset present if not aligned already. To align RGB and Depth images see:
Align already captured rgb and depth images
and the sub-link too...
The image you provided peaks with color picker the same 0x000B0B0B color for the silhouette inside. That hints it is either not Depth map or it has too low bit-width or the SO+Brownser conversion lose precision. If any pixel inside returns the same number for you too then the depth map is unusable.
In case your peaks returns 16 bit value it hints RAW Kinect depth values. If the case see:
Kinect raw depth to distance in meters
Otherwise it could be just scaled depth so you can convert your x value to depth like:
depth = a0 + x*(a1-a0)
where <a0,a1> is the depth range of the image which should be stated somewhere in your dataset source ...

From your description, and the filename, the values at each location in your image are depth values.
If you need actual color values for each pixel, they would likely be stored in a separate image file, hopefully of the same dimension.
What you are seeing here is likely the depth values normalized to a displayable region with MATLAB or your picture viewing software.
You will need to look at the specs to see how a value like 2977 converts to the physical world (e.g. cm). Hopefully it is just a scalar value you can multiply to get that answer.

Related

Reading grayscale image in matlab [duplicate]

This question already has an answer here:
What does the index refer to when selecting a pixel on an image in Matlab?
(1 answer)
Closed 6 years ago.
I have gray scale image "lena.bmp". I want read this image in matlab using imread() function.
When i use code below to read and show image my image is dark (black).
img = imread('lena.bmp');
imshow(img);
But when i use code below, I have no problem to view.
[img map]= imread('lena.bmp');
imshow(img,map);
It seems that my first code doses not reading image in grayscale mode (like what rgb2gray function generate).
My image is as follows:
What can i do to solve this problem?
Your image is an "indexed" image. That means it contains integer values which act as "labels" more than anything, and each of those labels is mapped to a colour (i.e. an rgb triplet). Your map variable represents that mapping; at row 5 you have the rgb triplet that corresponds to 'label' "5", for instance.
To see what I mean, do unique(img) and you'll see that the values of your img array are in fact quite regular. The command rgbplot can demonstrate the actual colourmap graphically. Run rgbplot(map) on your map variable to see the mapping for each of the red green and blue colours.
Now, save and read the image below on your computer as img2 and compare the array values.
This image was generated by converting from the "indexed" image you linked to, to a "grayscale" one using photoediting software (the GIMP). The difference is that
in a grayscale image, the pixel values represent actual intensities, rather than integer 'labels'. Imread reads grayscale images as uint8 images by default, meaning it assigns intensity values to pixels ranging from 0 (black) to 255 (white). Since these values happen to be integers you could still cheat and treat them as 'labels' and force a colour-mapping on them. But if you assign a 'linear map' (i.e. value 1 = intensity 1, value 2 = intensity 2, etc) then your image will look as you would expect.
You'll see that the values from unique(img2) are quite different. If you imshow(img2) you'll see this displays as you'd expect. If you don't specify a colormap for imshow, it will assume that the map is a linear mapping from the lowest to the highest value in the image array, which explains why your indexed image looked weird, since its values were never supposed to correspond to intensities.
Also try imagesc(img2) which will show this but using the "current" colormap. imagesc causes the colormap to be "scaled", so that the lowest colour goes to the lowest value in the image, and similarly for the highest.
The default colormap is jet so you should see a psychedelic looking image but you should be able to make out lena clearly. If you try colormap gray you should see the gray version again. Also try colormap hot. Now to make sense of the colormaps, try the rgbplot command on them (e.g. rgbplot(gray), rgbplot(hot) etc).
So, going back to imshow, imshow basically allows you to display an indexed image, and specify what colormap you want to use to display it. If you don't specify the colormap, it will just use a linear interpolation from the lowest value to the highest as your map. Therefore imshow(img) will show the image pretty much in the same way as imagesc(img) with a gray colormap. And since the values in your first img represent evenly spaced 'labels' rather than actual intensities, you'll get a rubbish picture out.
EDIT: If you want to convert your indexed image to a grayscale image, matlab provides the ind2gray function, e.g.:
[img, map] = imread('lena.bmp');
img_gray = ind2gray(img, map);
This is probably what you need if you mean to process pixel values as intensities.

Otsu method (graythresh function in matlab) produces a scaled result on which scale? 0:255, 0:max(px intensity), min:max?

Just clarifying a point about the Otsu thresholding method that lacks definition in the documentation & wikipedia articles. If you apply the Otsu method (in matlab the function graythresh) it returns a threshold value between 0 and 1.
Given 2 hypothetical grayscale images:
dark (with pixel intensities in the range of 0 to 100) and
light (with pixel intensities in the range of 155 to 255)
If I got an Otsu threshold of 0.75 for both dark and light images respectively, what grayscale pixel intensity would it map to in each case?
dark -> 75 and light -> 231 E.g. relative to the range of values in each image
dark -> 75 and light -> 191 E.g. relative to the range 0 to max pixel value
dark -> 191 and light -> 191 E.g. relative to the full range of grayscale pixel values (0-255)?
The accepted answer by #Ratbert makes the incorrect claims that
The correct answer is the first one
and
graythresh uses the min and max values in the image as boundaries,
which is the most logical behavior.
and rayryeng appears to agree with it. David Parks appears to have empirically verified it.
The correct answer is given by Anand which strangely seems to have a negative vote. He explains very convincingly that
full range of grayscale pixel values' depends on the data type of the input image
As he explains,
this is the third option
except for the fact that the dark image could not possibly get a threshold of 0.75.
First let us clarify the difference between the claims for the simplest case, in clear MATLAB, so there is no confusion. For an image with values ranging from min to max, the question poses three possibilities which, when translated to an equation are:
threshold = min + (max-min) * graythresh
threshold = max * graythresh
threshold = 255 * graythresh
Suppose the image consists of just two points one with an intensity of 0, and the other with 100. This means dark = uint8([0 100]);. A second image light = dark+155;. When we compute 255*graythresh(dark) we get exactly 49.5. When we compute 255*graythresh(light) we get exactly 204.5. These answers make it patently obvious that the third option is the only possibility.
There is one further fine point. If you try 255*graythresh(uint8(1:2)) the answer is 1, and not 1.5. So it appears that if you are using greythresh to threshold an image, you should use image <= 255*graythesh(image) with a less-than-or-equal-to, rather than a plain less-than.
Your third answer seems most right to me, with the clarification that 'full range of grayscale pixel values' depends on the data type of the input image. For example, for a uint8 image, an Otsu threshold of 0.75 corresponds to around 191. For a uint16 image, this would correspond to 49151.
Well, for posterity sake I did a comparison of the approaches mentioned before. I took a typical image with a full range of grayscale pixel intensities, then took a dark and light version of the same image and got the graythresh value for each. I Applied the Otsu threshold using each of the above mappings.
The light image pretty clearly demonstrates that the algorithm is producing a threshold over the range of the images' pixel intensities. Were we to assume a full range of pixel intensities, the Otsu algorithm would be producing a threshold below any actually present in the image, which doesn't make much sense, at least presuming the existing black background is transparent/irrelevant.
I suppose someone could make an argument for assuming the full range of pixel intensities if you assume the existing black part of the image were relevant darkness. I would certainly welcome comments there.
Full size images seen below
Amending my words above: When I blacken all but the top half of the light image and take the Otsu threshold again I get the same threshold value of 0.3020. Were the dark parts of the image relevant to the Otsu threshold being produced the extra darkness would have affected the value, therefore, Ratbert's answer is empirically demonstrated as correct.
The correct answer is the first one :
dark = 75 and light = 230, relative to the range of values in each image
graythresh uses the min and max values in the image as boundaries, which is the most logical behavior.

Mapping Depth onto RGB Kinect using MATLAB

I am trying to map the Depth map onto the RGB image on the Kinec using MATLAB.
So here are the steps that I took:
(1) Obtain the images using a C++ program.
(2) Using the depth value from each pixel on MATLAB, I was able to obtain the XYZ distances of all the pixels in mm.
(3) Then using some equations, I was able to obtain the XY pixel coordinates of those depth pixels on the RGB image.
So I am left with a huge cell containing all the locations of the depth map w.r.t the color image.
So my question is now if I want to overlay the depth image on the color image, how can I do that?
Can anyone help me?
Thanks;

Depth image from Kinect: Indexed image?

I am somewhat confused on how a depth image from a Kinect v1 is to be handled within MATLAB. I am using these (http://www.mathworks.com/matlabcentral/fileexchange/30242-kinect-matlab) mex-files, to read my depth images from saved *.oni files. As a result, I get images of resolution 640x480 or 320x240. The values in the images range roughly from 0-4500.
What type of image am I dealing here with now? Reading this http://www.mathworks.de/de/help/matlab/creating_plots/working-with-8-bit-and-16-bit-images.html I would assume it would have to be an indexed image, because it is no RGB image and the values are not linearly scaled. On the other hand, I believe the values in the image are actual distances to Kinect's focal plane in mm and therefore have a meaning other than an index.
When I want to look at the image using
imshow(depthMap);
I only see black. I have to use something like
imshow(depthMap, [0 9000])
to actually see something. Why exactly is that? What does imshow(depthMap) do with the values?
Do you think its correct to use depthMap as it is in my algorithms, but look at it using
imshow(depthMap, [0 9000])
?
depthMap is not an indexed image, but every pixel codes the distance from the focal plane in mm, as you correctly believe.
To show such an image using imshow, I suggest to use auto-scaling by default, i.e. imshow(depthMap,[]), or use a fixed scale (as you're currently doing) if there is a useful meaning to the minimum and maximum. Turn on the colorbar to visualize the correspondence between color and depth, either via the GUI, or by calling colorbar().
imshow with no scale argument will scale the color limits to [0 1], unless the image is of class uint8, where the color limits are set to [0 255]. In other words, with no scale argument, values of 0 and lower are mapped to black, values of 1 and higher are mapped to white. See also caxis.

Processing Kinect Depth data in MATLAB

So I used Kinect to obtain some depth images and they are now saved. So if I want to process this depth image to get the Z value (i.e. the distance from the object to the Kinect) how should I do that?
I have been doing some research online and found out that I need to be saving the image as a 16bit depth image for the depth values to be stored instead of an 8 bit depth image which can only store up to 256 values. based on: Save Kinect depth image in Matlab?
But I still do not quite understand the image I am getting. When I use imread and uses the data cursor to look at the individual pixels, I only obtain the XY coordinates and an Index value. the index value does not seem to represent the distance in mm.
Can anyone clear this part for me please.
Thanks.
looks like your are reading an indexed image with imread()
try this:
[idx, map] = imread('yourImage');
RGB = ind2rgb(idx, map);
and see if RGB contains the correct value.