-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSubBunches.py
23 lines (20 loc) · 1.05 KB
/
getSubBunches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import cv2
import numpy as np
class Data:
pass
def getSubBunches(rgb,contours):
mask_len = len(contours)
subBunch = [Data()]*mask_len
for i in range(mask_len):
subBunch[i].position = cv2.boundingRect(contours[i])
subBunch[i].orientation = cv2.fitEllipse(contours[i])[2]
(B,G,R) = cv2.split(rgb)
subBunch[i].mask = np.zeros(B.shape)
cv2.drawContours(subBunch[i].mask,[contours[i]],-1,(255,255,255),-1)
subBunch[i].mask = subBunch[i].mask[int(subBunch[i].position[1]-1):int(subBunch[i].position[1]+subBunch[i].position[3]),int(subBunch[i].position[0]-1):int(subBunch[i].position[0]+subBunch[i].position[2])]
subBunch[i].rgb = rgb[int(subBunch[i].position[1]-1):int(subBunch[i].position[1]+subBunch[i].position[3]),int(subBunch[i].position[0]-1):int(subBunch[i].position[0]+subBunch[i].position[2])]
for a in range(subBunch[i].mask.shape[0]):
for b in range(subBunch[i].mask.shape[1]):
if subBunch[i].mask[a][b] == 255:
subBunch[i].mask[a][b] =1
return subBunch