• 亚像素源码


    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)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    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)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    `在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    Office/WPS 好用的PPT插件-智能选择布局
    Spring Boot集成antlr实现词法和语法分析
    灵界的科学丨六、星际通信新科技──寻找外星人
    数据结构与算法编程题11
    Java程序设计——类加载(Java高级应用)
    基于Or-Tools的整数规划问题求解
    Xcode fetch package dependencies很慢的解决办法
    css垂直居中的6种方式
    简单DIV+CSS学生网页设计——电影请以你的名字呼唤我(4页)带音乐特效
    机器学习day3
  • 原文地址:https://blog.csdn.net/LOOKQAQ/article/details/126914099