-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.py
166 lines (109 loc) · 4.8 KB
/
functions.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import cv2
import numpy as np
import math
# Segment the part containing Green Leaf from Image
def segmentation(img):
GRAY_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(GRAY_img, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
img_contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2]
img_contours = sorted(img_contours, key=cv2.contourArea)
for i in img_contours:
if cv2.contourArea(i) > 100:
break
mask = np.zeros(img.shape[:2], np.uint8)
cv2.drawContours(mask, [i], -1, 255, -1)
new_img = cv2.bitwise_and(img, img, mask=mask)
return new_img
# Another Segmentation
def segmentation1(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
pixel_number = gray.shape[0] * gray.shape[1]
mean_weight = 1.0 / pixel_number
his, bins = np.histogram(gray, np.array(range(0, 256)))
final_thresh = -1
final_value = -1
for t in bins[1:-1]:
wb = np.sum(his[:t]) * mean_weight
wf = np.sum(his[t:]) * mean_weight
mub = np.mean(his[:t])
muf = np.mean(his[t:])
value = wb * wf * (mub - muf) ** 2
if value > final_value:
final_thresh = t
final_value = value
final_img = gray.copy()
final_img[gray > final_thresh] = 255
final_img[gray <= final_thresh] = 0
final_img = cv2.equalizeHist(final_img)
_, thresh = cv2.threshold(final_img, 0, final_thresh, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
img_contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2]
img_contours = sorted(img_contours, key=cv2.contourArea)
mask = np.zeros(img.shape[:2], np.uint8)
for i in img_contours:
mask = np.zeros(img.shape[:2], np.uint8)
cv2.drawContours(mask, [i], -1, 255, -1)
new_img = cv2.bitwise_and(img, img, mask=mask)
return new_img
# Compare both Segmentaion Images
def compareTwo(k1, k2):
gray1 = cv2.cvtColor(k1,cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray1,0,255,cv2.THRESH_BINARY)
count1 = cv2.countNonZero(thresh1)
gray2 = cv2.cvtColor(k2,cv2.COLOR_BGR2GRAY)
ret, thresh2 = cv2.threshold(gray2,0,255,cv2.THRESH_BINARY)
count2 = cv2.countNonZero(thresh2)
if(count1>count2):
k=k1
else:
k=k2
return k
# Apply contrasting to the image to make colors more clear
def contrasting(new_img):
alpha = 1.2
beta = 1.5
adjusted = cv2.convertScaleAbs(new_img,new_img, alpha=alpha, beta=beta)
return new_img
# Convert RGB image to HSI image to find and detect perfect green color Range
def RGB_TO_HSI(img):
with np.errstate(divide='ignore', invalid='ignore'):
#Load image with 32 bit floats as variable type
bgr = np.float32(img)/255
#Separate color channels
blue = bgr[:,:,0]
green = bgr[:,:,1]
red = bgr[:,:,2]
#Calculate Intensity
def calc_intensity(red, blue, green):
return np.divide(blue + green + red, 3)
#Calculate Saturation
def calc_saturation(red, blue, green):
minimum = np.minimum(np.minimum(red, green), blue)
saturation = 1 - (3 / (red + green + blue + 0.001) * minimum)
return saturation
#Calculate Hue
def calc_hue(red, blue, green):
hue = np.copy(red)
for i in range(0, blue.shape[0]):
for j in range(0, blue.shape[1]):
hue[i][j] = 0.5 * ((red[i][j] - green[i][j]) + (red[i][j] - blue[i][j])) / \
math.sqrt((red[i][j] - green[i][j])**2 +
((red[i][j] - blue[i][j]) * (green[i][j] - blue[i][j])))
hue[i][j] = math.acos(hue[i][j])
if blue[i][j] <= green[i][j]:
hue[i][j] = hue[i][j]
else:
hue[i][j] = ((360 * math.pi) / 180.0) - hue[i][j]
return hue
#Merge channels into picture and return image
hsi = cv2.merge((calc_hue(red, blue, green), calc_saturation(red, blue, green), calc_intensity(red, blue, green)))
return hsi
# Remove all grreen Pixels from the image to get only damaged part of the leaf
def damaged_only(img):
# img = cv2.imread(contrasted_img)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
upper_gree=np.array([80,255,255])
lower_gree=np.array([15,0,0])
mask=cv2.inRange(hsv,lower_gree,upper_gree)
final=cv2.bitwise_and(img,img,mask=mask)
defected = cv2.subtract(img , final)
return defected