利用快速傅里叶变换计算相关面

 Fast Template Matching,J. P. 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_ra  TI o = 0

img_tm_gray_ex = np.zeros([h_ref+h_tm*ex_ra  TI o,w_ref+w_tm*ex_ra  TI o])

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_ra  TI o,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()

97
92
0
14

相关资讯

  1. 1、吴卓林欠房租半夜投靠妈妈吴绮莉,成龙却忙着发新歌:物是人非4381
  2. 2、帝国杂志2018年度最佳电影榜单,漫威稳居第一2086
  3. 3、唐嫣伴娘团定了!不是刘亦菲杨幂,而是这四位美丽俏佳人!721
  4. 4、日本恐怖片《来了》新预告,《告白》导演新作,胆小慎入!1440
  5. 5、《分歧者》制片人:第四部电影不会有了668
  6. 6、“灭霸”的女儿出生了,网友喊话:哥,别打响指了哦4754
  7. 7、她被称“童颜教主”,与阮经天传绯闻,35岁美如少女却无人娶?3011
  8. 8、路人偶遇谢霆锋与儿子相聚,谢振轩身高抢镜,谢霆锋笑的像小孩189
  9. 9、寡姐在《黑寡妇》电影中获得更高片酬,比其他漫威男明星要高!356
  10. 10、宫崎骏《龙猫》今日公映五大看点打造年度最暖188
全部评论(0)
我也有话说
0
收藏
点赞
顶部