Make segmentation more efficient.
This commit is contained in:
@@ -21,41 +21,51 @@ img_gray[img_gray[:,:] < 90] = 0
|
||||
# Threshold to binary.
|
||||
ret,img_thresh = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
|
||||
|
||||
# Doesn't take too long.
|
||||
# Following method is much faster -> 0.00143s
|
||||
# Still want to speed up further by lowering reliance on memory, which is quite heavy..
|
||||
k = np.sum(img_thresh) / 255
|
||||
|
||||
x_ind = np.indices(img_thresh.shape[1])
|
||||
coords = np.zeros(img_thresh.shape)
|
||||
# Taking indices for num of rows.
|
||||
x_ind = np.arange(0,img_thresh.shape[1])
|
||||
y_ind = np.arange(0,img_thresh.shape[0])
|
||||
coords = np.zeros((img_thresh.shape[0], img_thresh.shape[1], 2))
|
||||
coords_x = np.zeros((img_thresh.shape[0], img_thresh.shape[1]))
|
||||
coords_y = np.zeros((img_thresh.shape[0], img_thresh.shape[1]))
|
||||
coords_x[:,:] = x_ind
|
||||
|
||||
|
||||
# generate individual coordinates for x then transpose the matrix o
|
||||
# Even this is extremely quick as it goes through rows in the numpy array.
|
||||
for element in y_ind:
|
||||
coords_y[element,:] = element
|
||||
|
||||
# Now need to get the average x value and y value for centre of gravity
|
||||
xb = int(np.sum(coords_x[img_thresh == 255])/k)
|
||||
yb = int(np.sum(coords_y[img_thresh == 255])/k)
|
||||
|
||||
centre = (int(np.sum(coords_x[img_thresh == 255])/k), int(np.sum(coords_y[img_thresh == 255])/k))
|
||||
|
||||
#x,y,k,xb,yb = 0,0,0,0,0
|
||||
#
|
||||
# First sum x coordinates.
|
||||
#xb = int(img_ind[img_thresh == 255].sum(axis=1).sum()/k)
|
||||
#print(xb)
|
||||
# Then sum y coordinates
|
||||
#yb = int(img_ind[img_thresh == 255].sum(axis=0).sum()/k)
|
||||
#print(yb)
|
||||
## this is inherently slow...like very very slow...0.114s
|
||||
#for pix in img_thresh:
|
||||
# for j in pix:
|
||||
# if j == 255:
|
||||
# k += 1
|
||||
# xb += x
|
||||
# yb += y
|
||||
# x += 1
|
||||
# y += 1
|
||||
# x = 0
|
||||
#
|
||||
#centre = (int(xb/k), int(yb/k))
|
||||
|
||||
x,y,k,xb,yb = 0,0,0,0,0
|
||||
|
||||
# this is inherently slow...like very very slow...
|
||||
for pix in img_thresh:
|
||||
for j in pix:
|
||||
if j == 255:
|
||||
k += 1
|
||||
xb += x
|
||||
yb += y
|
||||
x += 1
|
||||
y += 1
|
||||
x = 0
|
||||
|
||||
centre = (int(xb/k), int(yb/k))
|
||||
|
||||
cv2.rectangle(img_thresh, centre, (centre[0] + 20, centre[1] + 20), (0,0,255), 3)
|
||||
cv2.circle(img_thresh, centre, 140, (0,0,0), 3)
|
||||
|
||||
# Now need to trace around the circle to figure out where the fingers are.
|
||||
# First get equation of the circle:
|
||||
# y = sart(r2 - (x-c)2 + c)
|
||||
|
||||
cv2.imshow("Binary-cot-out", img_thresh)
|
||||
cv2.waitKey(0)
|
||||
|
||||
Reference in New Issue
Block a user