-
Notifications
You must be signed in to change notification settings - Fork 3
/
L2_Segmentation_v5.py
180 lines (157 loc) · 5.88 KB
/
L2_Segmentation_v5.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
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
178
179
180
import cv2, numpy as np
from Area import areaThreshold_by_havg, areaThreshold_by_top
from threshold import otsu_threshold
from _8connected import get_8connected_v2
from util_ import *
import warnings
import traceback
from eclipes_test import elliptic_fourier_descriptors, efd
warnings.filterwarnings("error")
import time
color = {i: np.random.randint(20, 255, 3) for i in range(5, 5000)}
color[1] = [255, 255, 255]
color[2] = [0, 0, 255]
def make_border(points, shape, bval=255):
# h,w = shape
boundry = np.zeros(shape, dtype=np.uint8)
# boundry = padding2D_zero(boundry,2)
boundry[points[0][0],points[0][1]] = bval
i=0
x,y = points[0]
while i < len(points)-1:
try:
boundry[x, y] = bval
except IndexError:
x1=x;y1=y
if x >= boundry.shape[0]:
x1 = boundry.shape[0]-1
if y >= boundry.shape[1]:
y1 = boundry.shape[1]-1
boundry[x1, y1] = bval
# traceback.print_exc()
if abs(points[i+1][0] - x) <=1 and abs(points[i+1][1] - y) <=1:
i+=1
x,y = points[i]
elif abs(points[i+1][0] - x) > 1:
x ,y = int(x + (points[i+1][0] - x)/abs(points[i+1][0] - x)), y
# x ,y = int(x + 1), y
elif abs(points[i+1][1] - y) > 1:
x ,y = x, int(y + (points[i+1][1] - y)/abs(points[i+1][1] - y))
# x ,y = x, int(y + 1)
# boundry = remove_padding2D_zero(boundry, 2)
return boundry
def mask_by_border(boundry, ival):
h,w = boundry.shape
inside = 0
b1=np.int0(boundry)
for i in range(h):
# try:
val = np.ones(np.argmax(b1[i,:])) * 2
b1[i,:len(val)] = val
val1 = np.ones(np.argmax(b1[i,::-1])) *2
b1[i,w-len(val1):] = val1
for i in range(w):
val = np.ones(np.argmax(b1[::-1,i])) * 2
b1[h-len(val):,i] = val
val = np.ones(np.argmax(b1[:,i])) * 2
b1[:len(val),i] = val
b1 = ((b1 - boundry)/-2 + 1) * ival
return b1
def L2_segmentation_2(iimg , T, index):
h, w, _ = iimg.shape
# cv2.imshow('image', iimg)
t0 = time.time()
gray = iimg[:, :, 2]
# cv2.imshow('gray', gray)
thresh = np.array([[0 if pixel < T else 255 for pixel in row] for row in gray], dtype=np.uint8)
sober = sober_operation(gray)
# cv2.imshow('sober', sober)
# print("\tsober operation", time.time() - t0)
# t0 = time.time()
sober = cv2.fastNlMeansDenoising(sober, None, h=2, templateWindowSize=3, searchWindowSize=5)
# cv2.imshow('sober cleaned', sober)
# print("\tnoise operation", time.time() - t0)
# t0 = time.time()
T= otsu_threshold(sober)
# print("\tsober threshold", time.time() - t0)
# t0 = time.time()
sthresh = np.array([[0 if pixel < T else 255 for pixel in row] for row in sober], dtype=np.uint8)
# cv2.imshow('sober Threshold', sthresh)
# print("\tcalc threshold", time.time() - t0)
# t0 = time.time()
diluted = cv2.dilate(sthresh, kernel=np.ones((5,5), np.uint8), iterations=1)
# cv2.imshow('dilutated2 ', diluted)
# print("\tdilation operation", time.time() - t0)
# t0 = time.time()
thresh2 = np.where((thresh == 0) * (diluted == 255), 0, thresh-diluted)
# cv2.imshow('Thresh - dilute ', thresh2)
mask = get_8connected_v2(thresh=thresh2, mcount=index)
# display_mask("Diluted mask", mask)
# print("\tmask foamation", time.time() - t0)
# t0 = time.time()
# Calcutaing the grain segment using mask image
s = cal_segment_area(mask)
# print("\tcalc area seg", time.time() - t0)
# t0 = time.time()
rmcount = 0
if len(s) < 2:
# print
return None
low_Tarea, up_Tarea = areaThreshold_by_top(s, 3)
slist = list(s)
for i in slist:
area = (s[i][0] - s[i][1]) * (s[i][2] - s[i][3])
if area < low_Tarea:
s.pop(i)
rmcount += 1
if len(s) < 2:
# print
return None
# print("\tselecting area", time.time() - t0)
# t0 = time.time()
# removing unwanted masks
mask = np.array([[0 if pixel not in s else pixel for pixel in row] for row in mask])
# print("\tremoving unwanted mask opeation", time.time() - t0)
# t0 = time.time()
# Adding boundry mask
boundry = get_boundry_img_matrix(thresh, 1)
# print("\tgetting boundry", time.time() - t0)
# t0 = time.time()
mask = np.where(boundry == 1, 1, mask)
# print("\tadding boundry to mask opeation", time.time() - t0)
# t0 = time.time()
# display_mask('boundried mask', mask)
# using mask fill the mask values in boundry
mask = flood_filling(mask)
# print("\tflood filling opeation", time.time() - t0)
# t0 = time.time()
# display_mask('flood fill', mask)
# replace boundry by respective mask value
mask = boundry_fill(mask)
# print("\tfilling opeation", time.time() - t0)
# t0 = time.time()
# cv2.waitKey()
masks =[]
for ii in s:
img = get_mask_value_area(gray, mask, ii)
# b1 = get_boundry_img_matrix(img)
# b2 = get_boundry_img_matrix(get_mask_value_area(boundry, mask, i),bval=255)
# img = b1-b2
points = get_boundry_as_points(img)
img = get_boundry_img_matrix(img, bval=255)
# cv2.imshow("img %d" % (ii), img)
coff = elliptic_fourier_descriptors(points,order=5)
if coff is None:
print("Ellipsis not work")
return None
x, y = np.int0(efd(coff, contour_1=points, locus=np.mean(points, axis=0)))
coordinates = [(x[i], y[i]) for i in range(len(x))]
boundry = make_border(coordinates, img.shape, bval=255)
# cv2.imshow("border %d"%(ii), boundry)
mask1 = mask_by_border(boundry, ii)
# display_mask("mask %d" % (ii), mask1)
masks.append(mask1)
# print("\telliptical fitting operation", time.time() - t0,'\n')
# cv2.waitKey()
# cv2.destroyAllWindows()
return masks, rmcount