Why is there Gap in this Matlab Polar presentation with export_fig?

限于喜欢 提交于 2019-12-08 05:13:19

问题


Code which yields a gap at the line from origo (0,0) to (1,0), which seems to reach zero if C dimensions reaches infinity; however, I cannot get it small enough with any sizes of C so I think the artifact can be caused by Matlab figure internal features or image data itself (AnderBiguri) because it does not seem to occur for img=imread('peppers.png'). Code which makes the image, stores it by export_fig and maps it from Cartesian to Polar where the artifact occurs

close all; clear all; clc;

%% Make image
f1=figure;
hax=axes(f1);
x = rand(1,6);
y = exp(rand(1,181));
C = rand(3613,3613);
imagesc(hax, x, y, C); 
box(hax, 'off');
axis(hax, 'off');
set(hax, 'yTickLabel', []);
set(hax, 'xTickLabel', []); % for polar presentation
set(hax, 'Ticklength', [0 0]); % http://stackoverflow.com/a/15529630/54964
filename='/home/masi/Images/testi';
% by default, export_fig has cropping
[img, alpha] = export_fig(filename, '-png', '-native', '-q101', '-a1', '-m1', '-RGB', '-nofontswap', '-dpng', hax);
close all; 

%% Read Image
img=imread('/home/masi/Images/testi.png');
% http://stackoverflow.com/a/7586650/54964
[h,w,~] = size(img);
s = min(h,w)/2; % have here .../1, some phenomenon occurs
[rho,theta] = meshgrid(linspace(0,s-1,s), linspace(0,2*pi,s));
[x,y] = pol2cart(theta, rho);
z = zeros(size(x));
subplot(121), imshow(img)
subplot(122), warp(x, y, z, img)
view(2), axis square tight off

Fig. 1 Output when size(C)=[3613 3613], Fig. 2 Output when size(C)=[36130 36130]

Matlab: 2016a
Export_fig: 08/08/16 version
OS: Debian 8.5 64 bit
Linux kernel: 4.6 of backports
Hardware: Asus Zenbook UX303UA


回答1:


I had a quick look at this, and yeah, something strange is happening. The problem is that you have a border around your image, a border that does not seem to be there when plotting the image, because its the same color as the background!

If you create your image with

C = rand(100,100);

and then plot it after loading it with imagesc instead of imshow you can see this:

That is what it gets translated to an band missing in the polar plot. If you just remove the border around the image of size 1, you'll get a full plot.

My best guess is that somewhere inside export_fig either your statement % by default, export_fig has cropping is false or the cropping has some minor bug and takes 1 pixel from the background.




回答2:


From the original wording, I understood your question to be:

  1. I'm trying to create a polar transformation (warp) from an existing image (i.e. image data).
  2. I export the image using export_fig. I'm not interested in axes or borders, just the image data, so I'm passing appropriate options to get rid of these.
  3. When I read the resulting file in at a later step, and try to create my polar transformation, I get a weird artifact.
  4. Why does this happen, and how can I get rid of it to have a clean polar plot.

If this is actually the question, then I suggest you don't use export_fig for this, because it's not designed to save image data, it saves a figure. That is, it's a wrapper for the print function with lots of customisations, producing something that you'll use in a publication. It applies appropriate borders, resolution, retains axes, grids, etc etc. And in this particular case with your options, it seems to add a white border of 1-pixel thickness at the bottom of your output image. (yes, this may or may not be a bug and it's worth reporting to the author).

Therefore, if what you're actually trying to do is save the image data instead (i.e. raw pixel rgb values) you should be using imwrite instead. Full example, adapted from your code:

close all; clear all; clc;

%% Make image
f1 = figure; hax = axes();
x = rand(1,6); y = exp(rand(1,181)); % image placement
C = rand(3613,3613);                 % image data
imagesc(x, y, C); 
colormap parula(256); % The colormap in the example (default since 2014b)
box(hax, 'off'); axis(hax, 'off');
set(hax, 'yTickLabel', [], 'xTickLabel', [] , 'Ticklength', [0 0]);
filename='testi.png';

% convert grayscale image to rgb to preserve "colormap" in output file
Cind = gray2ind(C, 256); Crgb = ind2rgb(Cind, parula(256));
imwrite(Crgb, filename, 'png');

%% Read image
img=imread('testi.png');
[h,w,~] = size(img); s = min(h,w)/2;
[rho,theta] = meshgrid(linspace(0,s-1,s), linspace(0,2*pi,s));
[x,y] = pol2cart(theta, rho); z = zeros(size(x));
subplot(1, 2, 1); imagesc(img); axis image off;
subplot(1, 2, 2); warp(x, y, z, img); view(2); axis image off;
colormap parula(256);

Resulting in:

HOWEVER

The question is badly worded. It seems to me after lots of unfortunate heated comments and edits to the original question, that the actual question now seems to be:

  1. I'm using the export_fig function specifically (for reasons I don't want to go into) and I'm trying to use it to export just the image data
  2. The output image has a weird "1-pixel thick bottom border" artefact.
  3. I can demonstrate this visually more clearly to you guys buy transforming this into a polar plot (but the polar plot itself isn't something I care about for my publication).
  4. Is this a bug in export_fig, should I report it to the author, is there a way to get around it?

In which case the answer is "yes this is totally a bug and you should report it".


(Furthermore, if this really is the version of the question you are asking, please make the extra effort to phrase your questions more clearly in the future stating clearly the problem you're trying to solve and steps taken, to avoid wasting people's time answering the wrong thing, and getting into heated discussions over nothing.)



来源:https://stackoverflow.com/questions/39963795/why-is-there-gap-in-this-matlab-polar-presentation-with-export-fig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!