|
a |
|
b/Preprocessing/bayesnoiseremoval.py |
|
|
1 |
import cv2 |
|
|
2 |
import numpy as np |
|
|
3 |
from nilearn import plotting |
|
|
4 |
import nibabel as nib |
|
|
5 |
import matplotlib.pyplot as plt |
|
|
6 |
|
|
|
7 |
class BayesPreprocessor: |
|
|
8 |
def __init__(self, image): |
|
|
9 |
# store target image width, height and interpolation |
|
|
10 |
# inter is an optional argument |
|
|
11 |
self.image = image |
|
|
12 |
# self.height = height |
|
|
13 |
# self.kernel = kernel |
|
|
14 |
# self.inter = inter |
|
|
15 |
@staticmethod |
|
|
16 |
def bayes_noise_removal(self,o): |
|
|
17 |
|
|
|
18 |
# implement image processing |
|
|
19 |
S1=self.image |
|
|
20 |
S2 = 255 - S1 |
|
|
21 |
S3 = S1 + S2 |
|
|
22 |
dv = self.calculate_params(S1, S2, S3, o) |
|
|
23 |
|
|
|
24 |
dv[dv < 0] = 0 |
|
|
25 |
|
|
|
26 |
return dv |
|
|
27 |
|
|
|
28 |
def calculate_params(self, S1, S2, S3, o): |
|
|
29 |
try: |
|
|
30 |
w = S2 / S3 |
|
|
31 |
except ZeroDivisionError: |
|
|
32 |
w = 0 |
|
|
33 |
w = np.divide(S2, S3) |
|
|
34 |
p = np.divide(S3, w) |
|
|
35 |
try: |
|
|
36 |
p = S3 / w |
|
|
37 |
except ZeroDivisionError: |
|
|
38 |
p = 0 |
|
|
39 |
|
|
|
40 |
dv = np.log(np.i0((p * np.sinc(w) * S1) / o ** 2)) + np.log(np.i0((p * np.sinc(1 - w) * S2) / o ** 2)) - p * ( |
|
|
41 |
((np.sinc(w)) ** 2 + (np.sinc(1 - w)) ** 2) / (2 * o ** 2)) |
|
|
42 |
return dv |
|
|
43 |
|
|
|
44 |
# plt.imsave('slice.png',slice_2l) |
|
|
45 |
|
|
|
46 |
|
|
|
47 |
# cv2.imshow('dw.jpg',slice_2l) |
|
|
48 |
# cv2.waitKey() |
|
|
49 |
|
|
|
50 |
img = nib.load('../../mr_train/mr_train_1020_image.nii.gz') |
|
|
51 |
label=nib.load('../../mr_train/mr_train_1001_label.nii.gz') |
|
|
52 |
|
|
|
53 |
# display = plotting.plot_anat(img) |
|
|
54 |
# plotting.show() |
|
|
55 |
# plotting.plot_img(img) |
|
|
56 |
# plotting.show() |
|
|
57 |
# |
|
|
58 |
img_data=img.get_fdata() |
|
|
59 |
shape_0=int((img_data.shape[0]-1)/2) |
|
|
60 |
shape_1=int((img_data.shape[1]-1)/2) |
|
|
61 |
shape_2=int((img_data.shape[2]-1)/2) |
|
|
62 |
|
|
|
63 |
|
|
|
64 |
slice_1=img_data[shape_0,:,:] |
|
|
65 |
image=cv2.imread('../tmp/pred_0.png') |
|
|
66 |
image = cv2.resize(image, (128,512), interpolation=cv2.INTER_AREA) |
|
|
67 |
cv2.imshow('daw',image) |
|
|
68 |
cv2.waitKey(0) |
|
|
69 |
# bayes=BayesPreprocessor(image) |
|
|
70 |
|
|
|
71 |
# # image = image / 255 |
|
|
72 |
# cv2.imshow('dfdw',bayes.bayes_noise_removal(90)) |
|
|
73 |
# # plt.imshow(slice_1) |
|
|
74 |
# # plt.show() |
|
|
75 |
# cv2.waitKey(0) |
|
|
76 |
|
|
|
77 |
|
|
|
78 |
##img=cv2.imread("mri.png",0); |
|
|
79 |
# |
|
|
80 |
# # plt.figure(1) |
|
|
81 |
# S1=slice_1 |
|
|
82 |
# # plt.figure(2) |
|
|
83 |
# S2=255-S1 |
|
|
84 |
# S3=S1+S2 |
|
|
85 |
# |
|
|
86 |
# |
|
|
87 |
# # plt.figure(3) |
|
|
88 |
# try: |
|
|
89 |
# w = S2/S3 |
|
|
90 |
# except ZeroDivisionError: |
|
|
91 |
# w = 0 |
|
|
92 |
# w=np.divide(S2,S3) |
|
|
93 |
# p=np.divide(S3,w) |
|
|
94 |
# try: |
|
|
95 |
# p = S3/w |
|
|
96 |
# except ZeroDivisionError: |
|
|
97 |
# p = 0 |
|
|
98 |
# |
|
|
99 |
# |
|
|
100 |
# |
|
|
101 |
# # cv2.imshow("pikseli",np.uint8(p)) |
|
|
102 |
# # cv2.imwrite("pikseli.png",np.uint8(p)) |
|
|
103 |
# |
|
|
104 |
# o=40 |
|
|
105 |
# |
|
|
106 |
# dv=np.log(np.i0((p*np.sinc(w)*S1)/o**2))+np.log(np.i0((p*np.sinc(1-w)*S2)/o**2))-p*(((np.sinc(w))**2+(np.sinc(1-w))**2)/(2*o**2)) |
|
|
107 |
# # plt.figure(4) |
|
|
108 |
# dv[dv<0]=0 |
|
|
109 |
# cv2.imshow("popravljena.png",np.uint8(dv)) |
|
|
110 |
# cv2.waitKey(0) |