Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

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

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

enter image description here

Matlab: 2016a
OS: Debian 8.5 64 bit

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...