Calculating Correlated Surfaces Using Fast Fourier Transform

Fast Template Matching, JP Lewis
#-*- coding:utf-8 -*-

Import numpy as np

Import matplotlib.pyplot as plt

Import cv2

From Integral_image import *

## read img #####

Tm_path = '/home/wdh/pytorch-CycleGAN-and-pix2pix1_run/results/map_pix2pix/test_latest/real_B/1_AB_real_B.png'

Ref_path = '/home/wdh/pytorch-CycleGAN-and-pix2pix1_run/results/map_pix2pix/test_latest/real_B/1_AB_real_B.png'

Img_ref_bgr = cv2.imread(ref_path)

Img_tm_bgr = cv2.imread(tm_path)

Img_ref_gray = (cv2.cvtColor(img_ref_bgr,cv2.COLOR_BGR2GRAY))/np.float64(255.0)

Img_src_gray = (cv2.cvtColor(img_tm_bgr,cv2.COLOR_BGR2GRAY))/np.float64(255.0)

[h_ref,w_ref] = img_ref_gray.shape

[h_tm,w_tm] = img_src_gray.shape

H_tm = int(h_tm/3)

W_tm = int(w_tm/3)

ex_raTIo = 0

Img_tm_gray_ex = np.zeros([h_ref+h_tm*ex_raTIo,w_ref+w_tm*ex_raTIo])

Img_tm_gray = np.copy(img_src_gray[h_tm:h_tm+h_tm,w_tm:w_tm+w_tm])

Img_tm_gray_ex[0:h_tm,0:w_tm] = np.copy(img_src_gray[h_tm:h_tm+h_tm,w_tm:w_tm+w_tm])

Tm_sum = np.sum(img_tm_gray_ex[0:h_tm,0:w_tm])

Tm_sum_sq = np.sum(img_tm_gray_ex[0:h_tm,0:w_tm]*img_tm_gray_ex[0:h_tm,0:w_tm])

Img_tm_gray_ex[0:h_tm,0:w_tm] = img_tm_gray_ex[0:h_tm,0:w_tm] - tm_sum/(h_tm*w_tm) #去均值

In_ref = calcIntag(img_ref_gray)

In_ref_sq = calcIntag(img_ref_gray*img_ref_gray)

Img_ref_gray_ex = np.zeros([h_ref+h_tm*ex_raTIo,w_ref+w_tm*ex_ratio])

Img_ref_gray_ex[0:h_ref,0:w_ref] = img_ref_gray[:,:]

# img_ref_gray_ex[0:h_ref-h_tm+1,0:w_ref-h_tm+1] = img_ref_gray_ex[0:h_ref-h_tm+1,0:w_ref-h_tm+1] #- ref_avg_mat #去均值

#傅里叶变换

F_tm = np.fft.fft2(-img_tm_gray_ex)

F_tm.real = -F_tm.real

F_ref = np.fft.fft2(img_ref_gray_ex)

F_w = F_tm * F_ref

Res = np.fft.ifft2(F_w).real#/((h_tm)*(w_tm))

Res_i = np.fft.ifft2(F_w).imag

Print(res_i)

Bg_h = 0

Bg_w = ​​0

Ref_div_sq = calcDivmat2(in_ref_sq,in_ref, h_tm, w_tm)

Tm_div = tm_sum_sq - (1.0/(h_tm*w_tm))*tm_sum*tm_sum

Res = res[bg_h:bg_h+h_ref-h_tm+1,bg_w:bg_w+w_ref-w_tm+1]/(np.sqrt(ref_div_sq*tm_div))

Print(np.max(res),np.min(res))

Print(img_ref_gray.dtype,img_tm_gray.dtype)

Res2 = cv2.matchTemplate(np.float32(img_ref_gray), np.float32(img_tm_gray),method=5)

Print(np.max(res2),np.min(res2))

Plt.figure()

Plt.subplot(221), plt.imshow(res,cmap='gray')

Plt.subplot(222), plt.imshow(res2,cmap='gray')

Plt.subplot(223), plt.imshow(in_ref,cmap='gray')

Plt.subplot(224), plt.imshow(in_ref_sq,cmap='gray')

Plt.show()

Crane Control Station

Crane Control Station also called Rainproof Pushbutton Switches, is a newly developed products, which is beautiful , strong and light, reliable struction, advanced and big contact capacity; mechanical interlocking , and if necessary, it can achieve interock circuit to assure the more safety and reliable using . The products matched cable jacket and waterproof rubber sealing strip, have a good (IP65 level) performance of dust and rain. So it most suitable for the remote controll in outdoor, rainy and snowy, dusty and damp environment, such as electric hoist, open conveyor blt, building line etc.

Crane Control Station

Crane Control Station,Crane Control Push Button Switch,Pendant Control Stations,Wireless Crane Control System

Ningbo Bond Industrial Electric Co., Ltd. , https://www.bondelectro.com

Posted on