如何将Matlab warp的表面贴图输出传递给export_fig?

时间:2016-10-12 15:38:26

标签: matlab matlab-figure

Warp对象位于极坐标中,我无法使用axes分配,因此我无法将曲面对象直接传递给export_fig生成图片的代码,但我无法抓住export_fig,如下所示,因为没有句柄

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

export_fig接受图形和语法处理程序,但不接受表面贴图,因此我无法将h传递给函数

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

提案失败的示例代码

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

图。 1输出数字Do not touch获取内容且主要数字因隐式声明

而中断

enter image description here

Matlab:2016a
操作系统:Debian 8.5 64位

1 个答案:

答案 0 :(得分:2)

如果我们修复它们会遇到一些问题。

  1. 由于warp不接受'Parent'属性,因此您需要确保您希望它出现的axes是当前轴。您可以使用axes函数强制执行此操作,并将轴句柄传递给它。

    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. 调用imagescplot时,使用'Parent'属性值对而不是将父级指定为第一个更清晰(并且跨系统更可靠)输入

    imagesc(Ip, 'Parent', hax_polar);
    plot(p(1:2:end), p(2:2:end), 'r+', 'Parent', hax_polar);
    hold(hax_polar, 'off')
    
  3. 一旦我们解决了所有这些问题,我们就会得到一张看似正确的图像

    enter image description here

    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));