forked from QTIM-Lab/qtim_ROP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.py
92 lines (64 loc) · 3.19 KB
/
methods.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
import cv2, numpy as np
from mask_retina import create_mask
from skimage.morphology import binary_erosion, selem
from retinaunet.lib.pre_processing import my_PreProc
def unet_preproc(img):
width, height, channels = img.shape
imgs_arr = np.empty((1, width, height, channels))
imgs_arr[0] = img
imgs_arr = np.transpose(imgs_arr, (0, 3, 1, 2))
pre_proc = my_PreProc(imgs_arr).astype(dtype=np.float16)
result = np.transpose(pre_proc[0], (1, 2, 0))[:, :, 0]
return result
# https://github.com/btgraham/SparseConvNet/blob/kaggle_Diabetic_Retinopathy_competition/Data/
# kaggleDiabeticRetinopathy/preprocessImages.py
def kaggle_BG(img, scale):
# Create a mask from which the approximate retinal center can be calculated
guide_mask = create_mask(img)
retina_center = tuple((np.mean(np.argwhere(guide_mask), axis=0)).astype(np.uint8))[::-1]
# Generate a circle of the approximate size, centered based on the guide mask
cf = .8 #0.95
circular_mask = np.zeros(img.shape)
cv2.circle(circular_mask, retina_center, int(scale * cf), (1, 1, 1), -1, 8, 0)
# Compute weight sum of image, blurred image and mask it
w_sum = cv2.addWeighted(img, 4, cv2.GaussianBlur(img, (0, 0), scale / 30), -4, 128) * circular_mask + 128 * (1 - circular_mask)
return w_sum.astype(np.uint8)
# https://github.com/btgraham/SparseConvNet/blob/kaggle_Diabetic_Retinopathy_competition/Data/
# kaggleDiabeticRetinopathy/preprocessImages.py
def scale_radius(img, scale):
x = img[img.shape[0] / 2, :, :].sum(1)
r = (x > x.mean() / 10).sum() / 2
s = scale * 1.0 / r
return cv2.resize(img, (0, 0), fx=s, fy=s)
def normalize_channels(img):
for colorband in xrange(img.shape[2]):
img[:, :, colorband] = image_histogram_equalization(img[:, :, colorband])
return img
# http://www.janeriksolem.net/2009/06/histogram-equalization-with-python-and.html
def image_histogram_equalization(image, number_bins=256):
# get image histogram
image_histogram, bins = np.histogram(image.flatten(), number_bins, normed=True)
cdf = image_histogram.cumsum() # cumulative distribution function
cdf = 255 * cdf / cdf[-1] # normalize
# use linear interpolation of cdf to find new pixel values
image_equalized = np.interp(image.flatten(), bins[:-1], cdf)
return image_equalized.reshape(image.shape)
def binary_morph(img, thresh=50, min_size=None, mask_only=True):
if min_size is None: # default to 10% of largest image dimension
min_size = float(max(img.shape)) * .1
if len(img.shape) == 3: # flatten if RGB image
img = np.mean(img, 2).astype(np.uint8)
# Apply binary threshold and erode
ret, thresh_im = cv2.threshold(img, thresh, 255, cv2.THRESH_BINARY)
# Connected component labelling
n, labels, stats, centroids = cv2.connectedComponentsWithStats(thresh_im)
mask = np.zeros_like(labels)
# Loop through areas in order of size
areas = [s[4] for s in stats]
sorted_idx = np.argsort(areas)
for lidx, cc in zip(sorted_idx, [areas[s] for s in sorted_idx][:-1]):
if cc > min_size:
mask[labels == lidx] = 1
if mask_only:
return mask * 255
return np.dstack([img * mask] * 3).astype(np.uint8)