在python opencv中,为什么数组[:]和数组不同?

在python opencv中,为什么数组[:]和数组不同?,python,arrays,opencv,Python,Arrays,Opencv,我是python和opencv的新手,所以请容忍我这个简单的问题 在下面的代码中,为什么两个数组计算提供了不同的答案 (a) 通道[:]=通道*标准化的Versealpha (b) 通道=通道*标准化为Versealpha 为什么(a)和(b)是不一样的 normalizedInverseAlpha = (1.0 / 255) * (255 - graySrc) channels = cv2.split(src) for channel in channels: channel[:] =

我是python和opencv的新手,所以请容忍我这个简单的问题

在下面的代码中,为什么两个数组计算提供了不同的答案

(a) 通道[:]=通道*标准化的Versealpha
(b) 通道=通道*标准化为Versealpha

为什么(a)和(b)是不一样的

normalizedInverseAlpha = (1.0 / 255) * (255 - graySrc)
channels = cv2.split(src)
for channel in channels:
    channel[:] = channel * normalizedInverseAlpha
    #this is different from channel = channel * normalizedInverseAlpha. Why?

cv2.merge(channels, dst)

我们使用
[:]
为数组中的每个元素执行数组切片运算

(一)

channel*alpha
的结果分配给
original channel

(二)

创建一个名为channel的新变量,并将
channel*alpha
的结果分配给
newchannel
,对原始变量没有影响


这里我做一个实验:

#!/usr/bin/python3
# 2018.01.20 19:28:35 CST
import cv2
import numpy as np

img = cv2.imread("test.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

alpha = (1.0 / 255) * (255 - gray)

channels = cv2.split(img)

for channel in channels:
    print("Before:", id(channel), channel.shape, channel.dtype)
    channel[:] = channel * alpha
    print("After:", id(channel), channel.shape, channel.dtype)

dst1 = cv2.merge(channels)


channels = cv2.split(img)
for channel in channels:
    print("Before:", id(channel), channel.shape, channel.dtype)
    channel = channel * alpha
    print("After:", id(channel), channel.shape, channel.dtype)

dst2 = cv2.merge(channels)

dst2 = cv2.merge(channels)
cv2.imshow("dst1", dst1)
cv2.imshow("dst2", dst2)
cv2.waitKey()

输出:

Before: 140130414391088 (480, 480) uint8
After: 140130414391088 (480, 480) uint8
Before: 140130413969616 (480, 480) uint8
After: 140130413969616 (480, 480) uint8
Before: 140130413969776 (480, 480) uint8
After: 140130413969776 (480, 480) uint8
Before: 140130413969936 (480, 480) uint8
After: 140130414391088 (480, 480) float64
Before: 140130413970016 (480, 480) uint8
After: 140130414391088 (480, 480) float64
Before: 140130413970096 (480, 480) uint8
After: 140130414391088 (480, 480) float64
我们可以清楚地看到频道的
id保持不变
,然后执行
频道[:]=channel*alpha
,因此在
原始频道上分配;当执行
channel=channel*alpha
操作时,
id发生变化,因此在
新频道上分配时,保留原始频道


结果如预期:


提供两种情况的输出。这令人印象深刻!我马上就明白了。我还从你那里学到了新代码。谢谢然而,我觉得奇怪的是Python会在同一条语句中创建一个同名的新变量
#!/usr/bin/python3
# 2018.01.20 19:28:35 CST
import cv2
import numpy as np

img = cv2.imread("test.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

alpha = (1.0 / 255) * (255 - gray)

channels = cv2.split(img)

for channel in channels:
    print("Before:", id(channel), channel.shape, channel.dtype)
    channel[:] = channel * alpha
    print("After:", id(channel), channel.shape, channel.dtype)

dst1 = cv2.merge(channels)


channels = cv2.split(img)
for channel in channels:
    print("Before:", id(channel), channel.shape, channel.dtype)
    channel = channel * alpha
    print("After:", id(channel), channel.shape, channel.dtype)

dst2 = cv2.merge(channels)

dst2 = cv2.merge(channels)
cv2.imshow("dst1", dst1)
cv2.imshow("dst2", dst2)
cv2.waitKey()
Before: 140130414391088 (480, 480) uint8
After: 140130414391088 (480, 480) uint8
Before: 140130413969616 (480, 480) uint8
After: 140130413969616 (480, 480) uint8
Before: 140130413969776 (480, 480) uint8
After: 140130413969776 (480, 480) uint8
Before: 140130413969936 (480, 480) uint8
After: 140130414391088 (480, 480) float64
Before: 140130413970016 (480, 480) uint8
After: 140130414391088 (480, 480) float64
Before: 140130413970096 (480, 480) uint8
After: 140130414391088 (480, 480) float64