Matlab figure flashes back and forth between black&white and color - matlab

I'm trying to display a color disparity map, but it first displays black and white until I do 'colormap jet' then it's in color.
How can black and white image be suppressed?
imshow(disparityMap, DisparityRange );
title('DISPARITY MAP');
colormap jet;

Specify the colormap parameter to imshow.
imshow(disparityMap, DisparityRange, 'colormap', jet);
Example use
im = imread('cameraman.tif');
imshow(im, [60,200], 'colormap', hsv);
We get this image:

Related

How to change a gray scale image to red colored image?

I have an image of a thick wave which changes from black to white smoothly like the black slowly changes from dark to white. How can I change this image to red instead of having different shades of black I need different shades of red going from dark red to white.
Create an array of size (rows,cols,3) where (rows,cols) is the size of gray image. Make its red channel equal to 255 and other channels equal to gray image
img = imread('moon.tif');
[r,c,z] = size(img);
red_img = uint8(zeros(r,c,3));
red_img(:,:,1) = 255;
red_img(:,:,2) = img;
red_img(:,:,3) = img;
subplot(1,2,1);
imshow(img,[]);
subplot(1,2,2);
imshow(red_img,[]);

Can we rotate an image in MATLAB filled with background color of original image?

By default, MATLAB function imrotate rotate image with black color filled in rotated portion. See this, http://in.mathworks.com/help/examples/images_product/RotationFitgeotransExample_02.png
We can have rotated image with white background also.
Question is, Can we rotate an image (with or without using imrotate) filled with background of original image?
Specific to my problem: Colored image with very small angle of rotation (<=5 deg.)
Here's a naive approach, where we simply apply the same rotation to a mask and take only the parts of the rotated image, that correspond to the transformed mask. Then we just superimpose these pixels on the original image.
I ignore possible blending on the boundary.
A = imread('cameraman.tif');
angle = 10;
T = #(I) imrotate(I,angle,'bilinear','crop');
%// Apply transformation
TA = T(A);
mask = T(ones(size(A)))==1;
A(mask) = TA(mask);
%%// Show image
imshow(A);
You can use padarray() function with 'replicate' and 'both' option to interpolate your image. Then you can use imrotate() function.
In the code below, I've used ceil(size(im)/2) as pad size; but you may want bigger pad size to eliminate the black part. Also I've used s and S( writing imR(S(1)-s(1):S(1)+s(1), S(2)-s(2):S(2)+s(2), :)) to crop the image where you can extract bigger part of image just expanding boundary of index I used below for imR.
Try this:
im = imread('cameraman.tif'); %// You can also read a color image
s = ceil(size(im)/2);
imP = padarray(im, s(1:2), 'replicate', 'both');
imR = imrotate(imP, 45);
S = ceil(size(imR)/2);
imF = imR(S(1)-s(1):S(1)+s(1)-1, S(2)-s(2):S(2)+s(2)-1, :); %// Final form
figure,
subplot(1, 2, 1)
imshow(im);
title('Original Image')
subplot(1, 2, 2)
imshow(imF);
title('Rotated Image')
This gives the output below:
Not so good but better than black thing..

Colouring specific pixels in an image

Say I have an image. How can I colour some specific pixels in that image using MATLAB?
Thanks.
RGB Pixels
I'd suggest working with an RGB image, so that you can easily represent color and gray pixels. Here's an example of making two red blocks on an image:
img = imread('moon.tif');
imgRGB = repmat(img,[1 1 3]);
% get a mask of the pixels you want and set an RGB vector to those pixels...
colorMask = false(size(imgRGB,1),size(imgRGB,2));
colorMask(251:300,151:200,:) = true; % two discontiguous blocks
colorMask(50:100,50:100,:) = true;
redPix = permute([255 0 0],[1 3 2]);
imgRGB(repmat(colorMask,[1 1 3])) = repmat(redPix, numel(find(colorMask)),1);
AlphaData image property
Another cool way of doing this is with an image's AlphaData property. See this example on a MathWorks blog. This essentially turns color on or off in certain parts of the image by making the gray image covering the color image transparent. To work with a gray image, do like the following:
img = imread('moon.tif');
influenceImg = abs(randn(size(img)));
influenceImg = influenceImg / (2*max(influenceImg(:)));
imshow(img, 'InitialMag', 'fit'); hold on
green = cat(3, zeros(size(img)), ones(size(img)), zeros(size(img)));
h = imshow(green); hold off
set(h, 'AlphaData', influenceImg)
See the second example at the MathWorks link.

Remove text Backgroundcolor in matlab plot

I have added some text in my plot. But I want to move its background to be as all other background which is gray. I know the following:
'Backgroundcolor',[0.7 0.7 0.7]
However, [0.7 0.7 0.7] gives color different than the backgroud. How can I make it same ?!
As #FranckDernoncourt said, background colour is changed to white when you export the figure.
However, you can use this to change the colour of the x-axis to be the same as the background colour of the figure:
h_fig1 = figure;
x = [1:4]; y = [2:2:8];
plot(x,y);
set(gca,'xcolor',get(h_fig1,'color'))
Replace 'xcolor' with 'ycolor' to change the colour of the y-axis.
Alternatively, if it is the axis label you want to change the colour of:
h_fig1 = figure;
x = [1:4]; y = [2:2:8];
plot(x,y);
h_2 = xlabel('x label');
set(h_2,'color',get(h_fig1,'color'))
(Though why you would want to do this, I'm not sure. You might as well just not set the xlabel property.)
Or if it is text you added to the plot, this will set the text colour to the background colour of the figure:
h_fig1 = figure;
x = [1:4]; y = [2:2:8];
plot(x,y);
h_3 = text(x(2),y(2),'string')
set(h_3,'color',get(h_fig1,'color'))

creating 3D image of png files with transparent background using surf

Problem : The image loses its transparency when plot using surf
I have already figured out how to create a png file with a transparent background as described in numerous other threads.here
However, when plot using surf.m, the image is not transparent
Here is the code that I have so far:
img = imread('image.png');
A1 = ones(size(img));A2 = ones(size(img));A3 = ones(size(img));
A1(img(:,:,1)==0)=0;A2(img(:,:,2)==0)=0;A3(img(:,:,3)==0)=0;
A = A1+A2+A3;
A= A(:,:,1);
imwrite(img,'test.png','alpha',A);
[img,map,alpha] = imread('test.png');
ximage = [-.5,.5;-.5,.5];
yimage = [0,0;0,0];
zimage = [.5,.5;-.5,-.5];
surf(ximage,yimage,zimage,'Cdata',img,'Facecolor','texturemap','Edgecolor','none','alphadata',alpha);
axis vis3d
The code converts image.png (blue square) to a test.png with a transparent background (get rid of the black background). test.png is then used to generate a surf plot which turns out to be not transparent. Any idea what I am doing wrong?
There is a function called alpha in MATLAB which sets transparency for the objects in current axes. I suggest to change the variable named alpha to another name by replacing the following line
[img,map,alpha] = imread('test.png');
to
[img,map,alphaChannel] = imread('test.png');
Now, after running surf, you can set the transparency for your plot through alpha function.
Using alpha function
surf(ximage,yimage,zimage,'Cdata',img,'Facecolor','texturemap','Edgecolor', ...
'none','alphadata',alpha);
alpha(0.5); %# line added
axis vis3d
Using surf function
If you want to set the transparency through the surf function, you need to add 'FaceAlpha' parameter:
surf(ximage,yimage,zimage,'Cdata',img,'Facecolor','texturemap','Edgecolor', ...
'none','AlphaData',alphaChannel,'FaceAlpha',0.5);
Result
More information about alpha and surf functions.
Setting a matrix transparency
But those functions above sets the transparency for the entire plot. If you want to set your original matrix of transparency, you need to pass 'FaceAlpha','texture' parameter to surf:
handler = surf( ximage , yimage , zimage , 'Cdata', img , ...
'FaceColor','texturemap', ...
'EdgeColor','none', ...
'FaceAlpha','texture', ...
'AlphaData', alphaChannel);
axis vis3d
Result
More details. Hope it helps!