python图像矩阵相减时负值变成0或者254的问题

import numpy as np 
import cv2
import matplotlib.pyplot as plt 

img = cv2.imread("butterfly.png", 0)
src_img = cv2.imread("qua.png", 0)
res_img = img-src_img
print(res_img)
n, bins, patches = plt.hist(res_img)
plt.show()

应改为

import numpy as np 
import cv2
import matplotlib.pyplot as plt 

img = cv2.imread("butterfly.png", 0).astype(np.float32)
src_img = cv2.imread("qua.png", 0).astype(np.float32)
res_img = img-src_img
print(res_img)
n, bins, patches = plt.hist(res_img)
plt.show()

就好了

你可能感兴趣的:(图像,零碎)