Matlab import B&W 16-bit tiff then plot? - matlab

I have a 16-bit tiff that is b&w. It has no color mapping.
I import it like the following:
Tiff = imread('MyImage.tif')
That gives me a Variable with a value 'single' named Tiff. It is just a grid/matrix of intensity values for each pixel.
I have tried then using
image(Tiff);
But I end up with an image that is all Yellow.
If I do
imagesc(Tiff);
Then it kind of works, but its not grayscale, it is more like a heat map.
How do I plot the tiff on a graph? I want to be able to then then graph other lines on top of that tiff image.

Try this,
[I,cmap] = imread('your_image.tif');
img = ind2rgb(I,cmap);
To plot something on top of your image you can do this:
figure, imshow(img);
hold on;
plot(your_x_data,your_y_data); % or whatever yo want plot on top that image.

Related

how to to make a car park shows different colors for each dots using matlab?

I convert my car park image to binary image and clear the unwanted white dots/region to get this image:
This is my codes:
sceneImage = imread('nocars10green.jpg');
figure;
imshow(sceneImage);
hsvscene = rgb2hsv (sceneImage);
figure;
imshow (hsvscene);
grayscene = rgb2gray (hsvscene);
figure;
imshow (grayscene);
bwScene = im2bw (grayscene);
figure;
imshow (bwScene);
str = strel('disk',4)
bw = imerode(bwScene,str)
figure;
imshow (bw);
How do I convert the binary image after erode so that I can show different colors for different dots?
I read in this journal.
Al-Kharusi, Hilal, and Ibrahim Al-Bahadly. "Intelligent parking management system based on image processing." World Journal of Engineering and Technology 2014 (2014).
it is mentioned:
if (newmatrix(y,x) > 0) % an object is there, if (e(newmatrix(y,x)) = 0) this object has not been seen
(newmatrix(y,x)) = x; make the value and index 3 equal to the current X coordinate.
and this is their output image:
But I don't understand how it works. Can anyone explain to me how it work and how to write the commands to convert my binary image to get the same as their output image in order to get different colors of each dots?
or if there is any other way to convert it?
The term you have to search the web for is connected component labeling or just labeling.
Given the provided image with 10 white dots on black background you can do the following:
Find the blobs:
https://en.wikipedia.org/wiki/Connected-component_labeling
https://de.mathworks.com/help/images/ref/bwlabel.html
Then color them. For example by using label2rgb:
You can display the output matrix as a pseudocolor indexed image. Each
object appears in a different color, so the objects are easier to
distinguish than in the original image. For more information, see
label2rgb.

How can I display a grayscale raster in color in Matlab?

I have a .tif file of a landmass that denotes elevation. I want to display this raster with a color ramp as opposed to a grayscale ramp. How would I do this in Matlab?
I looked at the information associated with the tiff using:
[Z, R] = geotiffread('Landmass.tif')
which denotes the heading 'ColourType' as 'grayscale'. I tried to change this to 'winter' (one of matlabs in-built color schemes) but it made no difference.
At the moment I am using the following commands to display the tiff:
[Z, R] = geotiffread('Landmass.tif');
e=uint8(Z);
mapshow(e,R);
All the higher areas are white and everything else is black...even around the landmass (which I think I may have to cut/mask the landmass out to get rid of).
All the black colour is making it too difficult for me to display other shapefiles on top of the tiff, so I want to change the color scheme from grayscale to something lighter.
How do I do this?
The reason colormap winter is not working is because the output of mapshow(e,R); is RGB image format.
Even when the displayed image is gray, it is actually RGB, when r=g=b for each pixel.
I took Matlab mapshow example, converted boston image to Grayscale, and used mapshow.
For using colormap winter, I got image using getimage, convert it to Grayscale using rgb2gray, and then colormap winter worked when showing the image.
Check the following example:
[boston, R] = geotiffread('boston.tif');
boston = rgb2gray(boston); %Convert to Grayscale for testing.
figure
mapshow(boston, R);
axis image off
%Get image data, note: size of I is 2881x4481x3 (I is not in Grayscale format).
I = getimage(gca);
%Convert I from RGB (R=G=B) formtat to Grayscale foramt, note: size of J is
%2881x4481 (J is Grayscale format).
%%%%%%%Avoid image being rotated%%%%%%%%%%%%%
%Close old image and open new figure
close Figure 1
Figure
J = rgb2gray(I);
imshow(J);
colormap winter %Now it's working...
Boston with winter colormap:

imshow() displays a white image for a grey image

I have computed an image with values between 0 and 255. When I use imageview(), the image is correctly displayed, in grey levels, but when I want to save this image or display it with imshow, I have a white image, or sometimes some black pixels here and there:
Whereas with imageview():
Can some one help me?
I think that you should use imshow(uint8(image)); on the image before displaying it.
Matlab expects images of type double to be in the 0..1 range and images that are uint8 in the 0..255 range. You can convert the range yourself (but change values in the process), do an explicit cast (and potentially loose precision) or instruct Matlab to use the minimum and maximum value found in the image matrix as the white and black value to scale to when visualising.
See the following example with an uint8 image present in Matlab:
im = imread('moon.tif');
figure; imshow(im);
figure; imshow(double(im));
figure; imshow(double(im), []);
figure; imshow(im2double(im));

Convert contour in a plot in matlab to a contour in image

I have x and y coordinates of a contour. Now they cannot be used as pixel locations since they are not integers. I am unable to find a way to convert them into pixel locations. Floor and round functions do not help as they create blobs. I need a crisp contour similar as in a plot.
You can plot your (x,y) points, remove axis ticks, and grab the axes into image. Try this:
figure;
plot(rand(10,1),rand(10,1));
box on; set(gca,'XTick',[],'YTick',[])
F = getframe(gca);
Image = F.cdata;
figure; imshow( Image );
EDIT:
If you need to draw many lines on image you may consider Bresenham's line algorithm to draw lines on image. See, for exmple, question and suggestions there:
How to draw a line on an image in matlab?
MATLAB: Drawing a line over a black and white image

Colorbar is not showing the colors I want

I have a similar question than the one in this post.
I have a grayscale image and I plot points on it. Fro plotting the points I use colormap('jet') but as I want the image to be grayscale, after plotting the points I reset the colormap, colormap('gray').
But I want to show a colorbar! And the colorbar is plotted in grayscale, not 'jet'.
How can I do that?
EDIT:
I want a Colorbar showing the color of the points!
You should convert your image to RGB by putting the same data into R-, G-, and B-channels (this will be grayscale RGB image). Colormap in MatLab is not applied to RGB images, only to indexed ones. Then plot your points over the image with colormap you like.
As discussed here, there's a few ways:
If you have the image processing toolbox, use subimage to create an independent image with a separate colormap. Then plot the image, your points, and join them into one using linkaxes.
Use freezeColors from the file exchange (or multiple colormaps, which I haven't ever tested personally). This is a very easy way to create a larger colormap, and automatically selecting the right portion of the colormap for display of images and colorbars.
As answered by anandr, convert your greyscale image to RGB; Matlab doesn't use colormaps on RGB images, which leaves you freedom to plot your points and show their colorbar independent of the image.
Example code for (3):
I = imread('cameraman.tif');
imshow(cat(3,I,I,I))
hold on
x = #() round(size(I,1) * rand(50,1));
y = #() round(size(I,2) * rand(50,1));
plot(x(), y(), 'r.')
plot(x(), y(), 'g.')
plot(x(), y(), 'b.')
colormap('jet')
colorbar
result: