MATLAB: how to save a geoshow figure with faceAlpha? - matlab

I am trying to save a figure in Matlab R2014a in which I want to plot data over an Image. This is the code:
[Singapore, R] = geotiffread(file);
s = size(Singapore);
matrix = rand(s(1),s(2));
geoshow(Singapore(:,:,1:3), R)
hold on
geoshow(matrix, R, 'DisplayType', 'texturemap','facealpha',.2);
xlim([103.605,104.04])
ylim([1.2,1.475])
This one is the plot that works perfectly:
While when I am printing the figure
print(gcf, '-dpng', fullfile(FileF, 'test.png'))
the image is completely white

Many thanks for the image link!
I have tried your code (adapted for the `Singapore.tif' file you provided and an appropriate output file) and it works as expected on my system (Matlab 2013b, Linux 64-bit). This is the output file:
So I'm sorry to say that there's nothing wrong with your code, and it's probably something to do with the 'png' driver on windows or your particular installation. Have you tried printing to a different driver? (e.g. jpg or pdf?). Does it actually work if you do this from the figure's graphical menu, i.e. File->Save As; or via File->Export Setup->Export with appropriate properties?
The only other thing I can think of which may be confusing your system is the attempt to print a uint8 rgb image (your Singapore image) and an overlayed double grayscale image. You can see if converting your Singapore image to double solves this by changing:
geoshow(Singapore(:,:,1:3), R)
to
geoshow(mat2gray(Singapore(:,:,1:3)), R)
Might also be worth trying to plot the data manually and see if printing that works, e.g.:
[Singapore, R] = geotiffread('Singapore.tif');
SingaporeXYImage = cat(3, flipud(Singapore(:,:,1)), ...
flipud(Singapore(:,:,2)), ...
flipud(Singapore(:,:,3)));
s = size(Singapore);
matrix3D = repmat( rand(s(1),s(2)), [1,1,3]);
imagesc(R.LongitudeLimits, R.LatitudeLimits, mat2gray(SingaporeXYImage));
hold on;
imagesc(R.LongitudeLimits, R.LatitudeLimits, matrix3D, 'alphadata', .2);
hold off;
axis xy equal tight;
xlim([103.605,104.04])
ylim([1.2,1.475])
print(gcf, '-dpng', 'test.png');
As a bonus, here's how you might perform the same thing in Octave, in case you're interested (I find printed plots from Octave look much nicer, especially in terms of fonts!):
pkg load mapping;
pkg load image;
[SingaporeStruct, R] = rasterread('Singapore.tif');
SingaporeImage = cat(3, SingaporeStruct(1:3).data); % note this is a matrix of
% "doubles" in range [0,255]
SingaporeImage = mat2gray(SingaporeImage); % Convert to appropriate [0,1] range
% for "doubles" rgb images!
s = size (SingaporeImage);
matrix3D = repmat (rand (s(1), s(2)), [1, 1, 3]);
imagesc (R.bbox(:,1), R.bbox(:,2), ...
SingaporeImage .* 0.8 + matrix3D .* 0.2); % manually create
% transparency effect
axis xy equal tight
xlim([103.605,104.04])
ylim([1.2,1.475])
print (gcf, '-dpng', 'test.png');
Also, no disrespect to my colleague and the effort he / she put into his / her answer, but I will note that the other answer you received is essentially completely wrong and you should retract your marked accepted regardless of his / her claim and warnings about how rude it is to retract a marked answer. mapshow is specifically used for images using a MapCellsReference format: the boston.tif image is one such image. Your image however uses a GeographicCellsReference format. mapshow is used for the former, geoshow is used for the latter; geoshow would have failed for boston.tif, in the same way mapshow fails for Singapore.tif. It should have been obvious your image is a "Geo" variant because your line geoshow(Singapore(:,:,1:3), R) worked without throwing an error. Therefore the suggestion to use mapshow is not the correct answer to your question, and is misleading. Not to mention it is completely irrelevant to your actual question about why the print command does not produce the expected result from its figure handle, which should in theory have nothing to do with how the figure was produced in the first place. I would have no qualms about retracting your "accepted" mark from it. Not least because this site functions as a reference for many other viewers; it does not make sense to direct users to the wrong answer just because you got bullied into accepting it.

As suggested by mathworks, using mapshow should solve your problem. The following works for me:
[boston, R] = geotiffread('boston.tif');
figure
mapshow(boston, R);
axis image off
S = size(boston);
matrix=rand(S(1),S(2));
hold on
mapshow(matrix, R,'DisplayType','texturemap','facealpha',0.2);
print(gcf, '-dpng','test.png') ;

Related

Matlab saves figure to a very large eps file for seemingly no reason

I use LaTeX for most of my writings and always uses eps files -- that Matlab can create. There wasn't any problem but, for a new case I started studying Matlab generated a 95MB file (and eps2pdf fails to produce a pdf with that)
This question would likely be difficult to answer. It seems like an unexpected Matlab behavior but I cannot share the original code unfortunately (plus the code is very long and depends of so many variables and functions...). I have tried to build a minimum working example using built-in functions but of course it doesn't reproduce the problem. Still, I can give the structure of the code and type of objects created (see end of post): curves, surfaces, transparency, no crazy things.
This code has been running without any problem for many different situations, and gives me files of a few MB. The number of points present in one of the surfac, the approximate shape, the number of points in the curves etc. nothing changes or the changes are minute. I cannot explain the file size difference.
I would welcome either an explanation of the behavior or an alternative for getting this picture to eps. Or both of course. For now I output it to png, but I'd like vector files very much. Has anyone encountered the issue of very large eps before?
colordef white;
figure('Color','White','Name','STUFF')
hold all
% Show a curve
lpot = plot3(//STUFF\\, '-',...
'Color', [0 0 1], 'LineWidth', 2);
% Show a curve
ph = plot3(//STUFF\\,'-',...
'Color', [1 0 0], 'LineWidth', 2);
% Show a curve
plot3(//STUFF\\, '-',...
'Color', [0 1 0], 'LineWidth', 2);
% Show a point
plot3(//STUFF\\, ...
'k', 'MarkerSize', 10, 'Marker', '^', ...
'MarkerFaceColor', [1 1 0]);
% Show a surface
surf(//STUFF\\);
colormap(jet);
shading interp;
% Show a surface in transparency
surf_1 = trisurf(//STUFF\\);
set(surf_1,'FaceColor', [1 1 1], 'EdgeAlpha', 0, 'FaceAlpha', 0.5);
% Axis adjustements
axis equal tight
xlabel('stuff');
ylabel('stuff');
zlabel('stuff');
view(2);
grid on;
h_cbar = colorbar;
ylabel(h_cbar, //STUFF\\);
legend(//STUFF\\,...
{//STUFF\\});
set(gcf,'units','normalized','outerposition',[0.10 0.10 0.80 0.80])
hold off
hgexport(gcf, ['mytoobigfigure.eps'],...
hgexport('factorystyle'), 'Format', 'eps');
The code runs on matlab r2014b, windows 7.
Thanks everyone!
Yay, thanks to Peter comment, I solved the problem! The fix:
myfig = gcf; % or define myfig when the figure is created
myfig.RendererMode = 'manual' % use a set with older versions of Matlab
Which does not mean I understand why this happens and this is most likely not a desired behavior of Matlab. I think that for some reason, the renderer would switch from OpenGL to painters while saving the figure. Examining the eps, it was clear the transparency was very wrong (off in most places) and the polygon offset (or the property that allows to draw line in front of their patches) was also very poor. Whether or not a bitmap was involved, it is beyond my skills to retrieve such an information. But clearly the saved figure was not the same as the displayed figure. It should also be noted that switching the renderer to manual does not affect the picture in any other way on the screen. I wonder if setting the renderer to manual by default would not be a good idea... would there be any drawbacks to forcing OpenGl all the time?
If somebody has a more detailed answer on the topic, I'd be happy to read it.
Go to File -> Export setup -> Rendering -> Custom rendering and change the painters (vector format) to OpenGL (bitmap format) and click on "Apply to figure". Now save the figure in whatever format. Even an eps file is not bigger than a few MB.
This works for me!

how can i apply exact dct2 on 8x8 blocks and display them in subplots?

I=imread('ft.jpg');
[a b]=size(I);
figure;imshow(I);
j=rgb2ycbcr(I);
[m n]=size(j);
figure;
imshow(j);
ca=mat2cell(j,8*ones(1,size(j,1)/8),8*ones(1,size(j,2)/8),3);
p = 1;
figure;
figure;
X=ones(m,n,8,8);
for c=1:size(ca,1)
for r=1:size(ca,2)
temp=zeros(8,8,'uint8');
temp(:,:)=X(c,r,:,:);
temp=temp-128;
temp=dct2(temp);
subplot(size(ca,1),size(ca,2),temp(:,:)); %// Change
imshow(ca{c,r});
p=p+1;
end
end
the error is:
Error using ==> subplot at 309
Illegal plot number.
Error in ==> project at 22
subplot(size(ca,1),size(ca,2),temp(:,:)); %// Change
That's because you're not calling subplot properly. It needs p as the parameter for the third argument, not temp. p determines which slot you want to place your figure in. You putting in a doublevector as the third parameter makes no sense. Also, ca contains 8 x 8 pixel blocks, and you'd like to show the DCT of each block. Your current code does not do this. Actually, X is what you're trying to find the DCT of, and it's all ones.... doesn't make much sense.
You probably want to show the DCT of each block inside the figure as well, not ca.... and so you need to do this:
for c=1:size(ca,1)
for r=1:size(ca,2)
temp = double(ca{c,r}); %// Change - cast to double for precision
temp=temp-128;
temp=dct2(temp);
subplot(size(ca,1),size(ca,2),p); %// Change
imshow(temp,[]); %// Change here too
p=p+1;
end
end
Note that I did imshow(temp,[]); so that we can contrast stretch the block so that the minimum value gets mapped to black while the maximum value gets mapped to white. Everything else is a shade of gray in between. Be advised that this doesn't change the image itself. It only changes it for display.
Suggestion
You should read up on my post on how subplot works here: How does subplot work and what is the difference between subplot(121) and subplot(1,2,1) in MATLAB?
Once you read through this post, you'll have a better understanding of how each parameter works for subplot, and how you can display graphs / images in a figure.
Minor Note
Looks like you took that code from this post but you didn't copy and paste properly :) That %// Change comment, as well as the variable name ca looked oddly familiar.

Matlab remove noise

I want to remove noise from an image. The image i've been given is a .mat file but it's very complicated because when i load the mat file no image can be seen, then i use imwrite to make it jpg
imwrite(destroyedImg, 'fr.jpg');
But when i use imshow I get only colorful dots in white background!
Is there a way to find out how to clear the noise from this picture?!
I'm not allowed to use the internal functions but to build one myself! But i cannot figure out the kind of noise and then remove it! i also have to return the "clear image" in RGB format and not grayscale!
here is some of my code
clear all; close all;
load('image_destroyed.mat');
imwrite(image_destroyed, 'fraou.jpg');
img = imread('fraou.jpg');
subplot(2,2,1), imshow(img)
title('Fraou');
H = fspecial('average',[3 3]);
average = imfilter(img, H, 'replicate');
subplot(2,2,2), imshow(average);
title('average');H = fspecial('gaussian',[5 5]);
average = imfilter(img, H, 'replicate');
subplot(2,2,3); imshow(average);title('gaussian');
H = wiener2(img,[5 5]);
subplot(2,2,4); imshow(H)
title('wiener 5x5');
DO NOT USE imwrite to 'jpg' to get your image - this only introduces artifacts.
Your input image is of type double with values exceeding the range [0..1] and thus all the confusion.
load('image_destroyed.mat');
image_destroyed = image_destroyed / 255.0; % back to [0..1] range
imshow( image_destroyed ); % should be meaningful now.
The following code, presuming that image_destroyed is a variable contained in image_destroyed.mat, is probably what is causing the issue:
load('image_destroyed.mat');
imwrite(image_destroyed, 'fraou.jpg');
img = imread('fraou.jpg');
At best this is equivalent to img = image_destroyed;, and therefore is unnecessary. It is possible (and given your strange results, quite likely) that this write/read process is actually introducing further degradation into your image, through inappropriate scaling or clipping, compression, etc.
For MATLAB image processing functions, when using doubles, you should make sure your data is scaled between 0 and 1.

How to display slope on a plot in Matlab

I used the regress function to find the slope for some data I plotted. I have managed to plot the data and the fitted line both on the same plot. I know how to make it clear that the fitted line is the slope, but I would also like to add a box in corner of the graph (dont care where) that shows the actual value of the slope (basically shows the value that the regress function returns), and I'm trying to find a way to do this automatically (like if there's a function for that or something). Can anybody help (I hope I explained my question well enough...)?
I didn't try to recreate your slope line but have you considered using an annotation?
Example:
x = [-1:.2:1];
plot(x,x.^2,'-bo');
annotation('textbox', [.4 .4 .1 .1], 'String', ...
['slope at x = 0.6 is: ',num2str(2*.6)]);
Which shows:
Of course you can control how the box is positioned, formatted, and so forth.
Check the help files for more detailed info. In some cases you might also consider using a legend().
The function text adds text to a figure. It requires a position and a string to display. In addition, you can highly customize the appearance of the text. For example:
x = 1:100;
y = randn(size(x)) + 0.3*x;
plot(x,y,'.');
p = polyfit(x,y,1);
hold on;
plot(x, polyval(p,x),'k-');
h = text(min(xlim(gca)), max(ylim(gca)), ...
sprintf('%fx + %f', p(1), p(2)),...
'verticalalignment','top',...
'horizontalalignment','left');
Then, to see the various settinsg you can change, look at:
get(h)
Those properties can almost all be changes at creation (like verticalalignment above) or after creation (e.g. set(h, verticalalignment, 'top')).

MATLAB - How to avoid a jagged image?

How do I avoid a jagged image in MATLAB?
I have a 600 x 600 pixels image opened in MATLAB and do some processing on the image. However, when I save it, it looks so blurred and jagged. What should I do?
(This question is related to my previous question, MATLAB - How to plot x,y on an image and save?)
fid = fopen(datafile.txt);
A = textscan(fid,'%f%f%f'); %read data from the file
code = A{1};
xfix = A{2};
yfix = A{3};
for k=1:length(code)
imagefile=code(k);
rgb = imread([num2str(imagefile) '.jpg']);
imshow(rgb);
hold on;
x = xfix2(k);
y = yfix2(k);
plot(x,y,'-+ b'); % plot x,y on the
saveas(([num2str(imagefile) '.jpg'])) % Save the image with the same name as it open.
end
hold off
If it is just a resolution issue, perhaps using the print command (as listed below) with an explicit resolution option may fix it.
print(gcf,'-djpeg','-r600',[num2str(imagefile)])
My guess would be JPEG compression artifacts. JPEG isn't a great format for data with a lot of high frequency components. Have you tried turning the compression down? Like so:
imwrite(f.cdata,([num2str(imagefile) '.jpg']),'Quality',100);
The default for the quality parameter is only 75. That's plenty for a lot of cases, but you might need more.