-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwater2.py
62 lines (39 loc) · 1.35 KB
/
water2.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
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
import os
import numpy.ma as ma
from dataset import PairedDatasetSingle
'''
array = np.load('Data/Out_New/0001.npy')
plt.imshow(array[64])
plt.show()
'''
def extract_slice_mask(slice):
ret, thresh = cv.threshold(slice, 0.1, 1, cv.THRESH_BINARY)
num_labels, labels_im, stats, centroids = cv.connectedComponentsWithStats(thresh.astype('uint8'))
max = stats[1, 4]
largest_index = 1
for k in np.delete(np.unique(labels_im), 0):
size = stats[k, 4]
if (size > max):
max = size
largest_index = k
mask = ma.masked_not_equal(labels_im, largest_index)
return mask
pd = PairedDatasetSingle('Data/In/0', 'Data/Out_New', 10)
contour_slices = []
for i in range(0, 127):
tslice = pd.y_train[0][i]
thresh = extract_slice_mask(tslice).filled(0)
im2, contours, hierarchy = cv.findContours(thresh.astype(np.uint8), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
if len(contours) != 0:
print(len(contours))
#cv.drawContours(tslice, contours, -1, 255, 3)
yeet = cv.fillPoly(tslice, pts = np.array(contours[0]), color = (255, 255, 255))
contour_slices.append(yeet)
contour_slices = np.array(contour_slices)
print(contour_slices.shape)
for i in range(0, 127):
plt.imshow(contour_slices[i])
plt.show()