I used matlab code.
img = imread('cmap3.png')
map = jet(256)
ind = rgb2ind(img,map)
colormap(map)
cm = colormap('gray)
image(ind)
Through above code, I got the .
I want to save just the gray scale image without any graduations and numbers on x,y axis.
How do I remove them and save gray scale image?
If you use imwrite, you won't save the axes' labels.
For actual plots, there exists a different solutions, eg. described here: set the axis to start at the very left bottom corner so that there is no space left for descriptions: set(gca, 'Position',[0 0 1 1]). Than you can even use print to save the image/figure.
I am using the MATLAB function ginput to label my image data for further process. Here is my code:
file_name = "test.jpg";
% Read the image
img = imread(file_name);
% Get the image dimension
imgInfo = imfinfo(file_name);
width = imgInfo.Width;
height = imgInfo.Height;
% Using ginput function to label the image
figure(1);
imshow(img);
hold on;
[x, y] = ginput(4); % Manually label 4 points
scatter(x, y, 100, 'ro', 'filled'); % Plot the marked points on img
hold off;
My Problem:
I found that the output x and yare not integers, so they are not representing the pixel indices.
Sometimes, these two conditions max(x) > width and max(y) > height are satisfied. It seems to suggest that the 4 points I marked using ginput are outside the image (but actually it is not).
I am aware of this issue is related to Image Coordinate System setting, but I am still not sure how to convert x and y obtained from ginput function to the actual pixel indices?
Thanks.
The code below shows a 2x2 image, enlarges the axes so we can see it, then turns on the axes ticks and labels. What this does is allow you to see the coordinate system used to render images in an axes object.
imshow([255,0;0,255])
set(gca,'position',[0.2,0.2,0.6,0.6])
axis on
The coordinates returned by ginput match these coordinates.
In short, what you need to do is simply round the coordinates returned by ginput to get indices into the image:
[x, y] = ginput(4); % Manually label 4 points
x = round(x);
y = round(y);
I have a plot I get from matlab, with the x-axis ranging from 0 to 1864 values. I want to save this plot as a .png image without need to modify the original .fig file manually
My plot looks like this:
As you can see, the last two numbers are overlapping. The reason is that when matlab displays my image, the popup window is too small. I already tried to change the axes ratio using daspect, but it does not work.
What I think could work is to use the function truesize, my problem is that when I try to use it, I receive the following error:
No images or texturemapped surfaces in the figure.
My code looks like this:
x = rand (1864,1);
F = bar (x);
xlim ([0 1864]);
set(gca, 'XTick', sort([1864, get(gca, 'XTick')]));
truesize(1,[100 100])
Why does this happen? How can I fix this problem in order to save the images preventing x labels overlapping?
You could try rotating your x labels using the following code:
x = rand (1864,1);
F = bar (x);
xlim ([0 1864]);
set(gca, 'XTick', sort([1864, get(gca, 'XTick')]));
set(gca, 'XTickLabelRotation', 90)
Note that you can change the degrees to rotate to other number besides 90 if you wanted.
It gives you a plot that looks like this:
I'd like to take in an RGB image, find the points in the image that are white, and get the cartesian coordinates of those points in the image. I've gotten most of the way there, but when I try to plot the cartesian coordinates, I get a vertically tiled image (i.e. 5 overlapped copies of what I should see). Anyone know what could be causing this?
,
Code: (JPG comes in as 2448 x x3264 x 3 uint8)
I = imread('IMG_0245.JPG');
imshow(I); % display unaltered image
% Convert image to grayscale
I = rgb2gray(I);
% Convert image to binary (black/white)
I = im2bw(I, 0.9);
% Generate cartesian coordinates of image
imageSize = size(I);
[x, y] = meshgrid( 1:imageSize(1), 1:imageSize(2) );
PerspectiveImage = [x(:), y(:), I(:)];
% Get indices of white points only
whiteIndices = find(PerspectiveImage(:,3));
figure; plot( PerspectiveImage(whiteIndices, 1), PerspectiveImage(whiteIndices, 2),'.');
% Flip vertically to correct indexing vs. plotting issue
axis ij
Very simple. You're declaring your meshgrid wrong. It should be:
[x, y] = meshgrid( 1:imageSize(2), 1:imageSize(1) );
The first parameter denotes the horizontal extents of the 2D grid, and so you want to make this vary for as many columns as you have. Similarly, the second parameter denotes the vertical extents of the 2D grid, and so you want to make this for as many rows as you have.
I had to pre-process some of your image to get some good results because your original image had a large white border surrounding the image. I had to remove this border by removing all pure white pixels. I also read in the image directly from StackOverflow:
I = imread('http://s7.postimg.org/ovb53w4ff/Track_example.jpg');
mask = all(I == 255, 3);
I = bsxfun(#times, I, uint8(~mask));
This was the image I get after doing my pre-processing:
Once I do this and change your meshgrid call, I get this:
I have a plot in Matlab and am setting the background to transparent by:
set(gcf, 'Color', 'None');
set(gca, 'Color', 'None');
When I try to save the image (from the viewer), I save as a ".png", but it saves with a white background. How can I save it with the transparent background?
It is disappointing but, MATLAB's default saveas and print commands cannot deal with transparent things very well. You'll have to save it with some background and then convert it either through imread/imwrite or some other tool.
There are some tools that might be helpful:
Export fig http://www.mathworks.com/matlabcentral/fileexchange/23629
svg export http://www.mathworks.com/matlabcentral/fileexchange/7401-scalable-vector-graphics-svg-export-of-figures
I prefer vector graphics, so use svg exports when transparency is needed. If indeed you have a bitmap, use imwrite(bitmapData, 'a.png', 'png', 'transparency', backgroundColor).
Things have changed since the MATLAB 2014b release. The newly implemented graphics system (so called HG2, for Handle Graphics version 2) does much better in terms of transparency.
Now it saves transparency correctly to SVG at least!
So I still wanted something simple that did not require me to install anything else (corporate pc not allowed :/). I stumbled upon this link, stating:
All you have to do is the following
1) In matlab file add the commands to format your figure with transparent background
set(gcf, 'color', 'none');
set(gca, 'color', 'none');
and save or export the figure generated in eps format. (say Bspline.eps)
2) Open Bspline.eps in NotePad
3) Look at the first line. For example %!PS-Adobe-3.0 EPSF-3.0. The last number 3.0 indicates the Postscript level. For level 3, search the string rf. You will find in one line like this (four numbers followed by rf)
0 0 3025 2593 rf %Comment that line using %.
(For level 2 search for string pr instead of rf)
Save the file.
Now you can use the eps file or you can convert it to pdf and then use it.
Anyway it will have transparent background
Extra
For me it was two lines with re and two lines, despite me having %!PS-Adobe-3.0 EPSF-3.0 just after each other. But the result was the Figure was now transparent.
As an addition to the answer by Memming. Matlab 2020 with exportgraphics supports transparent background but only for vectorized output (with doesn't work with rendered content). For exporting rendered data with transparency you can still not save it with transparency. However you can get the transparency by saving rendered data with two different background colors (white and black for example) and then loading both temporary images, solving a simple equation system and thereby retrieving the transparency and the original color data and then saving that all into a RGBA png file.
The key is to realize that the saved rendered output of Matlab is transparency times image data + (1 - transparency) times background color. From a single output, image data and transparency cannot be restored but from two outputs with different background colors it can. It would be easier if Matlab would support transparent background colors in rendered outputs, but this way works too.
Example script:
% create example data
g = -10:0.1:10;
[x, y, z] = ndgrid(g, g, g);
v = (x.^2 + y.^2 - 10).^2 + (z.^2 - 5).^2;
% render in 3D with transparency
fig = figure();
% one surface fully opaque
fv = isosurface(x, y, z, v, 20);
p = patch(fv, 'FaceColor', [1, 0, 0], 'FaceAlpha', 1, 'EdgeColor', 'none');
% another surface with transparency
fv = isosurface(x, y, z, v, 80);
p = patch(fv, 'FaceColor', [0, 1, 1], 'FaceAlpha', 0.5, 'EdgeColor', 'none');
fig.Children.Color = 'none'; % transparent background of figure axis
view([40 40]);
pbaspect([1,1,1]);
camlight;
lighting('gouraud');
% save figure in different ways
% save as vector format, doesn't produce nice output see for example https://stackoverflow.com/questions/61631063
exportgraphics(fig, 'test.pdf', 'ContentType', 'vector', 'BackGroundColor', 'none');
% prints warning: Warning: Vectorized content might take a long time to create,
% or it might contain unexpected results. Set 'ContentType' to 'image' for better performance.
% save as rendered output with transparent background not work
exportgraphics(fig, 'test.png', 'ContentType', 'image', 'BackGroundColor', 'none');
% prints warning: Warning: Background transparency is not supported; using white instead.
% now our solution, export with two background colors
exportgraphics(fig, 'test1.png', 'ContentType', 'image', 'BackgroundColor', 'k'); % black background
exportgraphics(fig, 'test2.png', 'ContentType', 'image', 'BackgroundColor', 'w'); % white background
% load exported images back in and scale to [0,1]
u = imread('test1.png');
u = double(u) / 255;
v = imread('test2.png');
v = double(v) / 255;
% recover transparency as a
a = 1 - v + u;
a = mean(a, 3);
a = max(0, min(1, a));
m = a > eps;
% recover rgb
c = zeros(size(u));
for i = 1 : 3
ui = u(:, :, i);
ci = c(:, :, i);
ci(m) = ui(m) ./ a(m);
c(:, :, i) = ci;
end
c = max(0, min(1, c));
% store again
imwrite(uint8(c*255), 'test.transparent.png', 'Alpha', a);
% temporary files test1.png and test2.png can now be deleted
These are the two temporary images with white and black background.
This is the resulting transparent image. To see that save the image and look at it with a suitable viewer.
One last comment: For high quality production figures you probably want to switch off (set the Visible property to off of) all rendered parts with transparency and save axes and labels as vectorized output, then reverse the visibility, i.e. switch off all axes and labels and only show the rendered parts with transparency and save them with two different background colors, then reconstruct the transparency and overlay both, the vectorized axes and labels as well as the rendered part with transparency and combine them. As of Matlab 2020b, Matlab is not capable of doing that on its own.
You can do it like this, Matlab2020a or a later version is valid!
x = 0:.1:2*pi;
y = sin(x);
figure;
plot(x,y,'LineWidth',4);
% save to transparented image
set(gcf, 'color', 'none');
set(gca, 'color', 'none');
exportgraphics(gcf,'transparent.emf',... % since R2020a
'ContentType','vector',...
'BackgroundColor','none')
According to Matlab staff exportgraphics(gca,'plot.png','BackgroundColor','none')
and similar should work in 2020a version, but when you try, you get beautifull errors in most cases:
Warning: Background transparency is not supported; using white instead.
I also got Warning: 'ContentType' parameter is ignored when it is set to 'vector' for image output. trying xing cui code.