简体   繁体   English

在这种情况下, cv2.addWeighted 和 numpy mean 之间有什么区别?

[英]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 )加载了灰度图像img1img2 ,两者的尺寸相同。 Now, I wish to take the mean of both img1 and img2 .现在,我想取img1img2的平均值。 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.imwritemean图像写入磁盘来验证这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.

相关问题 cv2.addWeighted()不起作用 - cv2.addWeighted() does not work 为什么cv2.addweighted()会给出一个错误,即操作既不是'array op array',也不是'array op scalar',也不是'scalar op array'? - Why does cv2.addweighted() give an error that the operation is neither 'array op array', nor 'array op scalar', nor ' scalar op array'? cv2.videocapture(1) 和 cv2.videocapture(0) 有什么区别? - What is the difference between cv2.videocapture(1) and cv2.videocapture(0)? Numpy中括号和括号有什么区别? - What is difference between parenthesis and bracket in Numpy? numpy 中的 dtype= 和 .astype() 有什么区别? - What is the difference between dtype= and .astype() in numpy? `numpy` 中的 `slice` (:) 和 `ellipsis` (...) 运算符有什么区别? - What is the difference between the `slice` (:) and the `ellipsis` (…) operators in `numpy`? 使用 Numpy 对矩阵进行元素操作的运算符与在矩阵上执行的操作有什么区别? - What is the difference between the operator acting elementwise vs on the matrix using Numpy? ** {'xerr':cv_std}是什么意思? - What does **{'xerr':cv_std} mean? 在这种情况下,语法[]是什么意思? - What does the syntax [] mean in this case? 运算符 '>>' 和 '|' 是什么意思在这种情况下是什么意思? - What do operators '>>' and '|' mean in this case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM