Images not being displayed properly - matlab

I am working with MATLAB R2012b. I am trying to get 7 images to display on one figure but I can get the image that MATLAB to displays to look exactly like the original file. I set the color map to gray in hopes that you make it look the same but no its still different. I have included both the original and what I get from MATLAB so you can see what is happening.
Here is my code:
w8 = imread('Winter8','jpg');
subplot(2,4,1), image(w8);
title('Winter8.jpg');
axis('off','image');
colormap('gray');
truesize;
And here are the images:
Orirginal:
Result from MATLAB:
Thanks for you help.

imagesc seems to work better than image
imagesc(w8);
colormap('gray');

imagesc makes a nicer looking image in your case because you seem interested in using a gray color map as a filter. You can specify a range with clims, but you don’t have as much control as with image.
If you run colorbar on your figure, you’ll see what I’m talking about.
image would be better to use in a situation where you want much finer control over your data. For example, if you wanted to plot your data in true color instead of with a colormap, it would be easier to hack that together with the image function compared to the imagesc function because you wouldn’t be worried about scaling clims with a true color image.

Related

Are there any options that prevent anti-aliasing when saving png in Matlab?

I need to export figure from Matlab to PNG, but if the size of the matrix I display with imshow gets too "large", the output saved starts adding shades of color different from the original ones. Setting opengl hardware or painter as a renderer helps until for lower dimensions, but if the figure grows the problem is still there. I attached a sample code so you can check the output. It should have only magenta and white pixels, but when I check the resulting PNG I can see a lot of pink shades (anti-aliasing?). I don't know if it depends on the code or on the export setup.
I need 600x300 PNG.
DISCLAIMER: I'd really rather not have to use export_fig, thanks. I really appreciate your help.
brick=zeros(2300,2600,3,'uint8');
colonnebianche=randi(1550,[1000,1]);
righebianche=randi(1000,[400,1]);
brick(:,:,1)=165;
brick(righebianche,colonnebianche,1)=255;
brick(:,:,2)=35;
brick(righebianche,colonnebianche,2)=255;
brick(:,:,3)=99;
brick(righebianche,colonnebianche,3)=255;
loops=1;
for index=1:loops
filenamearray=["face",index];
loopfilename=strjoin(filenamearray,'_');
imshow(brick);
set(0, 'DefaultFigureGraphicsSmoothing', 'off')
fig=get(groot,'CurrentFigure');
fig.PaperUnits='points';
fig.PaperPosition=[0 0 288 144];
fig.GraphicsSmoothing = 'off';
axis off;
print(loopfilename,"-dpng","-painters");
%saveas(fig,loopfilename,"png")
end
You can use the Interpolation="bilinear" Name/Value as part of image display with imshow, that includes anti-aliasing as part of the rendering pipeline.
imshow(brick,Interpolation="bilinear")
Alternatively, you could consider using imresize and downsampling with whatever interpolant you want. By default, imresize uses anti-aliasing when you downsample for the "bilinear" and "bicubic" cases. If what you want is to export your image data at a particular grid size, imresize + imwrite is what I would do instead of relying on the graphics system to downsample if I understand your workflow correctly.
B = imresize(brick,[600 300]); % Bicubic + anti-aliasing on by default
imwrite(B,"yourOutputPNG.png");

How to plot an HSV image with Matlab?

I'am interested in finding a way to plot an HSV image in Matlab. I know i can do it by converting it into RGB first but I want to figure out whether there is a direct way. Thanks.
Ther is the hsv2rgb function to convert an hsv image to rgb, in case you were about to convert the values yourself.
Since monitors work with rgb the image has to be converted at some place before display, so i think it doesn't really matter when this takes place. I don't know of any more direct matlab approach than using hsv2rgb and then show the image and i do not think there is one. (of course you could put those two into a user defined script if that helps making code more readable).

MatLab reducing bitdepth in plotted images?

I am plotting the feature matches between two scenes like this:
%...import, preprocessing, and stuff
%"Sandwich" Image1 and Image2 in a new image Image
ImSize=[size(Image1,1)+size(Image2,1),max(size(Image2,2),size(Image2,2))];
Image=zeros(ImSize);
Image(1:size(Image1,1),1:size(Image1,2))=Image1;
Image(size(Image1,1)+1:end,1:size(Image2,2))=Image2;
%show Image
imshow(Image,[]);
hold on
%plot keypoints and matching lines in all colors
cc=hsv(size(Keypoints1,1));
for ii=1:size(Keypoints1,1)
plot(Keypoints1(ii,1),Keypoints1(ii,2),'o','color',cc(ii,:))
plot(Keypoints2(ii,1),Keypoints2(ii,2)+size(Image1,1),'o','color',cc(ii,:))
line([Keypoints1(ii,1),Keypoints2(ii,1)],[Keypoints1(ii,2),Keypoints2(ii,2)+size(Image1,1)],'color',cc(ii,:),'LineWidth',0.5)
end
This normaly works alright and Matlab plots the entire bitdepth
but with increasing number of lines, I will start seeing a reduction in the bitdepth leading to binary images and even all black:
I know plotting this many lines is far from ideal, but still I would like to know why this is happening. Are there any mechanisms of matlab figures I should understand to explain this behaviour?
Note: this is only a problem displaying the images, saving them as .bmp,jpg,... will produce normal pictures.
try different renderers? Add this at the beginning of your script
h=figure;
set(h,'renderer','opengl');
Instead of 'opengl', also try 'painters' and 'zbuffer'

Convert just the content of a figure to an image in MATLAB

I am currently using getframe() and frame2im in MATLAB to convert a figure of a plot to an image.
I just realized that this is working almost like a screenshot of the figure, with all the axes and labels taken into account as well.
How can I convert JUST the contents of the figure (aka the "plot") into an image?
I don't really want to save all of them to file first.
You can use the getframe / cdata idiom. What this will do is that if you call getframe on the current frame in focus without any parameters, it will return a structure to you that contains a structure element called cdata. This stores the RGB pixel array of just the figure contents themselves. The axes and labels are not captured - only what is painted onto the figure is captured.
Here's an example to get you started:
im = imread('cameraman.tif');
imshow(im);
h = getframe;
out = h.cdata;
figure;
imshow(out); %// Should give you the contents within the imshow frame.
FWIW, I also answered this same question here, though it was for a different situation: keep new image when drawing lines by dragging the mouse in matlab
As far as i know, cdata DOES NOT WORK. I had a major problem recently with the same thing - the only work around i could find is to crop each image after using getframe and cdata - this will work fine for images that are all the same size (ugly as it is - you just need to find the grey edges in the image), but if the images are all different, this wont work (well, it wont work well. there might be some way to automatically adjust the crop size)

Smoothing matlab plot figures

I have obtained the following figure using a 120x120 matrix and the surf function.
Is there a simple way to make the lines between different colors look smoother?
First of all, surf may not be the best way to display 2D-image - if you don't actually need the height information, imagesc will work just fine. Even better, it won't show the differently colored lines between hexagons, since it's not going through the colormap at intersections.
However, regardless of your approach, a low-resolution bitmap will not be automatically transformed into a "arbitrary"-resolution vector graphics - and you may not want that, anyway, if you use the figure to allow you to inspect at which combination of (x,y) you obtained at a given value.
There are three approaches to make your image prettier - (1) segment the hexagons, and use patch to create a vector-graphics image. (2) upsample the image with imresample. (3) create a RGB image and smoothen each color separately to get softer transitions:
%# assume img is your image
nColors = length(unique(img));
%# transform the image to rgb
rgb = ind2rgb((img+fliplr(img)),jet(nColors)); %# there are much better colormaps than jet
%# filter each color
for i=1:3,rgbf(:,:,i)=imfilter(rgb(:,:,i),fspecial('gaussian',9,3),'replicate');end