图像配准 仿射变换

import re
from numpy import *
import cv2
import numpy as np
import os
import json

def getlabelingdata(file):
    with open(file, encoding='utf-8') as f:
        contents = json.load(f)
        # print(contents)
        dict = contents['markResult']['objects']
        dict = dict[0]
        dict = dict['features']
        dict = dict[0]
        dict = dict['geometry']['coordinates']
        labelpoint = np.array(dict).astype(np.float32)
        return labelpoint

flog = open('/data/cholangiocarcinoma/processed/yuxi_label_data/log.txt', 'w')

LNP = 'P'
root = '/data/cholangiocarcinoma/processed/datatang_label_data/P/json/'
for eachfile in os.listdir(root):
    hsi_file = root + eachfile
    hsi_file = '/data/cholangiocarcinoma/processed/datatang_label_data/P/json/h2-3-20x-roi6.png.json'
    eachfile = os.path.split(hsi_file)[1]
    if 'png.json' in hsi_file:
        name = eachfile.split('.')
        rgb_name = name[0] + '.' + name[2]
        rgb_file = root + rgb_name
        hsi_points = getlabelingdata(hsi_file)
        rgb_points = getlabelingdata(rgb_file)

        hsi_img_path = '/data_ssd/zhangmingjie/pytorch-3dunet-master-zr_20210621/visualization/' + LNP + '/' + name[0] + '_hsi_band_30.png'
        rgb_img_path = '/data/cholangiocarcinoma/processed/' + LNP + '/img_rgb_resize_w1365h1024/'  + name[0] + '.jpg'
        hsi_img_save_path = '/data/cholangiocarcinoma/processed/datatang_label_data/check/re_re/' + name[0] + '.png'
        rgb_img_save_path = '/data/cholangiocarcinoma/processed/datatang_label_data/check/re_re/' + name[0] + '_registered.jpg'
        #re_label_hsi_img_save_path = '/data/cholangiocarcinoma/processed/yuxi_label_data/labeling_data/' + LNP + '/' + sampleName + '.bmp'
        #re_label_rgb_img_save_path = '/data/cholangiocarcinoma/processed/yuxi_label_data/labeling_data/' + LNP + '/' + sampleName + '.jpg'
        hsi_img = cv2.imread(hsi_img_path)
        rgb_img = cv2.imread(rgb_img_path)
        rgb_img = np.flip(rgb_img, 0)
        cv2.imwrite(hsi_img_save_path, hsi_img)
        #cv2.imwrite(re_label_hsi_img_save_path, hsi_img)
        #cv2.imwrite(re_label_rgb_img_save_path, rgb_img)

        M = cv2.getAffineTransform(hsi_points, rgb_points)
        dst = np.zeros([1024, 1280, 3], dtype=np.uint8)
        for i in range(1024):
            for j in range(1280):
                new_x = int(j * M[0][0] + i * M[0][1] + M[0][2] + 0.5)
                new_y = int(j * M[1][0] + i * M[1][1] + M[1][2] + 0.5)
                if new_x >= 0 and new_x < 1365 and new_y >= 0 and new_y < 1024:
                    dst[i, j, :] = rgb_img[new_y, new_x, :]

        cv2.imwrite(rgb_img_save_path, dst)
    break
flog.close()




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值