偶尔,在Matlab中没有正确设置图形尺寸
问题描述:
我试图使用 loop在matlab中设置几个图像的相同图形尺寸并保存在png
但是其中一些(通常一个)具有不同的大小。 在下面的代码中,我尝试将图像保存在(48,64)
中。 为什么有些人物尺寸没有按照我的指示正确设置?偶尔,在Matlab中没有正确设置图形尺寸
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
答
我发现了一些技巧来解决现在的问题。
我说,
fig1 = figure(1);
drawnow
在
for
循环的前
,似乎所有的大小现在是相等的。
但还在等待更好的解决办法...
+0
这是最好的解决办法! 'drawnow'的全部目的是确保MATLAB在进行下一步操作之前可以有足够的时间进行绘图/调整大小。 –
尝试'getframe' –
前添加一个'drawnow'仍然有同样的问题... – user270700