Export image at full resolution with axes in MATLAB - matlab

I am trying to save a large image (1641x6139) at full resolution with MATLAB with axes. Doing this without axes is easy using the imwrite command. To plot the image with axes, I write:
image(x,y,my_image);
Because the image is very large, MATLAB automatically reduces the resolution of the image. The window size would have to be significantly larger than the screen to display the entire image at full resolution. I have tried a couple different approaches to exporting the figure and each runs into the same problem: the image is pixelated at a coarser resolution than the original image. The problem is not that the new image has fewer pixels; instead, neighboring pixels have been assigned the same value, creating the appearance of coarser-scale pixelation.
For example, where H is the figure handle, I tried:
set(H, 'PaperPosition', [0, 0, width , height ])
print -dpng -r500 my_filename
Increasing width, height, or resolution all had the effect of increasing the number of pixels in the image, but the apparent pixelation is unchanged.
I also tried using the function export_fig, which seems to be designed for this type of problem, claiming to export figures with contained images in their native resolution. The resulting image appears to have the correct number of pixels, but as before the problem is that neighboring pixels have the same value, creating the appearance of coarser pixelation absent from the original image. The code I used is:
export_fig(my_filename,'-a1','-native')
Here are a couple images showing roughly the same zoomed in portion of my image:
The first is the original image and the second is the image resulting from export_fig. The second looks pixelated, although again note that each image contains roughly the same number of actual pixels.
Any advice/solutions would be much appreciated! This problem has been much more frustrating than I expected.

I figured out a simple solution. First, I create a blank image of the correct size, add the axes, and then use export_fig to generate an image of the axes. Then, I write my full-resolution image into the image matrix, and save the resulting image using imwrite.

Related

Limit of Decreasing Plot Size in MATLAB

I use MATLAB R2021b. I have a 10x10 data array and I am using contourf function to plot the data. By default, MATLAB gives me a figure with 1120x840 pixels.
I know that I can use the command set(gcf, 'position', [700, 700, 100, 100]) to decrease the size of the figure. And I get a figure with 200x200 pixels. I decided to shrink the value in the set function by half to get a 100x100 figure.
However, when I try to do set(gcf, 'position', [700, 700, 50, 50]) , I got a figure with size 100x160. No matter how much further I decrease the value in the set function, the second dimension of the figure remained at 160 pixels. And there's no problem with the first dimension -- it can shrink with the value in the set function.
Does anyone know what's the problem here? My goal is to get a figure with 100x100 pixels.
Thank you very much for any advice!
MATLAB has always had a minimal size for figure windows. A smaller window cannot contain the minimal set of menus and toolbar buttons.
But you don’t need a small window to export a small plot.
When exporting a plot, MATLAB uses the “PaperPosition” and “PaperUnits” figure properties. Set the units to pixels and the size to your required sizes to export the figure in those sizes.
There are of course other alternatives:
Set the axes to your required sizes, then export only the axes area (or crop the produced image to size).
Export a figure at twice (or four times) the size, then resample by a factor 1/2 (or 1/4).
If you’re not exporting, and just want a small display on the screen, consider putting multiple things in the same figure window, all you need to do is control the axes’s “Position” property.
I am the original poster. I think there is a minimum window size in both Windows and Mac operating systems. 100 pixels are smaller than the minimum windows size, so I cannot generate such a small figure.
This should be the answer for my question.

Rendering fine detail when plotting Fractal sets MATLAB

I'm trying to analyse the area of a self-similar set in MATLAB. The full image is shown on the LHS below. When zooming in, one can see that MATLAB only renders the image to the scale shown in the centre image. On the RHS below is the resolution when I zoom in on the Matlab figure rather than the printed bitmap - it shows this detail that I want to render, but is not rendered when I print the figure.
I've made the plot with the viscircles function. As a side note, I'd also like to fill in the circles.
I'm using print to save save the plot as a bitmap. I've tried:
Increasing the resolution of the file I'm saving with print, but that doesn't appear to render the image in more detail but merely save the resolution-limited image with more pixels.
Increasing the size of the figure on the screen in the hope of trying to make it render the whole thing. I can't seem to make the image bigger than my monitor. Would suppressing the output of the image help?
A different approach: taking the coordinates that I was passing to viscircles and then drawing circles around them without using a MATLAB plot function. This required a loop for the 10^6 particles in my simulation and was very slow whereas the viscircles approach only took a day - I think it does plot all circles at once.

Saving Figure as High Resolution or Lossless Image that can be Zoomed in without Pixelation

I have a big plot in MATLAB (4095 points total) and I want to export this figure as a high res image which can be zoomed in just like the original figure can be zoomed in. Below I attached two images, one is full size plot and the second one was exported separately by zooming in the figure and then exporting. 1st image gets pixelated (obviously) when I zoom in. So my question is if it is possible to export the figure as an image (any format but .fig) that can be zoomed in for more granular detail.
You need to save the figure using a vector format, such as pdf, ps or eps.
For example: try
plot(sin(0:.001:2*pi)) %// example graph
The resulting figure is:
Then print to pdf:
print -dpdf example %// Or change -dpdf to -deps, -depsc, -dps, -dpsc
Now open the generated file, example.pdf. You can zoom on it and you won't see it pixelated. Here's an example of the top of the sinusoid at 6400% zoom.

matlab image blurring using fspecial

I am trying to write a matlab program for image blurring. I am required to use fspecial('average') and conv2 function. So far I have written the following code:
x=imread('ghoul.jpg');
subplot(211),imshow(x)
h=fspecial('average');
y=conv2(double(x),double(h));
subplot(212),imshow(y)
size of x is 250X250 uint8
The problem with the code is that it displays the original image fine but the image is only blurred at the bottom and white in the remaining area.
So far I have guessed that I haven't specified the size in h. But I am having problem in how to define the size in h. Whether it should be the size of x or not. It would be helpful if someone can just tell me how to write the size or another tip.
Thanks for your help.
The problem with the matlab code is that it was using imshow on double data type which caused the image intensity value to distort (barely visible or invisible in certain areas of the image). The filtered image needed rescaling of intensity values and as #eigenchris pointed out using:
imshow(y,[])
readjusted the image intensity values and the image was blurred perfectly.
side note: The size of the filter didn't had any effect on image distortion. (size is just used as a measure to how much you want to blur the image)

Can't drag 'imrect' object for high resolution images

I'm making a GUI (with GUIDE) in which there is an axis used to display image sequences. In order to let the user select a region of interest in the sequence I'm using 'imrect'. The problem is the following: everything goes fine when images are smaller than 512x512 pixels (approximately), however for larger images (I tried 600x600 and 1024x1024) the rectangle does appear, I can change its size but I can't drag it around. I though it had to be with axis units so I changed the property from 'pixels' to 'normalized' and use normalized coordinates, but it does not work.
Here is my code to create the rectangle and restrain its movement to the axis limits:
hROI = imrect(hVideo,[Width/4 Height/4 Width/2 Height/2]; % Arbitrary size and position of the rectangle, centered on the image.
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
setPositionConstraintFcn(hROI,fcn);
When I perform the same operation on those large images outside the GUI it works. Any hint is welcome!
thanks
I found a workaround to the problem, in case it can help someone:
In the call to imshow just before calling imrect, we need to specify the axis limits as the "XData" and "YData" parameters.
Example:
imshow(Movie{Frame},'parent',handles.axes1_Video,'XData',get(gca,'XLim'),'YData',get(gca,'YLim'))
It works for images up to 1024x1024.