简体   繁体   English

反转图像的 y 轴而不颠倒图像

[英]Invert the y-axis of an image without flipping the image upside down

I'm trying to do some image processing with matplotlib .我正在尝试使用matplotlib进行一些图像处理。 However the y-axis is decreasing bottom up.然而,y 轴自下而上递减。 I want it to be increasing bottom up without flipping the image upside down我希望它在不翻转图像的情况下自下而上增加

I have the following code我有以下代码

import matplotlib.pyplot as plt
import numpy as np
img_path = '/path/to/image.tif'
img = plt.imread(img_path)
plt.imshow(img, cmap = 'gray')

it produces the following image:它产生以下图像: 在此处输入图片说明

The images can be obtained there图像可以在那里获得

I tried plt.gca().invert_yaxis() without success我试过plt.gca().invert_yaxis()没有成功

What shall I do我该怎么办

The default behavior of imshow is to put the origin of the coordinate system in the upper left corner.的默认行为imshow是把坐标系的原点在左上角 This is different from plotting scientific data, such as two entities x and y against each other, where the origin, ie the point corresponding to the coordinate (0,0) , is in the lower left corner, with the (positive) x-axis extending to the right and the (positive) y-axis extending towards the top.这是从绘制的科学数据不同,如两个实体xy彼此抵靠,其中,所述原点,即,对应于坐标点(0,0)是在左下角,与(正)X轴轴向右延伸,(正)y 轴向上延伸。

The latter is just scientific convention, though one that goes back centuries.后者只是科学惯例,尽管可以追溯到几个世纪之前。 Arguably (albeit near impossible to back up with historical evidence), the x-axis is traditionally aligned left-to-right because that's how text is written in many languages, while the y-axis is oriented towards the top as that's how people intuit an increase — much like the elevation of terrain.可以说(尽管几乎不可能用历史证据来支持),x 轴传统上是从左到右对齐的,因为这就是用多种语言编写文本的方式,而 y 轴则朝向顶部,因为这是人们的直觉增加——很像地形的高度。

For images, on the other hand, the existing convention is rooted in the memory layout of pixel data and the way one might arrange consecutive pixels on the canvas: from left to right on the first line (by the same logic as above), then from the left again on the next line, and so on for all other lines, going from top to bottom.另一方面,对于图像,现有约定植根于像素数据的内存布局以及可能在画布上排列连续像素的方式:在第一行从左到右(按照与上述相同的逻辑),然后在下一行再次从左侧开始,对所有其他行以此类推,从上到下。 Just like words arranged on a page — in languages written from left to right and, much more universally so, top to bottom.就像排列在页面上的文字一样——语言是从左到右书写的,更普遍的是,从上到下书写。

It is for that reason that the y-axis in your image is oriented the way it is.正是出于这个原因,图像中的 y 轴按原样定向。 To have the y-values increase from the bottom up, you could invoke the option origin='lower' so that the input data is interpreted as per the scientific convention.要使 y 值自下而上增加,您可以调用选项origin='lower'以便按照科学惯例解释输入数据。 However, you then also need to flip the image's lines upside down so that, when displayed on the screen, the image appears in its intended orientation.然而,在屏幕上显示时,这时也需要翻转图像的线条倒挂,这样,图像出现在其预期的取向。 That's because what used to be the first line when the image was loaded into memory now corresponds to the last y-coordinate, the one at the top.那是因为当图像加载到内存时的第一行现在对应于最后一个 y 坐标,即顶部的那个。

Bottom line (pun not unintended), just call imshow like so in the above code:底线(双关语并非无意),只需在上面的代码中像这样调用imshow

plt.imshow(np.flipud(img), cmap='gray', origin='lower')

To illustrate further, here is a self-contained example that demonstrates the behavior:为了进一步说明,这里是一个独立的示例,演示了该行为:

from imageio import imread
image = imread('https://upload.wikimedia.org/wikipedia/commons'
               '/thumb/6/6a/Mona_Lisa.jpg/158px-Mona_Lisa.jpg')

from matplotlib import pyplot
figure = pyplot.figure(tight_layout=True)
(axes1, axes2, axes3) = figure.subplots(nrows=1, ncols=3)

axes1.set_title("origin='upper'")
axes1.imshow(image)

axes2.set_title("origin='lower'")
axes2.imshow(image, origin='lower')

axes3.set_title("'lower' + flipped")
axes3.imshow(image[::-1], origin='lower')

pyplot.show()

The example requires ImageIO to be installed in order to retrieve the sample image.该示例需要安装ImageIO才能检索示例图像。 Its output is:它的输出是:

演示脚本的输出

(In the example's code, I used image[::-1] to flip the image, instead of the aforementioned, and equivalent, syntax np.flipud(image) . All that does is avoid an explicit import of NumPy (as np ), ie, an extra line of code. Im plicitly, NumPy is still doing the work.) (在示例的代码中,我使用image[::-1]来翻转图像,而不是前面提到的等效语法np.flipud(image) 。所做的只是避免显式import NumPy(作为np )即,一个额外的代码行。plicitly,NumPy的还是做的工作。)

Similar questions:类似问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM