File size: 6,007 Bytes
6107278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import cv2 as cv
import lpips
import numpy as np
import os

from config import config
from openpyxl import Workbook

import torch
from skimage.metrics import structural_similarity as ssim
from math import log10, sqrt
import torchvision.transforms as transforms
from decimal import Decimal

lplps_model = lpips.LPIPS()


def cal_BSR(cxr_path, gt_path, bs_path):
    cxr = cv.imread(cxr_path, 0)
    gt = cv.imread(gt_path, 0)
    bs = cv.imread(bs_path, 0)

    cxr = cxr / 255
    gt = gt / 255
    bs = bs / 255

    bone = cv.subtract(cxr, gt)

    gt = cv.resize(gt, (config.image_size, config.image_size))
    bs = cv.resize(bs, (config.image_size, config.image_size))
    bone = cv.resize(bone, (config.image_size, config.image_size))

    bs += np.average(cv.subtract(gt, bs))

    bias = cv.subtract(gt, bs)
    bias[bias < 0] = 0

    BSR = 1 - np.sum(bias ** 2) / np.sum(bone ** 2)
    return BSR


def cal_MSE(gt_path, bs_path):
    gt = cv.imread(gt_path, 0)
    bs = cv.imread(bs_path, 0)

    gt = cv.resize(gt, (config.image_size, config.image_size))
    bs = cv.resize(bs, (config.image_size, config.image_size))

    # MSE = np.mean((gt - bs) ** 2)
    MSE = 2 * lpips.l2(gt, bs)
    return MSE


def cal_SSIM(gt_path, bs_path):
    gt = cv.imread(gt_path, 0)
    bs = cv.imread(bs_path, 0)

    gt = gt / 255
    bs = bs / 255

    gt = cv.resize(gt, (config.image_size, config.image_size))
    bs = cv.resize(bs, (config.image_size, config.image_size))

    SSIM = ssim(gt, bs, channel_axis=None, data_range=1)

    return SSIM


def cal_PSNR(gt_path, bs_path):
    mse = cal_MSE(gt_path, bs_path)
    max_pixel = 1

    PSNR = 20 * log10(max_pixel / sqrt(mse))
    return PSNR


def cal_LPIPS(gt_path, bs_path):
    gt = cv.imread(gt_path, 0)
    bs = cv.imread(bs_path, 0)

    gt = cv.resize(gt, (config.image_size, config.image_size))
    bs = cv.resize(bs, (config.image_size, config.image_size))

    gt = transforms.ToTensor()(gt)
    bs = transforms.ToTensor()(bs)

    gt = torch.unsqueeze(gt, dim=0)
    bs = torch.unsqueeze(bs, dim=0)

    LPIPS = lplps_model(gt, bs).item()

    return LPIPS


if __name__ == "__main__":
    wb = Workbook()

    ws = wb.active

    CXR_path = "SZCH-X-Rays-741/CXR"
    GT_path = "SZCH-X-Rays-741/BS"
    BS_path = "YOUR BONE SUPPRESSION RESULT"

    BSR_list = []
    MSE_list = []
    SSIM_list = []
    PSNR_list = []
    LPIPS_list = []
    # ws.append(["Filename", "BSR", "MSE", "SSIM", "PSNR", "LPIPS"])
    txt = 'SZCH-X-Rays_testset.txt'
    with open(txt, 'r', encoding='utf-8') as file:
        lines = file.readlines()
    file_names = [line.strip() for line in lines]

    for filename in os.listdir(BS_path):
        if filename in file_names:
            pass
        else:
            continue
        cxr_path = os.path.join(CXR_path, filename)
        gt_path = os.path.join(GT_path, filename)
        bs_path = os.path.join(BS_path, filename)

        BSR = cal_BSR(cxr_path, gt_path, bs_path)
        MSE = cal_MSE(gt_path, bs_path)
        SSIM = cal_SSIM(gt_path, bs_path)
        PSNR = cal_PSNR(gt_path, bs_path)
        LPIPS = cal_LPIPS(gt_path, bs_path)

        BSR_list.append(BSR)
        MSE_list.append(MSE)
        SSIM_list.append(SSIM)
        PSNR_list.append(PSNR)
        LPIPS_list.append(LPIPS)

        bs = cv.imread(bs_path, 0)

        bs = cv.resize(bs, (config.image_size, config.image_size))

        bs = transforms.ToTensor()(bs)
        bs = transforms.Normalize([0.5], [0.5])(bs)

        print(f"{filename} LPIPS:{LPIPS} MSE: {MSE} PSNR:{PSNR} SSIM:{SSIM} BSR: {BSR}")
        ws.append([filename, LPIPS, MSE, PSNR, SSIM, BSR])

    ws.append(["Mean",
               np.mean(np.array(LPIPS_list)),
               np.mean(np.array(MSE_list)),
               np.mean(np.array(PSNR_list)),
               np.mean(np.array(SSIM_list)),
               np.mean(np.array(BSR_list))])
    ws.append(["Std",
               np.std(np.array(LPIPS_list)),
               np.std(np.array(MSE_list)),
               np.std(np.array(PSNR_list)),
               np.std(np.array(SSIM_list)),
               np.std(np.array(BSR_list))])
    print("Average LPIPS:", np.mean(np.array(LPIPS_list)), "Std:", np.std(np.array(LPIPS_list)))
    print("Average MSE:", np.mean(np.array(MSE_list)), "Std:", np.std(np.array(MSE_list)))
    print("Average PSNR:", np.mean(np.array(PSNR_list)), "Std:", np.std(np.array(PSNR_list)))
    print("Average SSIM:", np.mean(np.array(SSIM_list)), "Std:", np.std(np.array(SSIM_list)))
    print("Average BSR:", np.mean(np.array(BSR_list)), "Std:", np.std(np.array(BSR_list)))

    print("LPIPS:", Decimal(str(np.mean(np.array(LPIPS_list)))).quantize(Decimal("0.001"), rounding="ROUND_HALF_UP"),
          "$\pm$", Decimal(str(np.std(np.array(LPIPS_list)))).quantize(Decimal("0.001"), rounding="ROUND_HALF_UP"))
    print("MSE:", Decimal(str(np.mean(np.array(MSE_list)) * 1000)).quantize(Decimal("0.001"), rounding="ROUND_HALF_UP"),
          "$\pm$", Decimal(str(np.std(np.array(MSE_list)) * 1000)).quantize(Decimal("0.001"), rounding="ROUND_HALF_UP"))
    print("PSNR:", Decimal(str(np.mean(np.array(PSNR_list)))).quantize(Decimal("0.001"), rounding="ROUND_HALF_UP"),
          "$\pm$", Decimal(str(np.std(np.array(PSNR_list)))).quantize(Decimal("0.001"), rounding="ROUND_HALF_UP"))
    print("SSIM:",
          Decimal(str(np.mean(np.array(SSIM_list)) * 100)).quantize(Decimal("0.001"), rounding="ROUND_HALF_UP"),
          "$\pm$", Decimal(str(np.std(np.array(SSIM_list)) * 100)).quantize(Decimal("0.001"), rounding="ROUND_HALF_UP"))
    print("BSR:", Decimal(str(np.mean(np.array(BSR_list)) * 100)).quantize(Decimal("0.001"), rounding="ROUND_HALF_UP"),
          "$\pm$", Decimal(str(np.std(np.array(BSR_list)) * 100)).quantize(Decimal("0.001"), rounding="ROUND_HALF_UP"))

    wb.save("sample_m.xlsx")