[英]What is the difference between cv2.addWeighted and numpy mean, in this case?
Suppose I have two OpenCV (python package cv2
) loaded grayscale images img1
and img2
, both of same dimensions.假设我有两个 OpenCV(python 包cv2
)加载了灰度图像img1
和img2
,两者的尺寸相同。 Now, I wish to take the mean of both img1
and img2
.现在,我想取img1
和img2
的平均值。 Here are two ways to do it:这里有两种方法:
# Method 1
mean = (img1 * 0.5) + (img2 * 0.5)
# Method 2
mean = cv2.addWeighted(img1,0.5,img2,0.5,0)
However, mean
is visually different in both methods, when I display them using cv2.imshow
.但是,当我使用cv2.imshow
显示它们时,这两种方法的mean
在视觉上是不同的。 Why is this so?为什么会这样?
I am glad that you have found a working solution to your problem, but this seems to be a workaround.我很高兴您找到了解决问题的有效方法,但这似乎是一种解决方法。 The real reason for this behaviour lies somewhere else.这种行为的真正原因在于其他地方。 The problem here is that mean = (img1 * 0.5) + (img2 * 0.5)
is returning a matrix with float32
data type which contains values in range 0.0 - 255.0
.这里的问题是mean = (img1 * 0.5) + (img2 * 0.5)
返回一个包含float32
数据类型的矩阵,其中包含范围0.0 - 255.0
值。 You can verify this by using print mean.dtype
.您可以通过使用print mean.dtype
来验证这print mean.dtype
。 Since the new matrix values have been converted to float unintentionally, we can revert this operation by using (img_1 * 0.5 + img_2 * 0.5).astype("uint8")
.由于新的矩阵值已被无意中转换为浮点数,我们可以使用(img_1 * 0.5 + img_2 * 0.5).astype("uint8")
恢复此操作。 In case of cv2.addWeighted()
it automatically returns you a matrix of data type uint8
and all things would work fine.在cv2.addWeighted()
情况下,它会自动返回一个数据类型为uint8
的矩阵,一切都会正常工作。
My concern is with the conclusion that you have drawn:我担心的是你得出的结论:
The issue is that the
cv2.imshow()
method used to display images, expects your image arrays to be normalized, ie in the range [0,1].问题是用于显示图像的cv2.imshow()
方法期望您的图像数组被归一化,即在 [0,1] 范围内。
cv2.imshow()
works just fine with range of [0-255]
and [0.0-1.0]
, but the issue arises when you pass a matrix whose values are in range [0-255]
, but the dtype is float32
instead of uint8
. cv2.imshow()
在[0-255]
和[0.0-1.0]
范围内工作得很好,但是当您传递值在[0-255]
范围内的矩阵时会出现问题,但 dtype 是float32
而不是uint8
。
Answering my own question, to help others who get confused by this:回答我自己的问题,以帮助其他对此感到困惑的人:
Both methods 1 and 2 yield the same result.方法 1 和 2 产生相同的结果。 You can verify this by writing the mean
image to disk using cv2.imwrite
.您可以通过使用cv2.imwrite
将mean
图像写入磁盘来验证这cv2.imwrite
。 The issue is not with the methods.问题不在于方法。
The issue is that the cv2.imshow
method used to display images, expects your image arrays to be normalized, ie in the range [0,1].问题是用于显示图像的cv2.imshow
方法期望您的图像数组被归一化,即在 [0,1] 范围内。 In my case, both the image arrays are 8-bit unsigned integers and so, its pixel values are in the range [0,255].就我而言,两个图像数组都是 8 位无符号整数,因此,其像素值在 [0,255] 范围内。 Since mean
is an average of the two arrays, its pixel values are also in the range [0,255].由于mean
是两个数组的平均值,因此其像素值也在 [0,255] 范围内。 So when I passed mean
to cv2.imshow
, pixels having values greater than 1 were interpreted as having a value of 255, resulting in vastly different visuals.因此,当我将mean
传递给cv2.imshow
,值大于 1 的像素被解释为值为 255,从而产生截然不同的视觉效果。
The solution is to normalize mean
before passing it to cv2.imshow
:解决方案是在将mean
传递给cv2.imshow
之前对其进行归一化:
# Method 1
mean = (img1 * 0.5) + (img2 * 0.5)
# Method 2
mean = cv2.addWeighted(img1,0.5,img2,0.5,0)
# Note that the division by 255 results in the image array values being squeezed to [0,1].
cv2.imshow("Averaged", mean/255.)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.