繁体   English   中英

numpy的,求和矩阵给出错误的结果

[英]numpy, summing matrices gives wrong result

当每个矩阵的最大值为255并且这些值位于相同的位置时,为什么将rgb通道矩阵加在一起不能得到765的最大结果? 但是,如果将所有矩阵除以255,则最大值为3。

import numpy as np
from PIL import Image

pic= Image.open(picture_dir)
r,g,b = pic.split()

g_ = np.asarray(g)
b_ = np.asarray(b)
r_ = np.asarray(r)

print((r_+g_+b_).max()) # gives result of 255, supposed to be 765


g_mat = np.asarray(g)/255
b_mat = np.asarray(b)/255
r_mat = np.asarray(r)/255

print((g_mat+b_mat+r_mat).max()) # gives result of 3.0

细分(例如:np.asarray(g)/ 255)是否会真正改变值以外的其他内容?

编辑:除以之前的dtype是uint8而除以float64之后的dtype

尝试检查g_,b_和r_的类型。

如果它们的类型为numpy.uint8,则应收到警告,结果应为253。

在第二种情况下,除法后将g_mat,b_mat和r_mat转换为numpy.int64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM