What does `imshow(someImage, [])` do? - matlab

I am trying to figure out what the second (empty vector) parameter in imshow(someImage, []) in Matlab is for.
According to doc imshow, it's either a color map or a range of width/height, but neither of these make sense to me since the vector is empty.

With the empty bracket imshow will display the range between the minimum and maximum value. For example, if your image is 16bits, the maximum value is 65536, but if your actual pixel values stop at 1000, imshow(image) will seem black (because even 1000 over 65536 is small). If you use imshow(image, []), then the display will be adjust between 0 and 1000.
It is the same as:
minValue = min(min(image));
maxValue = max(max(image));
imshow(image,[minValue maxValue]);

The documentation from help imshow describes this syntax:
imshow(I,[LOW HIGH]) displays the grayscale image I, specifying the display
range for I in [LOW HIGH]. The value LOW (and any value less than LOW)
displays as black, the value HIGH (and any value greater than HIGH) displays
as white. Values in between are displayed as intermediate shades of gray,
using the default number of gray levels. If you use an empty matrix ([]) for
[LOW HIGH], imshow uses [min(I(:)) max(I(:))]; that is, the minimum value in
I is displayed as black, and the maximum value is displayed as white.

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.

Difference between imshow(pic) and imshow(pic,[])

I have a very brief question on imshow in matlab.
what is the difference between imshow(pic) and imshow(pic,[])?
when should I use imshow(pic,[])?
From Mathwork's website (see here in the DisplayRange section):
Display range of a grayscale image, specified as a two-element vector [LOW HIGH]. imshow displays the value low (and any value less than low) as black, and the value high (and any value greater than high) as white. Values in between are displayed as intermediate shades of gray, using the default number of gray levels. If you specify an empty matrix ([]), imshow uses [min(I(:)) max(I(:))]; that is, use the minimum value in I as black, and the maximum value as white.
Suppose, you have an image which has the lowest value of 230 and highest value of 255, and if you do imshow(img), it will assume that the lowest of the displayed image is 0 and maximum is 255, so you end up with an (almost) white image. On the other hand, if you do imshow(img,[]), you tell MATLAB adjust the minimum and maximum of the displayed image according to the minimum and maximum values of the image. Therefore, in the above example, calling imshow(img,[]) is equivalent to calling imshow(img,[230 255]). This will enable you to see the intermediate shades of gray.

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.

matlab rgb values dilemma

When i wrote these commands
out = ones(size(ben))
imshow(out)
the output is a white picture but i expect almost dark picture because the rgb values are 1,1,1. when i give 255,255,255 it also gives a white picture. Isn't this a dilemma ?
Try out = ones(size(ben), 'uint8');
ones() by default creates an array of doubles. When imshow() gets an array of doubles it assumes that the pixel values range between 0 and 1, and assigns the white color to anything greater than 1. However, if you pass an array of uint8 to imshow() it will assume the range to be between 0 and 255.
You can also try using imagesc(); instead of imshow(), but you may need to do colormap gray after wards to get a grayscale image.
Another alternative is to rescale the image before display:
imshow(out / max(out(:)));