-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.py
More file actions
160 lines (94 loc) · 3.74 KB
/
Copy pathutils.py
File metadata and controls
160 lines (94 loc) · 3.74 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
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
from sklearn.cluster import KMeans
import random as rng
import cv2
import imutils
import argparse
from skimage.io import imread
import numpy as np
import matplotlib.pyplot as plt
def preprocess(img):
img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
img = cv2.GaussianBlur(img, (9, 9), 0)
img = img/255
return img
def plotImage(img):
plt.imshow(img)
#plt.title('Clustered Image')
plt.show()
def cropOrig(bRect, oimg):
# x (Horizontal), y (Vertical Downwards) are start coordinates
# img.shape[0] = height of image
# img.shape[1] = width of image
x,y,w,h = bRect
print(x,y,w,h)
pcropedImg = oimg[y:y+h,x:x+w]
x1, y1, w1, h1 = 0, 0, pcropedImg.shape[1], pcropedImg.shape[0]
y2 = int(h1/10)
x2 = int(w1/10)
crop1 = pcropedImg[y1+y2:h1-y2,x1+x2:w1-x2]
#cv2_imshow(crop1)
ix, iy, iw, ih = x+x2, y+y2, crop1.shape[1], crop1.shape[0]
croppedImg = oimg[iy:iy+ih,ix:ix+iw]
return croppedImg, pcropedImg
def overlayImage(croppedImg, pcropedImg):
x1, y1, w1, h1 = 0, 0, pcropedImg.shape[1], pcropedImg.shape[0]
y2 = int(h1/10)
x2 = int(w1/10)
new_image = np.zeros((pcropedImg.shape[0], pcropedImg.shape[1], 3), np.uint8)
new_image[:, 0:pcropedImg.shape[1]] = (255, 0, 0) # (B, G, R)
new_image[ y1+y2:y1+y2+croppedImg.shape[0], x1+x2:x1+x2+croppedImg.shape[1]] = croppedImg
return new_image
def kMeans_cluster(img):
# For clustering the image using k-means, we first need to convert it into a 2-dimensional array
# (H*W, N) N is channel = 3
image_2D = img.reshape(img.shape[0]*img.shape[1], img.shape[2])
# tweak the cluster size and see what happens to the Output
kmeans = KMeans(n_clusters=2, random_state=0).fit(image_2D)
clustOut = kmeans.cluster_centers_[kmeans.labels_]
# Reshape back the image from 2D to 3D image
clustered_3D = clustOut.reshape(img.shape[0], img.shape[1], img.shape[2])
clusteredImg = np.uint8(clustered_3D*255)
return clusteredImg
def edgeDetection(clusteredImage):
#gray = cv2.cvtColor(hsvImage, cv2.COLOR_BGR2GRAY)
edged1 = cv2.Canny(clusteredImage, 0, 255)
edged = cv2.dilate(edged1, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)
return edged
def getBoundingBox(img):
contours, _ = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#print(len(contours))
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
contours_poly = [None]*len(contours)
boundRect = [None]*len(contours)
for i, c in enumerate(contours):
contours_poly[i] = cv2.approxPolyDP(c, 3, True)
boundRect[i] = cv2.boundingRect(contours_poly[i])
return boundRect, contours, contours_poly, img
def drawCnt(bRect, contours, cntPoly, img):
drawing = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
paperbb = bRect
for i in range(len(contours)):
color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
cv2.drawContours(drawing, cntPoly, i, color)
#cv2.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), \
#(int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2)
cv2.rectangle(drawing, (int(paperbb[0]), int(paperbb[1])), \
(int(paperbb[0]+paperbb[2]), int(paperbb[1]+paperbb[3])), color, 2)
return drawing
def calcFeetSize(pcropedImg, fboundRect):
x1, y1, w1, h1 = 0, 0, pcropedImg.shape[1], pcropedImg.shape[0]
y2 = int(h1/10)
x2 = int(w1/10)
fh = y2 + fboundRect[2][3]
fw = x2 + fboundRect[2][2]
ph = pcropedImg.shape[0]
pw = pcropedImg.shape[1]
opw = 210
oph = 297
ofs = 0.0
if fw>fh:
ofs = (opw/pw)*fw
else :
ofs = (oph/ph)*fh
return ofs