Image 删除子地块中的死区,同时保留标题&;标签
嗨,我在matlab中遇到了一个问题 我想创建一个包含10个子地块的图形。在2X5方向。但问题是我想减少它们之间的死区面积。我还需要保留标题,第一行图像使用xlabel,第二行图像使用xlabel(仅限)。不需要任何标签 到目前为止,我已经完成了这个代码(对于第一行):一旦我计算出第一行,第二行应该是小菜一碟Image 删除子地块中的死区,同时保留标题&;标签,image,matlab,plot,subplot,Image,Matlab,Plot,Subplot,嗨,我在matlab中遇到了一个问题 我想创建一个包含10个子地块的图形。在2X5方向。但问题是我想减少它们之间的死区面积。我还需要保留标题,第一行图像使用xlabel,第二行图像使用xlabel(仅限)。不需要任何标签 到目前为止,我已经完成了这个代码(对于第一行):一旦我计算出第一行,第二行应该是小菜一碟 close all figure,set(gca,'Color','none') subplot(2,5,1);subplot('Position',[0.02 0.51 0.18 0
close all
figure,set(gca,'Color','none')
subplot(2,5,1);subplot('Position',[0.02 0.51 0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,1)), [0 0], 'r','LineWidth',2);
hold off;title('1st curve','FontSize',12);xlabel('(a)','FontSize',12)
subplot(2,5,2);subplot('Position',[0.215 0.51 0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,2)), [0 0], 'r','LineWidth',2);
hold off;title('2nd curve','FontSize',12);xlabel('(b)','FontSize',12)
subplot(2,5,3);subplot('Position',[0.410 0.51 0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,3)), [0 0], 'r','LineWidth',2);
hold off;title('3rd curve','FontSize',12);xlabel('(c)','FontSize',12)
subplot(2,5,4);subplot('Position',[0.605 0.51 0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,4)), [0 0], 'r','LineWidth',2);
hold off;title('4th curve','FontSize',12);xlabel('(d)','FontSize',12)
subplot(2,5,5);subplot('Position',[0.8 0.51 0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,5)), [0 0], 'r','LineWidth',2);
hold off;title('5th curve','FontSize',12);xlabel('(e)','FontSize',12)
这给了我如下输出:
如您所见,第四个图像不存在。代码有什么问题?我已经反复检查了我的代码和计算,但没有发现任何错误
有趣的是,当我运行这段代码时[只有第四个子部分]
subplot(2,5,4);subplot('Position',[0.605 0.51 0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,4)), [0 0], 'r','LineWidth',2);
hold off;title('4th curve','FontSize',12);xlabel('(d)','FontSize',12)
我得到了这个答案!!
谁能告诉我哪里错了?注意,我确实需要xlabel和第一行图像的标题
编辑:然后我去做了这件事。现在我得到了五张图片。然而,在所有情况下,图像之间的间距并不均匀,具体而言,第一个和第二个图像之间的间距不同于其他图像
我的代码:
close all
figure,set(gca,'Color','none')
subplot(2,5,1);subplot('Position',[0.02 0.51 0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,1)), [0 0], 'r','LineWidth',2);
hold off;title('1st curve','FontSize',12);xlabel('(a)','FontSize',12)
subplot(2,5,2);subplot('Position',[0.210 0.51 0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,2)), [0 0], 'r','LineWidth',2);
hold off;title('2nd curve','FontSize',12);xlabel('(b)','FontSize',12)
subplot(2,5,3);subplot('Position',[0.405 0.51 0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,3)), [0 0], 'r','LineWidth',2);
hold off;title('3rd curve','FontSize',12);xlabel('(c)','FontSize',12)
subplot(2,5,4);subplot('Position',[0.600 0.51 0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,4)), [0 0], 'r','LineWidth',2);
hold off;title('4th curve','FontSize',12);xlabel('(d)','FontSize',12)
subplot(2,5,5);subplot('Position',[0.795 0.51 0.18 0.45]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,5)), [0 0], 'r','LineWidth',2);
hold off;title('5th curve','FontSize',12);xlabel('(e)','FontSize',12)
现在我得到了这个答案:
我怎样才能纠正这个问题?求救!提前谢谢
编辑:我似乎已经解决了这个问题。请看下面我的答案。不过,我有疑问。
我想让代码在循环中运行,因此实现了这段代码
I = imread('coins.png');
I = imresize(I,[128 128]);
for no = 1:5
subplot('Position',[0.025+0.19*(no-1) 0.51 0.19 0.48]);
imshow(I);
get(gca,'Position');set(gca, 'XTick', []);set(gca, 'YTick', []);
title({'Image' num2str(no)});
subplot('Position',[0.025+0.19*(no-1) 0.03 0.19 0.48]);
imshow(I);
get(gca,'Position');set(gca, 'XTick', []);set(gca, 'YTick', []);
title({'Image' num2str(no)});
end
答案是这样的
这个代码可以矢量化吗 Aslak Grinsted提供的
subaxis
功能为您提供了一个非常方便的解决方案。当子地块重叠时,先前的地块将隐藏
尝试稍微减小“位置”宽度
他们应该再次出现
此外,可能存在一些“快速上网”问题,
当您调整窗口大小时,这是如何表现的?全部关闭
close all
figure,set(gca,'Color','none')
subplot('Position',[0.025 0.51 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,1)), [0 0], 'r','LineWidth',2);
hold off;title('1st curve','FontSize',12);h=xlabel('(a)','FontSize',12);
s=get(h,'Position');
s(2)=s(2)-28;
set(h,'Position',s);
subplot('Position',[0.025+0.19*1 0.51 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,2)), [0 0], 'r','LineWidth',2);
hold off;title('2nd curve','FontSize',12);h=xlabel('(b)','FontSize',12);
s=get(h,'Position');
s(2)=s(2)-28;
set(h,'Position',s);
subplot('Position',[0.025+0.19*2 0.51 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,3)), [0 0], 'r','LineWidth',2);
hold off;title('3rd curve','FontSize',12);h=xlabel('(c)','FontSize',12);
s=get(h,'Position');
s(2)=s(2)-28;
set(h,'Position',s);
subplot('Position',[0.025+0.19*3 0.51 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,4)), [0 0], 'r','LineWidth',2);
hold off;title('4th curve','FontSize',12);h=xlabel('(d)','FontSize',12);
s=get(h,'Position');
s(2)=s(2)-28;
set(h,'Position',s);
subplot('Position',[0.025+0.19*4 0.51 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
imshow(I);hold on; axis off;
contour((BW(:,:,5)), [0 0], 'r','LineWidth',2);
hold off;title('5th curve','FontSize',12);h=xlabel('(e)','FontSize',12);
s=get(h,'Position');
s(2)=s(2)-28;
set(h,'Position',s);
subplot('Position',[0.025 0.03 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
seg = phi0(:,:,1)<=0;imshow(seg);
h=xlabel('(f)','FontSize',12);
s=get(h,'Position');
s(2)=s(2)-28;
set(h,'Position',s);
subplot('Position',[0.025+0.19*1 0.03 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
seg = phi0(:,:,2)<=0;imshow(seg);
h=xlabel('(g)','FontSize',12);
s=get(h,'Position');
s(2)=s(2)-28;
set(h,'Position',s);
subplot('Position',[0.025+0.19*2 0.03 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
seg = phi0(:,:,3)<=0;imshow(seg);
h=xlabel('(h)','FontSize',12);
s=get(h,'Position');
s(2)=s(2)-28;
set(h,'Position',s);
subplot('Position',[0.025+0.19*3 0.03 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
seg = phi0(:,:,4)<=0;imshow(seg);
h=xlabel('(i)','FontSize',12);
s=get(h,'Position');
s(2)=s(2)-28;
set(h,'Position',s);
subplot('Position',[0.025+0.19*4 0.03 0.19 0.48]);get(gca,'position');set(gca, 'XTick', []);set(gca, 'YTick', []);
seg = phi0(:,:,5)<=0;imshow(seg);
h=xlabel('(j)','FontSize',12);
s=get(h,'Position');
s(2)=s(2)-28;
set(h,'Position',s);
图形,集合(gca、‘颜色’、‘无’)
子批次(‘位置’,[0.025 0.51 0.19 0.48]);get(gca,“位置”);集合(gca,'XTick',[]);集合(gca,'YTick',[]);
imshow(I);等等轴断开;
轮廓((BW(:,:,1)),[0],'r','LineWidth',2);
拖延;标题(“第一条曲线”,“字体大小”,12);h=xlabel(‘(a)’,‘FontSize’,12);
s=get(h,“位置”);
s(2)=s(2)-28;
设置(h,“位置”,s);
子批次(‘位置’,[0.025+0.19*10.51 0.19 0.48]);get(gca,“位置”);集合(gca,'XTick',[]);集合(gca,'YTick',[]);
imshow(I);等等轴断开;
轮廓((BW(:,:,2)),[0],'r','LineWidth',2);
拖延;标题(“第二条曲线”,“字体大小”,12);h=xlabel(‘(b)’,‘FontSize’,12);
s=get(h,“位置”);
s(2)=s(2)-28;
设置(h,“位置”,s);
子批次(‘位置’,[0.025+0.19*20.51 0.19 0.48]);get(gca,“位置”);集合(gca,'XTick',[]);集合(gca,'YTick',[]);
imshow(I);等等轴断开;
轮廓((BW(:,:,3)),[0],'r','LineWidth',2);
拖延;标题(“第三条曲线”,“字体大小”,12);h=xlabel('c','FontSize',12);
s=get(h,“位置”);
s(2)=s(2)-28;
设置(h,“位置”,s);
子批次(‘位置’,[0.025+0.19*30.51 0.19 0.48]);get(gca,“位置”);集合(gca,'XTick',[]);集合(gca,'YTick',[]);
imshow(I);等等轴断开;
轮廓((BW(:,:,4)),[0],'r','LineWidth',2);
拖延;标题(“第四条曲线”,“字体大小”,12);h=xlabel('d','FontSize',12);
s=get(h,“位置”);
s(2)=s(2)-28;
设置(h,“位置”,s);
子批次(‘位置’,[0.025+0.19*40.51 0.19 0.48]);get(gca,“位置”);集合(gca,'XTick',[]);集合(gca,'YTick',[]);
imshow(I);等等轴断开;
轮廓((BW(:,:,5)),[0],'r','LineWidth',2);
拖延;标题(“第五条曲线”,“字体大小”,12);h=xlabel('e','FontSize',12);
s=get(h,“位置”);
s(2)=s(2)-28;
设置(h,“位置”,s);
子批次(‘位置’,[0.025 0.03 0.19 0.48]);get(gca,“位置”);集合(gca,'XTick',[]);集合(gca,'YTick',[]);
seg=phi0(:,:,1)当然可以用一个循环来完成
(使用单元格数组作为变量,使用一个单元格数组作为绘图函数)
但为了简单起见
我会把它当作两个循环
labels = {'(a)' , '(n)' , '(etc)' , 'asdf','asddf'}
jay=1
for aye =1:5
subplot('Position',[0.025+0.19*(aye-1) 0.51-0.48*(jay-1) 0.19 0.48]);
...
h=xlabel(labelsabc{aye},'FontSize',12);
....
end
内置的子地块
功能非常强大和漂亮。我认为最好还是坚持使用子批次
,而不是使用自定义的计算位置。当然,问题是子批会粘在“额外”空间中。这个空间由两个因素控制。第一个是用户控制的图形应用程序数据的SubplotDefaultAxesLocation
属性。第二个是子批
函数中的硬编码插入
。SubplotDefaultAxesLocation
属性控制图形边缘周围的空白,而inset
控制面板之间的空间。可以使用删除地物周围的空格
hfig = figure;
setappdata(hfig, 'SubplotDefaultAxesLocation', [0, 0, 1, 1])
要删除面板之间的空间,您需要在R2013a中编辑(或复制编辑)subplot.m
,有问题的行是129和130
% This is the percent offset from the subplot grid of the plotbox.
inset = [.2, .18, .04, .1]; % [left bottom right top]
如果您将第130行更改为
inset = [0.0, 0.0, 0.0, 0.0]; % [left bottom right top]
然后面板之间将没有任何空间。更好的办法是
% This is the percent offset from the subplot grid of the plotbox.
if ~isappdata(ancestorFigure, 'SubplotDefaultInset')
inset = [.2, .18, .04, .1]; % [left bottom right top]
else
inset = getappdata(ancestorFigure, 'SubplotDefaultInset');
end
因此,您可以控制插入的
。使用此稍加修改的子批次
功能
I = imread('coins.png');
I = imresize(I,[128 128]);
voffset = 0.3;
hfig = figure;
setappdata(hfig, 'SubplotDefaultAxesLocation', [0, voffset/2, 1, 1-voffset]);
setappdata(hfig, 'SubplotDefaultInset', [0, 0, 0, 0]);
nrow = 2;
ncol = 5;
hax = nan(nrow, ncol);
for irow = 1:nrow
for icol = 1:ncol
hax(irow, icol) = mysubplot(nrow, ncol, icol+(irow-1)*ncol, 'align');
imshow(I);
if irow == 1
title(hax(irow, icol), ['Curve #', num2str(icol)], 'FontSize',12);
end
if irow == nrow
xlabel(hax(irow, icol), ['(', char(96+icol), ')'], 'FontSize',12);
end
end
end
set(hax, 'XTick', [], 'YTick', []);
pos = get(hfig, 'Position');
set(hfig, 'Position', [pos(1:2), (1-voffset)*pos(3), nrow/ncol*pos(3)]);
您需要voffset
以允许标题和xlabel适合图形。您需要缩放图形,使其具有正确的纵横比来处理面板。结果是
曲线1和2以及曲线3和4之间的小间隙似乎是渲染伪影
如果要在行之间添加标签,则需要添加一些额外的空间。例如,要仅在x轴上添加标签,可以执行以下操作
voffset = 0.35;
setappdata(hfig, 'SubplotDefaultAxesLocation', [0, voffset/4, 1, 1-voffset/2]);
setappdata(hfig, 'SubplotDefaultInset', [0, voffset/2, 0, 0]);
您不必调用子批
两次。它创建第二个子地块。该帮助说明,子图('Position',positionVector)在positionVector指定的位置创建一个新轴。如果要沿X轴分布图像,可以使用linspace(0.02,0.8,5)
创建等距值的向量。可能是重复的谢谢,但我不想使用其他人的作品。你能告诉我我的代码出了什么问题吗?我能理解,但正如你所看到的,它需要相当多的努力来解决