Repository navigation
Expand file tree
/
Copy pathHeadMaskGenerator.py
More file actions
75 lines (57 loc) · 1.93 KB
/
Copy pathHeadMaskGenerator.py
File metadata and controls
75 lines (57 loc) · 1.93 KB
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
# # import Dependencies
import sys
import os
from os import path
import matplotlib.pyplot as plt
import numpy as np
import warnings
sys.path.append(path.abspath('./util'))
from AnDiffusion import *
from MaskGenAlgo import *
from tqdm import tqdm
from hysteresisThresholding import apply_hysteresis_threshold
from time import sleep
from skimage.morphology import erosion, dilation, opening, closing, white_tophat
from skimage.morphology import disk
from ImageFinder import *
from scipy.stats import norm
from scipy.ndimage.morphology import binary_fill_holes
#print("Enter image folder path: ")
#Ipath = input()
#print("Enter path where to save the results: ")
#Spath = input()
#Dicom location
Ipath = "./sample_mri_image(DICOM)/"
Spath = "./OutputImg/"
print("Loading images .......... ")
l = filelocation(Ipath)
[orgImge,nofimage] = LoadOrginalImage(l)
print("Done! no of images: " + str(nofimage))
print("Apply pipeline for generate masks.....")
mask = np.zeros((512,512,nofimage))
fgnd = np.zeros((512,512,nofimage))
actImg = np.zeros((512,512,nofimage))
selem = disk(4)
for i in tqdm(range(nofimage)):
img = orgImge[:,:,i]
diff = anisodiff(img,20,50,0.1)
mu,sigma = norm.fit(diff)
htr = apply_hysteresis_threshold(diff,mu,sigma).astype(int)
pmask = binary_fill_holes(htr)
eroded = erosion(pmask, selem)
[fg,bg] = foregroundBackground(eroded,img)
mask[:,:,i] = eroded
fgnd[:,:,i] = fg
actImg[:,:,i] = img
sleep(0.1)
print("save all generate image in a folder.... ")
dictr = Spath
for i in tqdm(range(nofimage)):
x=l[i].split("/")
loc1=dictr+x[-1]+'.png'
loc2=dictr+x[-1]+'fg'+'.png'
loc3=dictr+x[-1]+'actImg'+'.png'
plt.imsave(loc1,mask[:,:,i],cmap = plt.cm.gray) # save psudo mask
plt.imsave(loc2,fgnd[:,:,i],cmap = plt.cm.gray)
plt.imsave(loc3,actImg[:,:,i],cmap = plt.cm.gray)
print("Done! ")