自动将图像和图形与共享的 x 轴对齐
Automatically align image and graph with shared x-axis
我有一张图像,我想在显示该图像任意行强度的图表下绘制。
显然,我无法 "automatically" 使这两个图既 对齐 (它们共享相同的 x 轴)又 不对齐扭曲.
这是一个 MWE,它使用 MATLAB 应该附带的 kobi.png
图像。对于此解决方案,我使用了 this question 的答案,但这并不是我要找的。原因看代码就明白了
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
) ,我想消除。另外,我认为这个数字只给出了一个 显然 对齐的解决方案,但由于我有轻微的强迫症,这个错误空间让我毛骨悚然!
所以问题是:
有没有可行的方法自动对齐具有相同 x 轴的图形和图像,同时保持图像纵横比?
旧代码:
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];
结果:
一点解释:
当第一次绘制图形时,您会注意到只有高度不同,虽然您可以清楚地看到宽度也不同。
我不是 100% 确定 Matlab 这样做的原因,但这是我的猜测。
通常,width 和 height 两个属性足以定义二维图形的大小,但 Matlab 引入了额外的 属性、PlotBoxAspectRatio
来控制大小。为了避免冲突,Matlab 决定在第一次创建图形时给宽度 属性 一个固定的数字。但是,实际宽度是按height*PlotBoxAspectRatio
.
计算的
因此,我们有:
TopAxis.width = TopAxis.height * TopAxis.ratio
Photo.width = Photo.height * Photo.ratio
为了保持TopAxis的初始高度,我们只能改变纵横比。
让
TopAxis.width = Photo.width
我们有
TopAxis.height * TopAxis.ratio = Photo.height * Photo.ratio
TopAixs.ratio = Photo.ratio * Photo.height / TopAxis.height
而 Matlab 等效代码是提议的新代码。
我有一张图像,我想在显示该图像任意行强度的图表下绘制。
显然,我无法 "automatically" 使这两个图既 对齐 (它们共享相同的 x 轴)又 不对齐扭曲.
这是一个 MWE,它使用 MATLAB 应该附带的 kobi.png
图像。对于此解决方案,我使用了 this question 的答案,但这并不是我要找的。原因看代码就明白了
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
) ,我想消除。另外,我认为这个数字只给出了一个 显然 对齐的解决方案,但由于我有轻微的强迫症,这个错误空间让我毛骨悚然!
所以问题是:
有没有可行的方法自动对齐具有相同 x 轴的图形和图像,同时保持图像纵横比?
旧代码:
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];
结果:
一点解释:
当第一次绘制图形时,您会注意到只有高度不同,虽然您可以清楚地看到宽度也不同。
我不是 100% 确定 Matlab 这样做的原因,但这是我的猜测。
通常,width 和 height 两个属性足以定义二维图形的大小,但 Matlab 引入了额外的 属性、PlotBoxAspectRatio
来控制大小。为了避免冲突,Matlab 决定在第一次创建图形时给宽度 属性 一个固定的数字。但是,实际宽度是按height*PlotBoxAspectRatio
.
因此,我们有:
TopAxis.width = TopAxis.height * TopAxis.ratio
Photo.width = Photo.height * Photo.ratio
为了保持TopAxis的初始高度,我们只能改变纵横比。
让
TopAxis.width = Photo.width
我们有
TopAxis.height * TopAxis.ratio = Photo.height * Photo.ratio
TopAixs.ratio = Photo.ratio * Photo.height / TopAxis.height
而 Matlab 等效代码是提议的新代码。