带有意外翻转的 Matlab 图像和绘图
Matlab Image and Plot with Unexpected Flip
在下面的例子中,每种情况下都有一些明显的代码差异,但我不明白它们将如何改变图像或绘图的方向,如每种情况所示。
原图尺寸为 407 x 813 x 3。
轮廓中的数据,X取值范围-180到+175,Y取值范围-85到+85
对于图像,我正在尝试调整它,使 X 的范围从 -180 到 +180,Y 的范围从 -90 到 +90。所以图像覆盖的地理范围比图像略宽。
我有一些地理数据的等高线图。它是这样绘制的,看起来像我期望的那样:
绘制此外观的代码是
figure (1);
contour(X, Y, dec0Mat,contoursAt, 'ShowText','on', 'LineWidth', 2);
colormap('Gray');
我会自己画图,看起来还可以:
使用此代码:
图(99)
imagesc([-180 180], [-90 90], worldMap);
但是当我尝试将两者结合起来时,我得到了这个:
使用此代码:
figure (1);
image([-180 180], [-90 90], worldMap);
hold on
title('Declination at elev = 0 km');
contour(X, Y, dec0Mat,contoursAt, 'ShowText','on', 'LineWidth', 2);
colormap('Gray');
在这种情况下,地图看起来是正确的,但等高线是垂直翻转的。
在另一种情况下,我得到这个:
使用此代码:
figure (3);
hold on
title('Declination at elev = 30 km');
imagesc([-180 180], [-90 90], worldMap);
contour(X, Y, dec30Mat,contoursAt, 'ShowText','on', 'LineWidth', 2);
colormap('Gray');
在这种情况下,地图垂直翻转,等高线绘制在右侧。
根据 imagesc 的 Matlab 文档,它说:
imagesc(x,y,C) displays C as an image and specifies the bounds of the
x- and y-axis with vectors x and y. If x(1) > x(2) or y(1) > y(2), the
image is flipped left-right or up-down, respectively. If x and y are
scalars, the image is translated to the specified location (x,y) such
that the upper left corner of the image starts at (x,y).
我强调了可能与翻转图像相关的内容,但这里似乎都不是这种情况,也无法解释在一种情况下翻转而不是另一种情况下翻转的不一致。
这不完全是旋转的问题,更多的是垂直翻转的问题。
如果您仔细查看前两个图,您会发现垂直比例发生了翻转,因此如果您直接将两个图合并(无论如何),您最终会得到您观察到的结果,即一个图相对于另一个图翻转。
我建议在叠加之前翻转等高线图:
hold on
image([-180 180], [-90 90], worldMap);
title('Declination at elev = 0 km');
contour(X, Y, flipud(dec0Mat), contoursAt, 'ShowText','on', 'LineWidth', 2);
colormap('Gray');
或
hold on
image([-180 180], [-90 90], worldMap);
title('Declination at elev = 0 km');
contour(X, -Y, dec0Mat, contoursAt, 'ShowText','on', 'LineWidth', 2);
colormap('Gray');
最佳,
解释是imagesc
命令和image
一样,设置'Ydir'
轴属性为'reverse'
。
When called with C
or X
,Y
,C
, image
sets the axes limits to tightly enclose the image, sets the axes YDir
property to 'reverse'
, and sets the axes View
property to [0 90]
.
这意味着垂直轴值从轴的顶部到底部增加。
现在你可以比较两种情况:
如果你运行 contourf
命令本身,你有 "normal" 轴模式,垂直轴值从轴的底部增加到顶端。您第一个图中的垂直轴标签反映了这一点。
如果在同一张图上绘制imagesc
和运行contour
的图像,则纵轴先翻转imagesc
。随后的 contour
命令在翻转的垂直轴上运行,并相应地绘制。这就是等高线相对于情况 1 垂直翻转的原因。
请注意,案例2得到的组合图是正确的。如果您 "visually" 组合了问题的前两张图片(分别通过调用 imagesc
和 countour
获得),那将是错误的,因为它们具有不同的垂直轴。
在下面的例子中,每种情况下都有一些明显的代码差异,但我不明白它们将如何改变图像或绘图的方向,如每种情况所示。
原图尺寸为 407 x 813 x 3。
轮廓中的数据,X取值范围-180到+175,Y取值范围-85到+85
对于图像,我正在尝试调整它,使 X 的范围从 -180 到 +180,Y 的范围从 -90 到 +90。所以图像覆盖的地理范围比图像略宽。
我有一些地理数据的等高线图。它是这样绘制的,看起来像我期望的那样:
绘制此外观的代码是
figure (1);
contour(X, Y, dec0Mat,contoursAt, 'ShowText','on', 'LineWidth', 2);
colormap('Gray');
我会自己画图,看起来还可以:
使用此代码: 图(99) imagesc([-180 180], [-90 90], worldMap);
但是当我尝试将两者结合起来时,我得到了这个:
使用此代码:
figure (1);
image([-180 180], [-90 90], worldMap);
hold on
title('Declination at elev = 0 km');
contour(X, Y, dec0Mat,contoursAt, 'ShowText','on', 'LineWidth', 2);
colormap('Gray');
在这种情况下,地图看起来是正确的,但等高线是垂直翻转的。
在另一种情况下,我得到这个:
使用此代码:
figure (3);
hold on
title('Declination at elev = 30 km');
imagesc([-180 180], [-90 90], worldMap);
contour(X, Y, dec30Mat,contoursAt, 'ShowText','on', 'LineWidth', 2);
colormap('Gray');
在这种情况下,地图垂直翻转,等高线绘制在右侧。
根据 imagesc 的 Matlab 文档,它说:
imagesc(x,y,C) displays C as an image and specifies the bounds of the x- and y-axis with vectors x and y. If x(1) > x(2) or y(1) > y(2), the image is flipped left-right or up-down, respectively. If x and y are scalars, the image is translated to the specified location (x,y) such that the upper left corner of the image starts at (x,y).
我强调了可能与翻转图像相关的内容,但这里似乎都不是这种情况,也无法解释在一种情况下翻转而不是另一种情况下翻转的不一致。
这不完全是旋转的问题,更多的是垂直翻转的问题。
如果您仔细查看前两个图,您会发现垂直比例发生了翻转,因此如果您直接将两个图合并(无论如何),您最终会得到您观察到的结果,即一个图相对于另一个图翻转。
我建议在叠加之前翻转等高线图:
hold on
image([-180 180], [-90 90], worldMap);
title('Declination at elev = 0 km');
contour(X, Y, flipud(dec0Mat), contoursAt, 'ShowText','on', 'LineWidth', 2);
colormap('Gray');
或
hold on
image([-180 180], [-90 90], worldMap);
title('Declination at elev = 0 km');
contour(X, -Y, dec0Mat, contoursAt, 'ShowText','on', 'LineWidth', 2);
colormap('Gray');
最佳,
解释是imagesc
命令和image
一样,设置'Ydir'
轴属性为'reverse'
。
When called with
C
orX
,Y
,C
,image
sets the axes limits to tightly enclose the image, sets the axesYDir
property to'reverse'
, and sets the axesView
property to[0 90]
.
这意味着垂直轴值从轴的顶部到底部增加。
现在你可以比较两种情况:
如果你运行
contourf
命令本身,你有 "normal" 轴模式,垂直轴值从轴的底部增加到顶端。您第一个图中的垂直轴标签反映了这一点。如果在同一张图上绘制
imagesc
和运行contour
的图像,则纵轴先翻转imagesc
。随后的contour
命令在翻转的垂直轴上运行,并相应地绘制。这就是等高线相对于情况 1 垂直翻转的原因。
请注意,案例2得到的组合图是正确的。如果您 "visually" 组合了问题的前两张图片(分别通过调用 imagesc
和 countour
获得),那将是错误的,因为它们具有不同的垂直轴。