我有以下功能:
import numpy as np
my_rects = np.array([[ 518, 792, 646, 1080]])
relative_rects = np.array([[ 16, 53, 116, 286.]])
crops_width = my_rects[:, 2] - my_rects[:, 0]
crops_height = my_rects[:, 3] - my_rects[:, 1]
def expand_rects(rects, img_width, img_height, width_ratio, height_ratio, epsilon=1e-5):
W = rects[:, 2] - rects[:, 0]
H = rects[:, 3] - rects[:, 1]
center_x = (rects[:, 0] + rects[:, 2]) / 2
center_y = (rects[:, 1] + rects[:, 3]) / 2
res = np.zeros_like(rects, dtype='float32')
res[:, 0] = center_x - W * width_ratio / 2
res[:, 2] = center_x + W * width_ratio / 2
res[:, 1] = center_y - H * height_ratio / 2
res[:, 3] = center_y + H * height_ratio / 2
res[:, 0] = np.clip(res[:, 0], 0, img_width)
res[:, 2] = np.clip(res[:, 2], 0, img_width)
res[:, 1] = np.clip(res[:, 1], 0, img_height)
res[:, 3] = np.clip(res[:, 3], 0, img_height)
return res
print(expand_rects(relative_rects, crops_width, crops_height, 1.2, 1.1 ))
运行此程序后,我得到了以下结果:
5.999996 41.34999 126. 288.
但是,第一个元素应该是 6.0 而不是 5.999996(66−1.2×(116−16)÷2 = 6)。更重要的是,如果我将函数放入另一个大文件中,输入相同,输出变为:
6. 41.35 126. 288.
我的环境是:
Linux (none) 4.9.37 #1 SMP Tue Nov 13 10:04:52 CST 2018 armv7l GNU/Linux
root@(none):/jkklklk/projects/# python3
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
对此有解释吗?我真的需要结果稳定(每次都应该是 5.9999996)。