Matlab: why is the input wrong? - matlab

I have an image that named 'binary3.tiff'.
I am asked:
"In the following function (is called func) the images are given as matrices of doubles. In those images 1 represents the object and 0 (black) represents the background."
what should the input be?
I tried:
img = imread('binary3.tiff');
img2 = double(img)/255;
newimg = func(img2);
but it doesn't work.
please help me.

Without more details about what func does and the nature of the error you're getting, I can't help you much, but you can do this instead:
img2 = double(img > 0);
to ensure that the values in the input image are binary, and give it another go. Note that instead of 0 you can, of course, put any threshold number below which it is considered "background".

The problem may be due to the fact that imread assumes tiff images use the CMYK color space instead of the RGB color space, thus making img=imread('image.tiff') a matrix whose thrid dimension has size 4, instead of 3, due to this, some functions don't work properly on img, for instance, image(img) will throw an error, this is probably why you interpret the input as being incorrect.
The format of img=imread('image.tiff') is uint8, that means every value is an integer between 0 and 255, if you want to conver them to doubles between 0 and 1 it is correct to do img2=double(img)/255 as dividing a matrix by a scalar is the same as dividing each element by that scalar.
Finally, if you are sure your image is in the RGB color space you can simply discard the 4th color layer of the matrix by doing img=imread('image.tif') and then img=img(:,:,1:3) if you do this, AND the image is indeed in RGB, commands such as image(img) will work fine.

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.

stretching histogram of image in matlab

I'm trying to implement a function imshow(img,[]), using the following formula
for each pixel:
(img(x,y)-min(img))/(max(img)-min(img))*255
But I got a different result
How can I stretch the histogram without using imshow(img,[])
tnx
code:
IAS=input('please enter image address','s');
Iimg=imread(IAS);
stimg=(Iimg-min(Iimg(:)))/(max(Iimg(:))-min(Iimg(:)))*255;
subplot(1,3,1)
imshow(stimg);
title('strechself');
subplot(1,3,2)
imshow(Iimg);
title('original image');
subplot(1,3,3)
imshow(Iimg,[])
title('imshow(img,[])');
Its probably due to the wrong use of max and min.
You are doing min(img) and that will give you an array of the minimum of each row. if you want the absolute minimum of the whole image, you should call min(img(:))
Therefore, change your line to:
img=(img-min(img(:)))/(max(img(:))-min(img(:)))*255
Note that its just 1 line. In Matlab you don't need to access each pixel (img(x,y)) and change independently as in other languages, you can do it directly.
Additionally, if img is not a uint8 , I suggest you make it (because you are using 0-255 scale)
img=uint8(img);
EDIT : Having a look to your results, very probably your original image is an uint8, therefore, before the line to strech the image, you should add the following line:
img=double(img);
So you can make divisions and keep the numbers. Else, you are performing integer division, so 34/255=0

Matlab Grayscale Normalization

I am new to matlab and to image processing, and I am having some issues normalizing but I am not sure why.
In my code I store the image as a black and white image in lim3, then:
minvalue = min(min(min(lim3)));
maxvalue = max(max(max(lim3)));
normimg = (lim3-minvalue)*255/(maxvalue-minvalue);
Unfortunately, this gives a new image that is exactly the same as lim3 at all, but I am not sure why. Ideally, I don't want to use the histeq function, so if someone could explain how to fix this code to get it to work, I would appreciate it.
All of the people above in the comments have raised very good points, but if you want a tl;dr answer, here are the most important points:
If your minimum and maximum values are 0 and 255 respectively for all colour channels, this means that the minimum and maximum colour values are black and white respectively. This is the same situation if your image was a single channel image / grayscale. As such, if you try and normalize your output image, it will look the same as you would be multiplying and dividing by the same scale. However, just as a minor note, your above code will work if your image is grayscale. I would also get rid of the superfluous nested min/max calls.
You need to make sure that your image is cast to double before you do this scaling as you will most likely generate floating point numbers. Should your scale be < 1, this will inadvertently be truncated to 0. In general, you will lose precision when you're trying to normalize the intensities as the type of the image is most likely uint8. You also need to remember to cast back to uint8 when you're done, as that is what the original type of the image was before you cast. You can do this casting, or you can use im2double as this essentially does what you want under the hood, but normalizes the image's intensities to the range of [0,1].
As such, if you really really really really... really... want to use your code above, you'd have to do something like this:
lim3 = double(lim3); %// Cast to double
minvalue = min(lim3(:)); %// Note the change here
maxvalue = max(lim3(:)); %// Got rid of superfluous nested min/max calls
normimg = uint8((lim3-minvalue)*255/(maxvalue-minvalue)); %// Cast back to uint8
This code will work if the image you are reading in is grayscale.
Bonus - For Colour Images
However, if you want to apply the above for colour images, I don't recommend you use the above approach. The reason being is you will only see a difference if the minimum and maximum values for each colour plane are the same - 0 and 255 respectively. What I would recommend you do is normalize each colour plane separately so that you'll push each colour plane to the range of [0,1], not being bound to the minimum and maximum of just one colour plane.
As such, I would recommend you do something like this:
lim3 = double(lim3); %// Cast to double
normimg = uint8(zeros(size(lim3))); %// Allocate output image
for idx = 1 : 3
chan = lim3(:,:,idx);
minvalue = min(chan(:));
maxvalue = max(chan(:));
normimg(:,:,idx) = uint8((chan-minvalue)*255/(maxvalue-minvalue)); %// Cast back to uint8
end
The above code accesses each colour plane individually, normalizes the plane, then puts the result in the output image normimg. I would recommend you use the above approach instead if you want to see any contrast differences for colour images.

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(:)));

Working with tiff's in matlab

How do find out what bit a file is? I have a tiff and convert it to class double to work with the values in the command line. When I do that though the histogram (256 bins) looks like it is binary when that isn't the case for the original file. Why is that? How do I correct that? Another odd thing is the image that I get isnt binary, you can actually see different shades of gray.
Im not sure I understood the problem but to get a detailed information of an image file, use the IMFINFO function
I assume that you converted the image to double using IM2DOUBLE. This rescales the image so that its values are distributed between 0 and 1 (for example, the pixel values of an 8-bit image would be divided by 255).
If you were to plot the histogram with hist(img(:),(0:255)/255), where img is the converted image, you'd see the 256 bins as you'd expect them.