How to pass surface map output of Matlab warp to export_fig?

人盡茶涼 提交于 2019-12-16 18:05:06

问题


Warp object is in polar coordinates which I cannot assign with axes so I cannot pass the surface object directly to export_fig. Code which generates the image but I cannot catch it for export_fig as shown below because no handle for it

clear all; close all; clc; 
img=imread('peppers.png'); 
% http://stackoverflow.com/a/7586650/54964
[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));
figure;
h=warp(x, y, z, img); 
view(2), axis square tight off

The export_fig accepts the figure and syntax handlers but not the surface map so I cannot pass h to the function

% https://github.com/altmany/export_fig
export_fig(figure_handle, filename);
export_fig(axes_handle, filename);

Example code which fails with proposals

clear all; close all; clc; 

fp=figure();
hax_polar=axes(fp);

f_do_not_touch=figure('Name', 'Do not touch'); 

index=0;
I=imread('peppers.png'); 
while index < 7
    [x, y, z]=makePolar(I);
    h=warp(x, y, z, I);
    view(2), axis square tight off
    %
    [Ip, alpha] = export_fig('/home/masi/Images/masi', '-png', '-native', '-q101', '-a1', '-m1', '-RGB', '-nofontswap',  '-transparent', '-dpng', ...
        hax_polar);
    p=FastPeakFind(Ip); % https://se.mathworks.com/matlabcentral/fileexchange/37388-fast-2d-peak-finder
    imagesc(hax_polar, Ip); hold on
    plot(hax_polar, p(1:2:end),p(2:2:end),'r+')
    hold off
    index=index+1;
end

function [x, y, z]=makePolar(img)
% http://stackoverflow.com/a/7586650/54964
[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));
end

Fig. 1 Output where the figure Do not touch gets content and the main figure breaks because implicit declarations

Matlab: 2016a
OS: Debian 8.5 64 bit


回答1:


A couple of issues that if we fix them we get what you expect.

  1. Since warp doesn't accept a 'Parent' property, you need to be sure that the axes on which you want it to appear is the current axes. You can force this by using the axes function and pass the axes handle to it.

    axes(hax_polar)
    h = warp(x, y, z, I);
    
    % Explicitly modify this axes
    view(hax_polar, 2)
    axis(hax_polar, 'square')
    axis(hax_polar, 'tight')
    axis(hax_polar, 'off')
    
  2. When calling imagesc and plot it is much clearer (and works more reliably across systems) to use the 'Parent' property value pair rather than specifying the parent as the first input.

    imagesc(Ip, 'Parent', hax_polar);
    plot(p(1:2:end), p(2:2:end), 'r+', 'Parent', hax_polar);
    hold(hax_polar, 'off')
    

Once we fix all of these things, we get an image that looks like what appears to be correct

clear all; close all; clc;

fp=figure();
hax_polar=axes('Parent', fp);

f_do_not_touch=figure('Name', 'Do not touch');

index=0;
I=imread('peppers.png');
while index < 7
    [x, y, z]=makePolar(I);

    axes(hax_polar)

    h=warp(x, y, z, I);

    % Different
    view(hax_polar, 2);
    axis(hax_polar, 'square')
    axis(hax_polar, 'tight')
    axis(hax_polar, 'off')
    %
    [Ip, alpha] = export_fig('test.png', '-png', '-native', '-q101', '-a1', '-m1', '-RGB', '-nofontswap',  '-transparent', '-dpng', ...
        hax_polar);
    p=FastPeakFind(Ip); % https://se.mathworks.com/matlabcentral/fileexchange/37388-fast-2d-peak-finder
    imagesc(Ip, 'Parent', hax_polar); hold on
    plot(p(1:2:end),p(2:2:end),'r+', 'Parent', hax_polar)
    hold(hax_polar, 'off')
    index=index+1;
end

function [x, y, z]=makePolar(img)
% http://stackoverflow.com/a/7586650/54964
[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));


来源:https://stackoverflow.com/questions/40002735/how-to-pass-surface-map-output-of-matlab-warp-to-export-fig

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