-
Notifications
You must be signed in to change notification settings - Fork 1
/
brainExtraction.py
66 lines (58 loc) · 2.1 KB
/
brainExtraction.py
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
import cv2
import numpy as np
import os
import glob
from matplotlib import pyplot as plt
from PIL import Image
cwd = os.getcwd()
pathToDataFolder = cwd+'\\testPatient'
task1 = '\Slices'
pathToTask1 = cwd+task1
if not os.path.exists(pathToTask1):
os.makedirs(pathToTask1)
for globalImage in glob.glob('%s\*_thresh.png' % pathToDataFolder):
words = globalImage.split('_')
global_image_number = words[1]
org_img = cv2.imread(globalImage)
img_gray = cv2.cvtColor(org_img, cv2.COLOR_BGR2GRAY)
template = cv2.imread('template.png',0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
xstart = -1
xdiff = -10
ystart = -1
ydiff = -10
for pt in zip(*loc[::-1]):
xtemp = pt[0]
ytemp = pt[1]+h
if(xstart == -1):
xstart = xtemp
if(ystart == -1):
ystart = ytemp
if(xdiff == -10 and xstart != xtemp):
xdiff = xtemp - xstart
if(ydiff == -10 and ystart != ytemp):
ydiff = ytemp - ystart
if(xstart != -1 and ystart != -1 and xdiff != -10 and ydiff != -10):
break
pathToEachImageFolder = pathToTask1+"\\"+str(global_image_number)
if not os.path.exists(pathToEachImageFolder):
os.makedirs(pathToEachImageFolder)
os.chdir(pathToEachImageFolder)
img1 = Image.open(globalImage)
width, height = img1.size
loc_img_count = 0
for y0 in range(ystart, height, ydiff):
for x0 in range(xstart, width, xdiff):
if(x0+xdiff < width and y0+ydiff < height):
box = (x0+5, y0,
x0+xdiff if x0+xdiff < width else width,
y0+ydiff if y0+ydiff < height else height)
img2 = img1.crop(box)
extrema = img2.convert("L").getextrema()
if extrema != (0, 0):
loc_img_count = loc_img_count + 1
img1.crop(box).save('%d.png' % loc_img_count)
os.chdir(cwd)