import math
import numpy as np
def backslash(a,b):
m,n=a.shape
p,q=b.shape
if(m!=p):
print('dimensions do not match!')
return -1
if(m==n):
return np.linalg.solve(a,b)
else:
a1 = a.T.dot(a)
b1 = a.T.dot(b)
return np.linalg.solve(a1,b1)
def slash(a,b):
a=a.T
b=b.T
x = backslash(a,b)
return x.T
zi = np.array([7664387, 3014446, 5834255, 5189098, 489725, 3608269, 5999937, 1611564, 4839245])
xi = np.array([[1904], [1905], [1906], [1904], [1905], [1906], [1904], [1905], [1906]])
yi = np.array([[1219], [1219], [1219], [1220], [1220], [1220], [1221], [1221], [1221]])
z = np.array([[7664387, 3014446, 5834255], [5189098, 489725, 3608269], [5999937, 1611564, 4839245]])
XX = np.empty([9, 5])
ZiT = np.array([[7664387], [3014446], [5834255], [5189098], [489725], [3608269], [5999937], [1611564], [4839245]])
for i in range(9):
XX[i, 0] = 1
XX[i, 1] = xi[i]
XX[i, 2] = yi[i]
XX[i, 3] = xi[i] * xi[i]
XX[i, 4] = yi[i] * yi[i]
B = np.dot(XX.T, XX)
print(B.shape)
C = np.dot(XX.T,np.log(ZiT))
print(C.shape)
# B = slash(B, C)
B = np.linalg.solve(B, C)
b0 = B[0]
b1 = B[1]
b2 = B[2]
b3 = B[3]
b4 = B[4]
xmin = -b1/b3/2
ymin = -b2/2/b4
xmin = xmin - 1905
ymin = ymin - 1220
x = xmin / 96 * 25.4
y = ymin / 96 * 25.4
print(x,y)
```cpp main2.py
import cv2
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
from matplotlib import cm
# 相关系数计算
def correlation(num1, num2, i, j, h0, w0):
C = 0
for i0 in range(i, i + h0):
for j0 in range(j, j + w0):
C = C + (int(num1[i0 - i, j0 - j]) - int(num2[i0, j0])) * (int(num1[i0 - i, j0 - j]) - int(num2[i0, j0]))
return C
# 截取target(100 * 100)
img = Image.open("1.jpg")
# cropped1 = img.crop((1895, 800, 1995, 900)) # (left, upper, right, lower) 左上,右下
# cropped1.save("target0.png")
# cropped2 = img.crop((1895, 925, 1985, 1015)) # (left, upper, right, lower) 左上,右下
# cropped2.save("target1.png")
# cropped3 = img.crop((1890, 1040, 1998, 1148)) # (left, upper, right, lower) 左上,右下
# cropped3.save("target2.png")
# cropped4 = img.crop((1905, 1220, 1995, 1315)) # (left, upper, right, lower) 左上,右下
# cropped4.save("target3.png")
img1 = cv2.imread("./1.jpg", 0)
img1 = np.array(img1)
img2 = cv2.imread("./2.jpg", 0)
img2 = np.array(img2)
target0 = cv2.imread("./target2.png", 0)
target0 = np.array(target0)
h = img1.shape[0]
w = img1.shape[1]
h2 = target0.shape[0]
w2 = target0.shape[1]
def fun(x): # 处理符号问题
round(x, 2)
if x >= 0:
return '+' + str(x)
else:
return str(x)
# xx = 1895 # target1
# yy = 925
xx = 1890 # target2
yy = 1040
# xx = 1905 # target3
# yy = 1220
# 主函数
if __name__ == '__main__':
n = 9
# print(n)
X = []
Y = []
Z = []
# 读入n个点的坐标
# for i in range(n):
# a = [int(i) for i in input('please input: ').split()]
# X.append(a[0])
# Y.append(a[1])
# Z.append(a[2])
for i in range(yy - 1, yy + 2):
for j in range(xx - 1, xx + 2):
C = correlation(target0, img2, i, j, h2, w2)
print(i, j, C)
X.append(j)
Y.append(i)
Z.append(C)
# print(X)
# print(Y)
# print(Z)
# fig = plt.figure() #建立一个新的图像
# ax = Axes3D(fig) #建立一个三维坐标系
# ax.scatter(X,Y,Z,c='b')
# ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
# plt.show()
# 求方程系数
sigma_x = 0
for i in X:
sigma_x += i
sigma_y = 0
for i in Y:
sigma_y += i
sigma_z = 0
for i in Z:
sigma_z += i
sigma_x2 = 0
for i in X:
sigma_x2 += i * i
sigma_y2 = 0
for i in Y:
sigma_y2 += i * i
sigma_x3 = 0
for i in X:
sigma_x3 += i * i * i
sigma_y3 = 0
for i in Y:
sigma_y3 += i * i * i
sigma_x4 = 0
for i in X:
sigma_x4 += i * i * i * i
sigma_y4 = 0
for i in Y:
sigma_y4 += i * i * i * i
sigma_x_y = 0
for i in range(n):
sigma_x_y += float(X[i] * Y[i])
# print(sigma_xy)
sigma_x_y2 = 0
for i in range(n):
sigma_x_y2 += float(X[i] * Y[i] * Y[i])
sigma_x_y3 = 0
for i in range(n):
sigma_x_y3 += float(X[i] * Y[i] * Y[i] * Y[i])
sigma_x2_y = 0
for i in range(n):
sigma_x2_y += float(X[i] * X[i] * Y[i])
sigma_x2_y2 = 0
for i in range(n):
sigma_x2_y2 += float(X[i] * X[i] * Y[i] * Y[i])
sigma_x3_y = 0
for i in range(n):
sigma_x3_y += float(X[i] * X[i] * X[i] * Y[i])
sigma_z_x2 = 0
for i in range(n):
sigma_z_x2 += float(Z[i] * X[i] * X[i])
sigma_z_y2 = 0
for i in range(n):
sigma_z_y2 += float(Z[i] * Y[i] * Y[i])
sigma_z_x_y = 0
for i in range(n):
sigma_z_x_y += float(Z[i] * X[i] * Y[i])
sigma_z_x = 0
for i in range(n):
sigma_z_x += float(Z[i] * X[i])
sigma_z_y = 0
for i in range(n):
sigma_z_y += float(Z[i] * Y[i])
# print("-----------------------")
# 给出对应方程的矩阵形式
a = np.array([[sigma_x4, sigma_x3_y, sigma_x2_y2, sigma_x3, sigma_x2_y, sigma_x2],
[sigma_x3_y, sigma_x2_y2, sigma_x_y3, sigma_x2_y, sigma_x_y2, sigma_x_y],
[sigma_x2_y2, sigma_x_y3, sigma_y4, sigma_x_y2, sigma_y3, sigma_y2],
[sigma_x3, sigma_x2_y, sigma_x_y2, sigma_x2, sigma_x_y, sigma_x],
[sigma_x2_y, sigma_x_y2, sigma_y3, sigma_x_y, sigma_y2, sigma_y],
[sigma_x2, sigma_x_y, sigma_y2, sigma_x, sigma_y, n]])
b = np.array([sigma_z_x2, sigma_z_x_y, sigma_z_y2, sigma_z_x, sigma_z_y, sigma_z])
# 高斯消元解线性方程
print(a)
print(b)
res = np.linalg.solve(a, b)
# print(a)
# print(b)
# print(x)
# print("-----------------------")
# 输出方程形式
print("z=%.10s*x^2%.10s*xy%.10s*y^2%.10s*x%.10s*y%.10s" % (
fun(res[0]), fun(res[1]), fun(res[2]), fun(res[3]), fun(res[4]), fun(res[5])))
# 画曲面图和离散点
fig = plt.figure() # 建立一个空间
ax = fig.add_subplot(111, projection='3d') # 3D坐标
n = 256
u = np.linspace(-20, 20, n) # 创建一个等差数列
x, y = np.meshgrid(u, u) # 转化成矩阵
# 给出方程
z = res[0] * x * x + res[1] * x * y + res[2] * y * y + res[3] * x + res[4] * y + res[5]
x_ans = (2 * res[3] * res[2] - res[4] * res[1]) / (res[1] * res[1] - 4 * res[0] * res[2])
y_ans = (2 * res[4] * res[0] - res[3] * res[1]) / (res[1] * res[1] - 4 * res[0] * res[2])
print("X相对位移(pt):", x_ans)
print("Y相对位移(pt):", y_ans)
x_ans = (x_ans - xx) / 96 * 25.4
y_ans = (y_ans - yy) / 96 * 25.4
print("X相对位移(mm):", x_ans)
print("Y相对位移(mm):", y_ans)
# print("X相对位移(mm):", round(x_ans, 2))
# print("Y相对位移(mm):", round(y_ans, 2))
# 画出曲面
ax.plot_surface(x, y, z, rstride=3, cstride=3, cmap=cm.jet)
# 画出点
ax.scatter(X, Y, Z, c='r')
# 第二张图片搜索
# for i in range(924, 924 + 3):
# for j in range(1894, 1894 + 3):
# C = correlation(target0, img2, i, j, h2, w2)
# print(i, j, C)
import cv2
img = cv2.imread('./1.jpg')
cut_img = img[920:1020, 1890:1990]
cv2.imshow('image', cut_img)
cv2.waitKey(0)
cv2.imwrite('./target2.png', cut_img)
`