使用共享的x轴自动对齐图像和图形 [英] Automatically align image and graph with shared x-axis

查看:94
本文介绍了使用共享的x轴自动对齐图像和图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张要绘制在显示该图像任意行强度的图形下的图像.

I have an image that I want to plot under a graph showing the intensity of an arbitrary row of this image.

显然,我无法自动"使两个图都对齐(它们共享相同的x轴)并且不失真.

Apparently, there is no way I can "automatically" make the two graphs both aligned (they share the same x-axis) and not distorted.

这是使用MATLAB随附的kobi.png图像的MWE.对于此解决方案,我使用了这个问题的答案,但这并不是我想要的.代码后,原因将很清楚.

Here is a MWE that uses the kobi.png image that should come with MATLAB. For this solution I used the answer to this question, but it's not exactly what I am looking for. The reason will be clear after the code.

im = imread('kobi.png'); % read default image
img = rgb2gray(im); % convert to grayscale

y = 600; % select line to "scan"

% plot image with highlithed line
subplot(3,3,4:9);
imagesc(img);
colormap gray
hold on
line([0 size(img,2)], [y y], 'Color', 'r', 'LineWidth', 1.5);
hold off
axis image

photoAxs = gca;
photoAxsRatio = get(photoAxs,'PlotBoxAspectRatio');

% plot intensity of selected row
subplot(3,3,1:3);
r = img(y, :);
plot(r);
axis tight

topAxs = gca;

% adjust ratios
topAxsRatio = photoAxsRatio;
topAxsRatio(2) = photoAxsRatio(2)/2.4; % I want to get rid of this number!  
set(topAxs,'PlotBoxAspectRatio', topAxsRatio)

如您所见,这几乎产生了预期的结果,但是有一个硬编码的数字(我链接的答案是不同的3.8,而这里是2.4),我想排除.另外,我认为这个数字只能给出一个明显对齐的解决方案,但是由于我的OCD很小,这个错误的余地让我感到毛骨悚然!

As you can see, this produces (almost) the expected result, but there is an hardcoded number (that in the answer I linked was different, 3.8, while here is 2.4), that I would like to eliminate. Also, I think this number gives only an apparently aligned solution, but with my slight OCD this room for error gives me the creeps!

所以问题是:

有什么可行的方法来自动对齐具有相同x轴的图形和图像,同时保持图像的长宽比?

Is there any viable way to automatically align a graph and an image that have the same x-axis while maintaining the image aspect ratio?

推荐答案

旧代码:

topAxsRatio = photoAxsRatio;
topAxsRatio(2) = photoAxsRatio(2)/2.4; % I want to get rid of this number!  
set(topAxs,'PlotBoxAspectRatio', topAxsRatio)

新代码:

photoratio = photoAxs.PlotBoxAspectRatio(1)/photoAxs.PlotBoxAspectRatio(2);
ratio = photoratio * photoAxs.Position(4)/topAxs.Position(4);
topAxs.PlotBoxAspectRatio = [ratio, 1, 1];

结果:

一些解释:

首次绘制图形时,您会注意到只有高度不同,尽管您可以清楚地看到宽度也不同.

When figures are first plotted, you will notice only the heights are different, although you can clearly see the widths are also different.

我不确定100%Matlab这样做的原因,但这是我的猜测.

I'm not 100% sure the reason Matlab does this, but this is my guess.

通常,宽度和高度这两个属性足以定义2D图形的大小,但是Matlab引入了一个额外的属性PlotBoxAspectRatio来控制大小.为避免冲突,Matlab决定在首次创建图形时为width属性赋予一个固定的数字.但是,实际宽度是通过height*PlotBoxAspectRatio计算的.

Usually, two properties, width and height, are sufficient to define the size of a 2D figure, but Matlab introduces an extra property, PlotBoxAspectRatio, to control the size. To avoid conflict, Matlab decides to give the width property a fixed number when a figure is first created. However, the actual width is calculated by height*PlotBoxAspectRatio.

因此,我们有:

TopAxis.width = TopAxis.height * TopAxis.ratio 
Photo.width = Photo.height * Photo.ratio

为了保留TopAxis的初始高度,我们只能更改纵横比.

In order to preserve the initial height of the TopAxis, we can only change the aspect ratio.

让我们

TopAxis.width = Photo.width

我们有

TopAxis.height * TopAxis.ratio = Photo.height * Photo.ratio
TopAixs.ratio = Photo.ratio * Photo.height / TopAxis.height

与Matlab等效的代码是提出的新代码.

And the Matlab code equivalent is the new code proposed.

这篇关于使用共享的x轴自动对齐图像和图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆