首页 > 解决方案 > 如何在 Matlab 应用程序设计器中使图像和绘图很好地对齐

问题描述

我正在使用 matlab 应用程序设计器构建一个演示应用程序。它有两个轴。一个显示图像。另一个显示图像的轮廓,以及轮廓的导数。

在下面的示例中,我有一个 200 列、5 行的图像。

在图像下方,我有一个图像轮廓图和一个导数图。图像配置文件(毫不奇怪)有 200 个元素。我希望 X/Y 图与图像轴一致。

有没有办法控制图像轴,与绘图轴正确对齐?
显然,我可以删除下面的轴(app.AxesImageView,'off')行,然后强制轴打开。但是,这也不起作用,因为轴标签非常不同,所以它们不对齐。我希望它能够灵活地适用于任何尺寸的图像,并且标签尺寸也会影响这一点。

TLDR:是否可以强制绘图或图像中的坐标轴驻留在坐标轴为渲染预留的区域中的特定位置?

当前用于渲染图像和绘图的代码是:

w = 5; l = 200;
parms.filterHalfWidth = 20;
img = zeros(w,l);
img(:,ceil(l/2):end)=255;
img = uint8(img);
imagesc(app.AxesImageView,img,[0,255]);
colormap(app.AxesImageView, 'gray');
axis(app.AxesImageView,'off');
title(app.AxesImageView,'Image to find edge');

profile = mean(img,2);
vals    = derivative(profile, parms.filterHalfWidth);
[~, peak] = max(vals);

plot(app.AxesProfiles, profile);
hold(app.AxesProfiles, 'all');
plot(app.AxesProfiles, parms.filterHalfWidth + (1:numel(vals)), vals);
plot(app.AxesProfiles, peak*[1,1], [0,255],'Color','Red','LineWidth',2);
hold(app.AxesProfiles, 'off');

图像和应用程序设计器的屏幕截图

标签: matlablayoutmatlab-app-designer

解决方案


我无法重现您的代码,但我用 uifigure 编写了一段代码,并且......(这是 appdesigner 中主要使用的)它是完全灵活的 ps:我不需要工具箱来计算导数,所以我使用了另一个阴谋。

f=uifigure;
f.Units = "normalized"; % essential 
f.Position= [0.3 0.2 0.5 0.7];
f.AutoResizeChildren='off'; % essential 

ax2=uiaxes(f);
ax2.Units = "normalized";
ax2.InnerPosition=[0.1 0.1 0.8 0.4]; %  set Innerposition of axes at your desired position relative to the figureto ensure axes alignment 

ax1=uiaxes(f);
ax1.Units = "normalized";
ax1.InnerPosition=[0.1 0.55 0.8 0.4];



w = 5; l = 200;
parms.filterHalfWidth = 20;
img = zeros(w,l);
img(:,ceil(l/2):end)=255;
img = uint8(img);
imagesc(ax1,img,[0,255]);
colormap(ax1, 'gray');
axis(ax1,'off');
title(ax1,'Image to find edge');

profile = mean(img,2);
vals    = derivative(profile, parms.filterHalfWidth);
[~, peak] = max(vals);

plot(ax2, profile);
hold(ax2, 'all');
plot(ax2, parms.filterHalfWidth + (1:numel(vals)), vals);
plot(ax2, peak*[1,1], [0,255],'Color','Red','LineWidth',2);
hold(ax2, 'off');

在此处输入图像描述 在此处输入图像描述


推荐阅读