-
Notifications
You must be signed in to change notification settings - Fork 1
/
soln.py
84 lines (67 loc) · 2.8 KB
/
soln.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
import os
import numpy as np
import cv2 as cv
import pandas as pd
from matplotlib import pyplot as plt
root = os.getcwd()
data_folder = 'input'
data_path = os.path.join(root,data_folder)
output_folder = 'output'
output_path = os.path.join(root,output_folder)
if not os.path.exists(output_path):
os.makedirs(output_path)
def draw(img,fn):
fig,ax = plt.subplots()
ax.axis('off')
ax.imshow(img,cmap='gray')
fig.savefig(os.path.join(output_path,fn),bbox_inches='tight')
plt.close()
def draw_hist(data,fn):
fig,ax = plt.subplots()
(n,bins,patches) = ax.hist(data)
fig.savefig(os.path.join(output_path,'hist-'+fn),bbox_inches='tight')
plt.close()
output_dict = {
'File': [],
'Number of Cells': []
}
for _,subfolders,files in os.walk(data_path):
for file in files:
print('{:-^50}'.format('FILE ' + file))
img_file = os.path.join(data_path,file)
img = cv.imread(img_file)
gray_img = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
gray_data = gray_img.flatten()
hist_data = np.histogram(gray_data)
mx_freq_idx = np.argmax(hist_data[0])
mn_freq_idx = np.argmin(hist_data[0])
t_mn = hist_data[1][mn_freq_idx+1] # Lower value of the range of bin (x-axis) with the highest frequency (y-axis)
t_mx = hist_data[1][mx_freq_idx]
# t = hist_data[1][mn_freq_idx]
# print(hist_data[0].shape,hist_data[1].shape)
# print('max idx: {} ---> {} | min idx: {} ----> {}'.format(mx_freq_idx,t_mx,mn_freq_idx,t_mn))
# print(hist_data[0])
# print(hist_data[1])
draw_hist(gray_data,file)
# edges = cv.Canny(gray_img,100,200) # Canny edge mask instead of blur and threshold to binary image?
blur_img = cv.GaussianBlur(gray_img, (5, 5), 0)
(_, binary_img) = cv.threshold(blur_img,t_mx,255, cv.THRESH_BINARY_INV)
# (_, binary_img) = cv.threshold(blur_img, 200, 255, cv.THRESH_BINARY)
# (_, binary_img) = cv.threshold(blur_img, 240, 255, cv.THRESH_BINARY)
# (_, binary_img) = cv.threshold(blur_img,200,255, cv.THRESH_BINARY_INV)
# binary_img = cv.inRange(blur_img, t_mn,t_mx)
# binary_img = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
(_, contours, _) = cv.findContours(binary_img, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
num_cells = len(contours) - 1
print('Number of cells: {}'.format(num_cells))
output_dict['File'].append(file)
output_dict['Number of Cells'].append(num_cells)
# for (i, c) in enumerate(contours):
# c_img = cv.drawContours(img, [c], 0, (0,255,0), 3)
# draw(c_img,'contour-' + str(i) + '-' + file)
# print('\tSize of contour {}: {}'.format(i, len(c)))
contour_img = cv.drawContours(img, contours, -1, (0, 0, 255), 3)
draw(contour_img,file)
# print(output_dict)
df = pd.DataFrame(output_dict)
df.to_csv(os.path.join(output_path,'output.csv'),index=False)#index_label='ID')