【问题标题】:Occasionally, figure size is not set properly in Matlab有时,Matlab 中的图形大小设置不正确
【发布时间】:2017-02-23 21:12:09
【问题描述】:

我尝试在 matlab 中使用 for 循环为多个图像设置相同的图形大小并保存在 png 但是其中一些(通常是一个)具有不同的大小。 在下面的代码中,我尝试将图像保存在 (48,64) 中。 为什么有些图形尺寸没有按照我的命令正确设置?

nMarker = 5;
mark = ['o', 's', 'd', '^', 'p'];
nSize = 3;
mSize = [9, 18, 27];

nRow = 48;
nCol = 64;
BG = zeros(nRow, nCol);


idxStage = 2;
numAction = 1;
numPositionX =  4;
numPositionY =  4;
xtrain = [1,2,3,4];
ytrain = [1,2,3,4];
xpos = [20, 30, 40, 50];
ypos = [8, 18, 28, 38];

nStepS = 10;
nStepB = 10;
nStep = nStepS + nStepB;

for a = 1
    for x = 1:numPositionX
        for y = 1:numPositionY
            for obj = 1:nMarker
                for s = 1:nSize

                    obj_command = x*1000 + y*100 + obj*10 + s;

                    fig1 = figure(1);
                    imagesc(BG)
                    hold on
                    scatter(xpos(x), ypos(y), mSize(s), mark(obj),     'k', 'filled')
                    axis off
                    set(fig1, 'Position', [500, 500, 64, 48]);
                    set(gca,'position',[0 0 1 1],'units','normalized')

                    F = getframe(gcf);
                    pause(0.05)
                    [X, Map] = frame2im(F);%   
                    tmp_frame = rgb2gray(X);

                    tmp_im_fn = sprintf('tmp/image_seq%04d.png',obj_command);
                    imwrite(tmp_frame, tmp_im_fn)   

                    clf

                end
            end
        end
    end
end 

【问题讨论】:

  • 尝试在getframe之前添加drawnow
  • 还是一样的问题...

标签: matlab


【解决方案1】:

我现在找到了一些解决问题的技巧。

我说,

fig1 = figure(1); 
drawnow

for 循环之前,现在似乎所有大小都相等。

但仍在等待更好的解决方案...

【讨论】:

  • 这是最好的解决方案! drawnow 的全部目的是确保 MATLAB 在执行后续步骤之前有足够的时间来绘制/调整大小。
猜你喜欢
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 2012-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
相关资源
最近更新 更多