forked from derbedhruv/openDR
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimage_processing.py
122 lines (82 loc) · 3.38 KB
/
image_processing.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
import cv2
import numpy as np
import imutils
# constants for extract_circles()
center=(1386,948)
radius=804
# constants for erode_thresh()
threshold_value=65
kernel_size=14
erosion_iterations=5
#function to extract circular region from the given image
def extract_circles(image):
#make a null matrix of same dimension as image
mask=np.zeros(image.shape[:], dtype=np.uint8)
#draw a white circle in the mask
cv2.circle(mask,center,radius,(255,255,255),thickness=-1)
# the mask will be a bgr image
# has to be converted to grayscale for threshholding
mask=cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)
# threshholding the grayscale image to get a binary image, with max_value 1
ret,mask=cv2.threshold(mask,10,1,cv2.THRESH_BINARY)
# the mask is multiplied with all 3 channels of the original image
b=np.multiply(image[:,:,0],mask)
g=np.multiply(image[:,:,1],mask)
r=np.multiply(image[:,:,2],mask)
# img_final combines these 3 seperated channels to get the final Fundus image
img_final=np.zeros(image.shape[:])
img_final=np.uint8(img_final)
img_final[:,:,0]=b
img_final[:,:,1]=g
img_final[:,:,2]=r
return img_final
# function to erode and threhold a given image
def erode_thresh(image):
image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#kernel for erosion
kernel=np.ones((kernel_size,kernel_size),np.uint8)
#threshold the image
ret,threshed=cv2.threshold(image,threshold_value,255,cv2.THRESH_BINARY)
#erode the thresholded image to smoothen the edges
threshed=cv2.erode(threshed,kernel,iterations = erosion_iterations)
#blur the image to smoothen it
cv2.GaussianBlur(threshed,(21,21),0)
return threshed
# function to fit an ellipse, given an image and its thresholded form
def ellipse_fit(image,cont_img):
# to detect contours in the binary image
contours, hierarchy = cv2.findContours(cont_img, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# make a mask of zeros
mask=np.zeros(image.shape[:], dtype=np.uint8)
# find the biggest contour and fit an ellipse
c=max(contours, key=cv2.contourArea)
ellipse = cv2.fitEllipse(c)
# draw an ellipse of the same size on the mask
cv2.ellipse(mask, ellipse, (255,255,255), -1)
# the mask will be a bgr image
# has to be converted to grayscale for threshholding
mask=cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)
# threshholding the grayscale image to get a binary image, with max_value 1
ret,mask=cv2.threshold(mask,10,1,cv2.THRESH_BINARY)
# the mask is multiplied with all 3 channels of the original image
b=np.multiply(image[:,:,0],mask)
g=np.multiply(image[:,:,1],mask)
r=np.multiply(image[:,:,2],mask)
# img_final combines these 3 seperated channels to get the final Fundus image
img_final=np.zeros(image.shape[:])
img_final=np.uint8(img_final)
img_final[:,:,0]=b
img_final[:,:,1]=g
img_final[:,:,2]=r
return img_final
#To test the above functions
test_img=cv2.imread('owl1.jpg')
circle=extract_circles(test_img)
cv2.imshow('extracted circle',imutils.resize(circle,width=432,height=324))
threshed_image=erode_thresh(circle)
cv2.imshow('eroded and threshed',imutils.resize(threshed_image,width=432,height=324))
final_image=ellipse_fit(circle,threshed_image)
cv2.imshow('window',imutils.resize(final_image,width=432,height=324))
cv2.waitKey(0)
cv2.destroyAllWindows()