为什么我的图像变量值会发生变化?

时间:2015-08-11 23:15:47

标签: python-2.7 image-processing

我不知道我的变量发生了什么。当变量“amarilloSegm”在方法“Suma_Manchas(amarilloSegm,NecrosadoSegm)中”时,它会改变它的值。奇怪的是我的变量并没有在所有方法中修改。

这是我的代码,当我调用方法时:

...
imgManchasUnidas = Suma_Manchas(amarilloSegm, necrosadoSegm)
...

这是我的方法体:

def Suma_Manchas(imagen_1, imagen_2):
    imgAma = imagen_1
    imgNecro = imagen_2
    manchasUnidas = imgAma  

    y, x = manchasUnidas.shape

    for fil in range (0, y):
        for col in range (0, x):
#           print "x: " + str(fil) + " y: " + str(col)
            valUmbral = imgNecro[fil, col]
            if (valUmbral > BLANCO-20):
                manchasUnidas.itemset((fil, col), BLANCO)

    return manchasUnidas

1 个答案:

答案 0 :(得分:0)

您执行imgAma = imagen_1然后manchasUnidas = imgAma,最后修改manchasUnidas,最后修改imagen_1,因为它复制引用而不是创建新对象记忆。

如果你想在内存的另一部分有一个全新的对象,你可以做copy.deepcopy(),这样当你改变一个时,另一个不会被修改。

更具体地说:

import copy
def Suma_Manchas(imagen_1, imagen_2):
    imgAma = copy.deepcopy(imagen_1)
    ...