-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_nodules4.py
129 lines (100 loc) · 3.81 KB
/
count_nodules4.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
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
from __future__ import print_function
import cv2 as cv
import numpy as np
import argparse
# to get neighbours
def getAdjacentNodes(shape, i, j, k):
neighbours = {}
if i > 0:
neighbours['N'] = (i - 1, j)
if j > 0:
neighbours['W'] = (i, j - 1)
# four node
if k == 4:
return neighbours
if i > 0 and j > 0:
neighbours['NW'] = (i - 1, j - 1)
if i > 0 and j < shape[1] - 1:
neighbours['NE'] = (i - 1, j + 1)
return neighbours
def twoPass(img, size, nodes, frontColor):
labels = {0: 0}
labeled_img = np.zeros(img.shape, dtype=np.uint64)
bg_color = 0 + 255 * (1 - frontColor)
if len(img.shape) > 2:
img = img[:, :, 0]
#first Pass
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if img[i, j] == bg_color:
continue
neighboursList = getAdjacentNodes(img.shape, i, j, nodes)
label_group = set()
combineLabelsGroup(label_group, labeled_img, neighboursList)
if len(label_group) == 0:
labeled_img[i, j] = max(labels) + 1
label_group.add(labeled_img[i, j])
else:
labeled_img[i, j] = min(label_group)
label_group = MergeEquivalence(label_group, labels)
for label in label_group:
labels[label] = label_group
#second pass
for i in range(labeled_img.shape[0]):
for j in range(labeled_img.shape[1]):
if labeled_img[i, j] == 0:
continue
labeled_img[i, j] = min(labels[labeled_img[i, j]])
for label in np.unique(labeled_img):
if labeled_img[labeled_img == label].size < size:
labeled_img[labeled_img == label] = 0
return labeled_img
def MergeEquivalence(label_group, labels):
for label in label_group:
if label not in labels:
continue
label_group = label_group.union(labels[label])
return label_group
def combineLabelsGroup(label_group, labeled_img, neighboursList):
for n in neighboursList:
if labeled_img[neighboursList[n]] > 0:
label_group.add(labeled_img[neighboursList[n]])
def printNodules(image):
B = [75, 75, 25, 200, 48, 180, 240, 230, 60, 190, 128, 255, 40, 200, 0, 195, 0, 180, 128, 128, 255]
G = [25, 180, 225, 130, 130, 30, 240, 50, 245, 190, 128, 190, 110, 250, 0, 255, 128, 215, 0, 128, 255]
R = [230, 60, 255, 0, 245, 145, 70, 240, 210, 250, 0, 230, 170, 255, 128, 170, 128, 255, 0, 128, 255]
colors = np.stack((B, G, R), axis=-1)
colormap = {}
counter = 0
for label in np.unique(image):
if label == 0:
colormap[label] = np.array([0, 0, 0])
else:
colormap[label] = colors[counter % colors.shape[0]]
counter += 1
shape = image.shape
coloredImage = np.zeros((image.shape[0], image.shape[1], 3), dtype=np.uint8)
for r in range(image.shape[0]):
for c in range(image.shape[1]):
coloredImage[r, c, :] = colormap[image[r, c]]
return coloredImage
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Example with long option names')
parser.add_argument('--input', action="store")
parser.add_argument('--size', action='store')
parser.add_argument('--optional_output', action="store")
results = parser.parse_args()
grid = 0
if not results.input:
print('Required input argumenst : input_image')
exit(1)
if not results.size:
print('Required grid size')
grid = int(results.size)
exit(1)
img = cv.imread(results.input,0)
labeledImg = twoPass(img, grid, 4, 0)
print(len(np.unique(labeledImg)) - 1)
coloredImage = printNodules(labeledImg)
if results.optional_output:
cv.imwrite(results.optional_output, coloredImage)