对图片坐标进行线性映射


import cv2

path = "obama2.jpg"

img = cv2.imread(path)
y, x, c_1 = (list(img.shape))
print(x, ",", y, ",", c_1)
img = cv2.resize(img, (224, 224))
y_hat,x_hat, c_2 = (list(img.shape))
print(x_hat, ",", y_hat, ",", c_2)

# cv2.imwrite('obm.jpg', img)

def Change_lable(m,n,x,y,x_hat,y_hat):
m_hat = (m/x) * x_hat
n_hat = (n/y) * y_hat
return m_hat,n_hat
new_m,new_n = Change_lable(137,445,x,y,x_hat,y_hat)
print(new_m,",",new_n)
def Restore_lable(m_hat,n_hat,x,y,x_hat,y_hat):
m = m_hat*x/x_hat
n = n_hat*y/y_hat
return m,n
pre_m,pre_n = Restore_lable(new_m,new_n,x,y,x_hat,y_hat)
print(pre_m,",",pre_n)
结果:

626 , 1200 , 3
224 , 224 , 3
49.0223642172524 , 83.06666666666666
137.00000000000003 , 445.0

原图:↑

原文地址:https://www.cnblogs.com/ansang/p/8624158.html