-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcode.py
76 lines (60 loc) · 1.94 KB
/
code.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
import cv2
import numpy as np
from matplotlib import pyplot as plt
def get_classificaton(ratio):
ratio =round(ratio,1)
toret=""
if(ratio>=3):
toret="Slender"
elif(ratio>=2.1 and ratio<3):
toret="Medium"
elif(ratio>=1.1 and ratio<2.1):
toret="Bold"
elif(ratio<=1):
toret="Round"
toret="("+toret+")"
return toret
#rnjn
print "Starting"
img = cv2.imread('rice.png',0)#load in greyscale mode
#convert into binary
ret,binary = cv2.threshold(img,160,255,cv2.THRESH_BINARY)# 160 - threshold, 255 - value to assign, THRESH_BINARY_INV - Inverse binary
#averaging filter
kernel = np.ones((5,5),np.float32)/9
dst = cv2.filter2D(binary,-1,kernel)# -1 : depth of the destination image
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
#erosion
erosion = cv2.erode(dst,kernel2,iterations = 1)
#dilation
dilation = cv2.dilate(erosion,kernel2,iterations = 1)
#edge detection
edges = cv2.Canny(dilation,100,200)
### Size detection
_,contours,hierarchy = cv2.findContours(erosion, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print "No. of rice grains=",len(contours)
total_ar=0
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
aspect_ratio = float(w)/h
if(aspect_ratio<1):
aspect_ratio=1/aspect_ratio
print round(aspect_ratio,2),get_classificaton(aspect_ratio)
total_ar+=aspect_ratio
avg_ar=total_ar/len(contours)
print "Average Aspect Ratio=",round(avg_ar,2),get_classificaton(avg_ar)
#plot the images
imgs_row=2
imgs_col=3
plt.subplot(imgs_row,imgs_col,1),plt.imshow(img,'gray')
plt.title("Original image")
plt.subplot(imgs_row,imgs_col,2),plt.imshow(binary,'gray')
plt.title("Binary image")
plt.subplot(imgs_row,imgs_col,3),plt.imshow(dst,'gray')
plt.title("Filtered image")
plt.subplot(imgs_row,imgs_col,4),plt.imshow(erosion,'gray')
plt.title("Eroded image")
plt.subplot(imgs_row,imgs_col,5),plt.imshow(dilation,'gray')
plt.title("Dialated image")
plt.subplot(imgs_row,imgs_col,6),plt.imshow(edges,'gray')
plt.title("Edge detect")
plt.show()