-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_rice.py
More file actions
71 lines (70 loc) · 2.68 KB
/
Copy pathip_rice.py
File metadata and controls
71 lines (70 loc) · 2.68 KB
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
import cv2
import numpy as np
from os import listdir
from os.path import isfile, join
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
mypath=r"C:\Users\subha\Desktop\garbage\work\project\pic"
images = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
image = np.empty(len(images), dtype=object)
for n in range(0, len(images)):
images[n] = cv2.imread( join(mypath,images[n]),0 )
ret,binary = cv2.threshold(images[n],160,255,cv2.THRESH_BINARY)#averaging filter
kernel = np.ones ((5,5),np.float32)/25
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_LIST, cv2.CHAIN_APPROX_NONE)
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(images[n],'gray')
plt.title("Original image")
plt.xlabel("Length")
plt.ylabel("Breadth")
plt.subplot(imgs_row,imgs_col,2),plt.imshow(binary,'gray')
plt.title("Binary image")
plt.xlabel("Length")
plt.ylabel("Breadth")
plt.subplot(imgs_row,imgs_col,3),plt.imshow(dst,'gray')
plt.title("Filtered image")
plt.xlabel("Length")
plt.ylabel("Breadth")
plt.subplot(imgs_row,imgs_col,4),plt.imshow(erosion,'gray')
plt.title("Eroded image")
plt.xlabel("Length")
plt.ylabel("Breadth")
plt.subplot(imgs_row,imgs_col,5),plt.imshow(dilation,'gray')
plt.title("Dialated image")
plt.xlabel("Length")
plt.ylabel("Breadth")
plt.subplot(imgs_row,imgs_col,6),plt.imshow(edges,'gray')
plt.title("Edge detect")
plt.xlabel("Length")
plt.ylabel("Breadth")
plt.show()