为什么要替换原始图像矩阵值?

Why is the original image matrix values getting replaced?

我正在尝试根据硬编码对图像进行阈值处理 value.I 我正在通过将原始图像分配给一个变量来做到这一点。此变量用于阈值化。但是,当我执行此操作时,原始图像也被阈值化了。难道我做错了什么?还是有其他方法可以做到这一点?代码如下:

import numpy as np
from scipy.misc import imread
import matplotlib.pyplot as plt 
img1 = imread('4.2.04.tiff')
imgx = img1
imgx[img1>=150] = 0
plt.figure()
plt.imshow(np.uint8(img1))
plt.show()
plt.title('Original Image after thresholding')
plt.figure()
plt.imshow(np.uint8(imgx))
plt.title('Thresholded Image')

图片如下:

谢谢。

imgx = img1

您基本上是在创建对现有变量 imgx 的引用。现在 imgximg1 指向同一个地址。

如果你想深度复制数组,请执行此操作。

img1 = numpy.array(imgx)

有关详细信息,请参阅 this post