在计算图像矩的过程中,发现数据溢出了,与调用openCV库中的函数算出的结果不同。
目前使用的是numpy数组储存数据,数据类型设为int64,但是他的范围还是不够大,数值会在10的16 次方的数量级。应该怎么设置这个数组的类型呀?
# 计算p+q阶图像矩
def img_m(img):
# 用于存放图像矩计算结果
result = np.zeros([4,4],dtype='float')
# 获取图像大小
height = img.shape[0]
width = img.shape[1]
# 遍历图像内的像素点,进行图像矩计算
for row in range(height):
for col in range(width):
result[0, 0] += img[row,col]
result[0, 1] += row * img[row, col]
result[1, 0] += col * img[row, col]
result[1, 1] += row * col * img[row, col]
result[0, 2] += row ** 2 * img[row, col]
result[2, 0] += col ** 2 * img[row, col]
result[0, 3] += row ** 3 * img[row, col]
result[3, 0] += col ** 3 * img[row, col]
result[2, 1] += row * col ** 2 * img[row, col]
result[1, 2] += row ** 2 * col * img[row, col]