简体   繁体   English

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

[英]numpy, summing matrices gives wrong result

Why summing together rgb channel matrices together doesn't give maximum result of 765, when every matrix has maximum of 255 and these values are at same position? 当每个矩阵的最大值为255并且这些值位于相同的位置时,为什么将rgb通道矩阵加在一起不能得到765的最大结果? But it gives maximum of 3 if all matrices are divided by 255. 但是,如果将所有矩阵除以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

Does subdividing (like here : np.asarray(g)/255) actually changes anything other than value? 细分(例如:np.asarray(g)/ 255)是否会真正改变值以外的其他内容?

EDIT: dtype before dividing is uint8 and after dividing float64 编辑:除以之前的dtype是uint8而除以float64之后的dtype

Try examining the type of g_,b_, and r_. 尝试检查g_,b_和r_的类型。

If they have type numpy.uint8, you should get a warning and the result should be 253. 如果它们的类型为numpy.uint8,则应收到警告,结果应为253。

In the second case, the g_mat, b_mat and r_mat are converted to numpy.int64 upon division 在第二种情况下,除法后将g_mat,b_mat和r_mat转换为numpy.int64

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

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