label_json

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


register_name = []
register_hsi = []
register_rgb = []

class Label_Json:
    def find_matrix(rootname, name, namelist):
        idx = -1
        exact_match = -1
        i = 0
        for curname in namelist:
            if rootname in curname:
                idx = i
                break
            i += 1
        i = 0
        for curname in namelist:
            if name == curname:
                exact_match = i
            i += 1
        if exact_match >= 0:
            return exact_match
        else:
            return idx



    fregister = open('/pytorch-3dunet-master-zr_rgb_and_hsi/pytorch3dunet/datasets/data_label_company/data_coordinate.txt', 'r')
    flog = open('/pytorch-3dunet-master-zr_rgb_and_hsi/pytorch3dunet/datasets/data_label_company/log.txt', 'w')  #raw


    for eachline in fregister.readlines():
        if "RGB" in eachline:
            points = re.split(' ', eachline)
            point1 = re.split(',', re.split(':', points[0])[1])
            point2 = re.split(',', points[1])
            point3 = re.split(',', points[2])
            parr = np.float32(
                [[np.float64(point1[0]), np.float64(point1[1])], [np.float64(point2[0]), np.float64(point2[1])], [np.float64(point3[0]), np.float64(point3[1])]])
            register_rgb.append(parr)

        elif "HSI" in eachline:
            points = re.split(' ', eachline)
            point1 = re.split(',', re.split(':', points[0])[1])
            point2 = re.split(',', points[1])
            point3 = re.split(',', points[2])
            parr = np.float32(
                [[np.float64(point1[0]), np.float64(point1[1])], [np.float64(point2[0]), np.float64(point2[1])], [np.float64(point3[0]), np.float64(point3[1])]])
            register_hsi.append(parr)

        else:
            register_name.append(eachline)
    print(register_name)

    fp = open('/pytorch-3dunet-master-zr_rgb_and_hsi/pytorch3dunet/datasets/data_label_company/all.txt', 'r')  #raw file
    rootpath = ''
    for eachline in fp.readlines():
        print(eachline)
        flog.write(eachline)

        path, name = os.path.split(eachline)
        sampleName = name[:-5]
        sampleRootName = sampleName[:-5]
        LNP = os.path.split(os.path.split(path)[0])[1]
        idx = find_matrix(sampleRootName, sampleName, register_name)
        hsi_img_path = '/pytorch-3dunet-master-zr_rgb_and_hsi/pytorch3dunet/datasets/data_label_company/1/' + sampleName + '.png'
        rgb_img_path = '/pytorch-3dunet-master-zr_rgb_and_hsi/pytorch3dunet/datasets/data_label_company/1/' + sampleName + '.jpg'
        hsi_img_save_path = '/pytorch-3dunet-master-zr_rgb_and_hsi/pytorch3dunet/datasets/data_label_company/' + sampleName + '_save.png'
        rgb_img_save_path = '/pytorch-3dunet-master-zr_rgb_and_hsi/pytorch3dunet/datasets/data_label_company/' + sampleName + '_save.jpg'
        # re_label_hsi_img_save_path = '/labeling_data/' + LNP + '/' + sampleName + '.bmp'
        # re_label_rgb_img_save_path = '/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)
        if idx == -1:
            print("not find matrix, please label the registration !\n")
            flog.write("not find matrix, please label the registration !\n")
        else:
            hsi_points = register_hsi[idx]
            rgb_points = register_rgb[idx]
            print(hsi_points)
            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)
    flog.close()








'''#030406-20x-roi1
pts1 = np.float32([[156, 124], [1158, 845], [1155, 26]]) #rgb
pts2 = np.float32([[85, 100], [1125, 970], [1186, 66]]) #hsi
#030406c_2-20x-roi1
pts1 = np.float32([[327, 45], [1150, 209], [219, 870]]) #rgb
pts2 = np.float32([[293, 13], [1175, 284], [85, 905]]) #hsi
#030447-20x-roi4
pts1 = np.float32([[160, 190], [1141, 108], [397, 753]]) #rgb
pts2 = np.float32([[94, 155], [1173, 172], [294, 800]]) #hsi
#test
pts1 = np.float32([[326, 601], [1151, 209], [697, 776]]) #rgb
pts2 = np.float32([[232, 624], [1174, 285], [615, 856]]) #hsi

M = cv2.getAffineTransform(pts2, pts1)
# = np.array([[0.91, -0.085, 150], [0.072, 0.91, 50]])

rootpath = "/data1/zhourong/data/cholangiocarcinoma/processed/L/img_rgb_resize_w1365h1024/"
paths = os.listdir(rootpath)
for eachline in paths:
    eachline = "030406c_2-20x-roi1.jpg"
    filepath = os.path.join(rootpath, eachline)
    img = cv2.imread(filepath)
    img = np.flip(img, 0)
    savepath = "/data1/zhourong/data/cholangiocarcinoma/processed/tmp/L/" + eachline + "-.jpg"
    rows,cols,ch = img.shape
    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, :] = img[new_y, new_x, :]
    cv2.imwrite(savepath, dst)
a =0'''

'''fp = open('/data4/zhourong/workspace_pytorch/pytorch-3dunet-master-zr_rgb_and_hsi/exp/multifusion/train_2021-08-13_23-32-05.log', 'r')
op = open('/data4/zhourong/workspace_pytorch/pytorch-3dunet-master-zr_rgb_and_hsi/exp/multifusion/train_val_loss_miou.csv', 'w')

val_loss = []
val_mIou = []
train_loss = []
train_mIou = []

for eachline in fp.readlines():
        if "Validation finished. Loss:" in eachline:
            lines = re.split(' ', eachline)
            loss = round(float(lines[9][:-1]), 4)
            miou = round(float(lines[12][:-1]), 4)
            val_loss.append(loss)
            val_mIou.append(miou)
        if "Training stats. Loss:" in eachline:
            lines = re.split(' ', eachline)
            loss = round(float(lines[9][:-1]), 4)
            miou = round(float(lines[12][:-1]), 4)
            train_loss.append(loss)
            train_mIou.append(miou)
op.write("train loss:\n")
for i in train_loss:
    op.write(str(i)+',')
op.write("\ntrain miou:\n")
for i in train_mIou:
    op.write(str(i)+',')
op.write("\nval loss:\n")
for i in val_loss:
    op.write(str(i)+',')
op.write("\nval miou:\n")
for i in val_mIou:
    op.write(str(i)+',')'''

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值