Matlab图像和具有意外翻转的图 [英] Matlab Image and Plot with Unexpected Flip

查看:140
本文介绍了Matlab图像和具有意外翻转的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,每种情况下都有一些明显的代码差异,但是我不了解它们如何改变图像或绘图的方向,如每种情况所示.

In the examples below, there are some obvious code differences in each case, but I don't understand how they would change the orientation of the image or the plot, as shown in each case.

原始图像的尺寸为407 x 813 x3.

The original image has a size of 407 x 813 x 3.

对于轮廓数据,X的范围是-180至+ 175,Y的范围是-85至+85

For the data in the contour, X ranges from -180 to +175, Y ranges from -85 to +85

对于图像,我正在尝试使其适合X的范围从-180到+ 180,Y的范围从-90到+90.因此,该图像所覆盖的地理范围比该图像要宽一些.

For the image, I am trying to fit it so that X ranges from -180 to +180, Y ranges from -90 to +90. So the image covers a slightly wider geographic range than the image does.

我有一些地理数据的轮廓图.它像这样绘制,看起来像我期望的那样:

I have a contour plot of some geographic data. It plots like this, and looks like I expect it to look:

绘制此外观的代码为

figure (1);
contour(X, Y, dec0Mat,contoursAt, 'ShowText','on', 'LineWidth', 2);
colormap('Gray');   

我可以自己绘制图像,看起来还可以:

I can plot the image by itself, and it looks ok:

使用以下代码: 图(99) imagesc([-180 180],[-90 90],worldMap);

using this code: figure (99) imagesc([-180 180], [-90 90], worldMap);

但是当我尝试将两者结合时,我得到了:

But when I try to combine the two, I get this:

使用此代码:

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');

在这种情况下,地图看起来正确,但轮廓线是垂直翻转的.

In this case the map looks right, but the contour is flipped vertically.

在另一种情况下,我得到了:

In another case, I get this:

使用此代码:

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');

在这种情况下,垂直翻转地图,并向右绘制轮廓.

In this case the map is flipped vertically and the contour is plotted right.

在Matlab文档中找到imagesc,它说:

Going by the Matlab documentation for imagesc, it says:

imagesc(x,y,C)将C显示为图像,并指定 带有向量x和y的x轴和y轴. 如果x(1)> x(2)或y(1)> y(2),则 图像分别左右或上下翻转.如果x和y为 标量,将图像转换到指定位置(x,y),例如 图像的左上角从(x,y)开始.

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).

我强调了与翻转图像可能相关的东西,但在这里似乎都不是,也无法解释在一种情况下而不是在另一种情况下翻转位置的不一致之处.

I highlighted what might be relevant for an image being flipped, but neither seems to be the case here and wouldn't explain the inconsistency where is is flipped in one case and not the other.

推荐答案

解释是imagesc命令,例如

The explanation is that the imagesc command, like image, sets the 'Ydir' axis property to 'reverse'.

CXYC调用时,image设置轴限制以紧密包围图像,将轴YDir属性设置为'reverse' ,并将轴View属性设置为[0 90].

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].

这意味着垂直轴值从轴的顶部到底部增加.

This means that the vertical axis values increase from the top of the axis to the bottom.

现在您可以比较这两种情况:

Now you can compare the two cases:

  1. 如果单独运行contourf命令,则将具有常规"轴模式,垂直轴值从轴的底部到顶部增加.第一张图中的垂直轴标签反映了这一点.

  1. If you run the contourf command by itself, you have the "normal" axis mode, with vertical axis values increasing from the bottom of the axis to the top. The vertical axis labelling in your first figure reflects that.

如果使用imagesc绘制图像并在同一图形上运行contour,则垂直轴首先会翻转imagesc.随后的contour命令在翻转的垂直轴上进行操作,并相应地进行绘制.这就是轮廓线相对于情况1垂直翻转的原因.

If you plot the image with imagesc and run contour on the same figure, the vertical axis is first flipped by imagesc. The subsequent contour command operates on a flipped vertical axis, and plots accordingly. That's why the contour lines are vertically flipped with respect to case 1.

请注意,在情况2中获得的组合数字是正确的.如果您直观地"组合问题的前两个图像(它们是分别通过调用imagesccountour获得的),那将是错误的,因为它们的垂直轴不同.

Note that the combined figure obtained in case 2 is correct. If you "visually" combined the first two images of your question (which were obtained from calling imagesc and countour independently) it would be wrong, because they have different vertical axes.

这篇关于Matlab图像和具有意外翻转的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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