为什么计算灰度图像的平均值时有流浪像素?
我正在计算附带的三个灰度图像的平均值。 Image1EATH图像的大小为256 x 256。平均时,我在平均输出中获得了几个流浪像素。下面给出的是我用来生成平均图像的代码。
import cv2
img1 = 'image1.png'
img2 = 'image2.png'
img3 = 'image3.png'
img_1 = cv2.imread(img1).astype("float32")
img_2 = cv2.imread(img2).astype("float32")
img_3 = cv2.imread(img3).astype("float32")
# averaging
result = 255*((img_1 + img_2 + img_3)/3)
result1 = result.clip(0, 255).astype("uint8")
cv2.imwrite('avg.png', result1)
I am computing the average of three grayscale images attached herewith.
image1Each image is of size 256 x 256. Upon averaging, I am getting several stray pixels in the averaged output. Given below is the code I am using to generate the averaged image.
import cv2
img1 = 'image1.png'
img2 = 'image2.png'
img3 = 'image3.png'
img_1 = cv2.imread(img1).astype("float32")
img_2 = cv2.imread(img2).astype("float32")
img_3 = cv2.imread(img3).astype("float32")
# averaging
result = 255*((img_1 + img_2 + img_3)/3)
result1 = result.clip(0, 255).astype("uint8")
cv2.imwrite('avg.png', result1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JIFI有答案。如果您已经打印了所读的阵列,则您会意识到这些阵列已经从0到255。您假设它们从0到1。将获得您期望的结果。
确实说,至少您的某些图像确实在该底部的区域中有流浪像素。在任何图像中,在最终平均值中,任何图像中的像素值为1(在255中)将导致85个。
随访
好,我现在假设您真正想要的是这三张图像的结合,而不是平均值。如果是这样,这将做到这一点:
因此,
img_x
将包含true,而图像像素值大于1。如果更改
> 1
to> 0
,您将在原始图像中看到所有非零像素,这为您提供底部的杂物。Jifi has the answer. If you had printed the arrays you're reading, you would have realized that those arrays ALREADY run from 0 to 255. You assumed they run from 0 to 1. If you remove the
255*
, you'll get the results you expect.That does say that at least some of your images do have stray pixels down in that bottom area. A pixel value of 1 (out of 255) in any of the images would result in 85 in the final average.
Followup
OK, I'm now assuming that what you really want is the UNION of those three images, not the AVERAGE. If so, this will do it:
So,
img_x
will contain True where the image pixel value is larger than 1. True and False behave as 1 and 0 in arithmetic computations.If you change the
> 1
to> 0
, you will see all of the non-zero pixels in your original images, which gives you the stray blobs at the bottom.