-
Notifications
You must be signed in to change notification settings - Fork 23
/
utils.py
executable file
·63 lines (48 loc) · 2.08 KB
/
utils.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
import numpy as np
import scipy
import scipy.io as sio
from keras.preprocessing import image
from skimage.measure import label, regionprops
from skimage.transform import rotate, resize
import cv2
def pro_process(temp_img, input_size):
img = np.asarray(temp_img).astype('float32')
img = scipy.misc.imresize(img, (input_size, input_size, 3))
return img
def BW_img(input,thresholding):
if input.max() > thresholding:
binary = input > thresholding
else:
binary = input > input.max()/2.0
label_image = label(binary)
regions = regionprops(label_image)
area_list = []
for region in regions:
area_list.append(region.area)
if area_list:
idx_max = np.argmax(area_list)
binary[label_image != idx_max+1] = 0
return scipy.ndimage.binary_fill_holes(np.asarray(binary).astype(int))
def Deep_Screening(target_model, tmp_img, input_size):
temp_img = scipy.misc.imresize(tmp_img, (input_size, input_size, 3))
temp_img = np.reshape(temp_img, (1,) + temp_img.shape)
Pre_result = target_model.predict(temp_img)
return Pre_result
def Disc_Crop(org_img, DiscROI_size, C_x, C_y):
disc_region = np.zeros((DiscROI_size, DiscROI_size, 3), dtype= org_img.dtype)
crop_coord = [int(C_x-DiscROI_size/2), int(C_x+DiscROI_size/2),int(C_y-DiscROI_size/2),int(C_y+DiscROI_size/2)]
err_coord = [0,DiscROI_size,0,DiscROI_size]
if crop_coord[0] < 0:
err_coord[0] = abs(crop_coord[0]) +1
crop_coord[0] = 0
if crop_coord[2] < 0:
err_coord[2] = abs(crop_coord[2]) +1
crop_coord[2] = 0
if crop_coord[1] > org_img.shape[0]:
err_coord[1] = err_coord[1] - (crop_coord[1] - org_img.shape[0]) -1
crop_coord[1] = org_img.shape[0]
if crop_coord[3] > org_img.shape[1]:
err_coord[3] = err_coord[3] - (crop_coord[3] - org_img.shape[1]) -1
crop_coord[3] = org_img.shape[1]
disc_region[err_coord[0]:err_coord[1], err_coord[2]:err_coord[3], ] = org_img[crop_coord[0]:crop_coord[1], crop_coord[2]:crop_coord[3], ]
return disc_region