简体   繁体   中英

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? But it gives maximum of 3 if all matrices are divided by 255.

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?

EDIT: dtype before dividing is uint8 and after dividing float64

Try examining the type of g_,b_, and r_.

If they have type numpy.uint8, you should get a warning and the result should be 253.

In the second case, the g_mat, b_mat and r_mat are converted to numpy.int64 upon division

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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