gpt4 book ai didi

python - 使用 imshow 显示图像 - Matplotlib/Python

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:53 24 4
gpt4 key购买 nike

我正在尝试使用 imshow 绘制图像,但得到的输出是我没有预料到的...显示图像的方法是:

def generate_data_black_and_white_heat_map(data, x_axis_label, y_axis_label, plot_title, file_path):
plt.figure()
plt.title(plot_title)
plt.imshow(data.data, extent=[0, data.cols, data.rows, 0], cmap='Greys')
plt.xlabel(x_axis_label)
plt.ylabel(y_axis_label)
plt.savefig(file_path + '.png')
plt.close()

我的数据表示为:

def __init__(self, open_image=False):
"""
The Data constructor
"""
self.data = misc.lena() / 255.0
x, y = self.data.shape
self.rows = x
self.cols = y

我做了一些计算,在某些时候我必须这样做:

# A -> 2D ndarray
A.data[A.data >= 0.5] = 1.0
A.data[A.data < 0.5] = 0.0

这给了我:

enter image description here

但我想要相反的(白色背景)。所以,我只是这样做了:

# A -> 2D ndarray
A.data[A.data >= 0.5] = 0.0
A.data[A.data < 0.5] = 1.0

然后,我得到了这个(!!!):

enter image description here

我只是没明白。这对我来说很有意义。奇怪的是,如果我这样做:

for x in range(A.cols):
for y in range(A.rows):
if A.data[x][y] >= 0.5:
A.data[x][y] = 0.0
else:
A.data[x][y] = 1.0

它有效。我是否以错误的方式访问某些内容?

如果能帮助我澄清这一点,我将不胜感激。

提前谢谢您。

最佳答案

它正在按照您的指示进行操作:

A[A >= 0.5] = 0.0  #  all of you values are now < 0.5
A[A < 0.5] = 1.0 # all of your values are now 1

直接去做要好得多

B = A > .5  # true (1) where above thershold
iB = B < .5 # true (1) where below threshold

关于python - 使用 imshow 显示图像 - Matplotlib/Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22546431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com