数据集准备: 数据集Camelyon16,数据集的介绍以及下载地址,请点击参考链接。 参考链接 切割数据集,引用百度NCRF项目的patch_gen.py文件 数据配准方法采用以下代码:
import cv2
import os
import argparse
parser = argparse.ArgumentParser(description='Generate hr_matched')
parser.add_argument('--template_path', default=None, type=str,
help='Path to the input directory of template files')
parser.add_argument('--target_path', default=None,
type=str, help='Path to the input directory of target files')
parser.add_argument('--hr_path', default=None, metavar='PATCH_PATH', type=str,
help='Path to the output directory of matched images')
def image_match(template, target):
h, w = template.shape[:2]
template = cv2.resize(template, (2*w, 2*h), interpolation=cv2.INTER_CUBIC )
theight, twidth = template.shape[:2]
result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED)
cv2.normalize(result, result, 0, 1, cv2.NORM_MINMAX, -1)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
hr = target[int(min_loc[0]):int(min_loc[0] + twidth), int(min_loc[1]): int(min_loc[1] + theight)]
return hr
def main():
args = parser.parse_args()
if not os.path.exists(args.hr_path):
os.mkdir(args.hr_path)
for lp in os.listdir(args.template_path):
hp = lp
template = cv2.imread(os.path.join(args.template_path, lp))
target = cv2.imread(os.path.join(args.target_path, hp))
hr = image_match(template, target)
path = os.path.join(args.hr_path, hp)
cv2.imwrite(path, hr)
if __name__ == '__main__':
main()
参考博文: cv2 模板匹配函数
|