-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisionSegment.py
More file actions
108 lines (83 loc) · 3.88 KB
/
visionSegment.py
File metadata and controls
108 lines (83 loc) · 3.88 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
import cv2
import numpy as np
import time
import glob
def colorMasks(inputImage):
hsv = cv2.cvtColor(inputImage, cv2.COLOR_BGR2HSV)
######RED RANGE#########
#Red Mask Lower
redLow = np.array([0,120,70])
redHigh = np.array([10,255,255])
redLowMask = cv2.inRange(hsv, redLow, redHigh)
#Red Mask Upper
redLow = np.array([170,120,70])
redHigh = np.array([180,255,255])
redHighMask = cv2.inRange(hsv, redLow, redHigh)
redMask = redLowMask + redHighMask
######GREEN RANGE#########
#Green Mask
# greenLow = np.array([30,65,65])
# greenHigh = np.array([80,255,255])
# greenMask = cv2.inRange(hsv, greenLow, greenHigh)
#
# ######YELLOW RANGE#########
# #Yellow Mask
# yellowLow = np.array([20,20,20])
# yellowHigh = np.array([30,255,255])
# yellowMaskBlended = cv2.inRange(hsv, yellowLow, yellowHigh)
#
# ######BLUE RANGE#########
# #Yellow Mask
# blueLow = np.array([90,50,50])
# blueHigh = np.array([110,255,255])
# blueMask = cv2.inRange(hsv, blueLow, blueHigh)
#Merged Mask
rgbyMask = greenMask + redMask + yellowMaskBlended + blueMask
blendedMask = cv2.bitwise_and(inputImage, inputImage, mask = rgbyMask)
return blendedMask, redMask, blueMask, greenMask
def contourBoundWrite(colorMask, currentScene):
imageToCrop = colorMask
grayscaledMask = cv2.cvtColor(imageToCrop, cv2.COLOR_BGR2GRAY)
(contours, _) = cv2.findContours(grayscaledMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
generatedFilename = int(round(time.time() * 1000))
for currentContour in contours:
xPosition, yPosition, width, height = cv2.boundingRect(currentContour)
if width > 150 and height > 25:
generatedFilename += 1
cv2.rectangle(currentScene, (xPosition, yPosition), (xPosition+width, yPosition+height), (0, 255, 0), 8);
# croppedImage = imageToCrop[yPosition: yPosition + height, xPosition: xPosition + width]
currentScene = cv2.resize(currentScene, (1280, 720))
cv2.imshow('Corners', currentScene)
cv2.waitKey(0)
cv2.imwrite(str(generatedFilename) + '.png', currentScene)
# yPosition = yPosition + height
# xPosition = xPosition + width
# cv2.rectangle(currentScene, (xPosition, yPosition), (xPosition, yPosition), (0, 255, 0), 10);
# print(yPosition)
def imageSegment():
imageFiles = glob.glob("images/*.jpg")
images = [cv2.imread(currentImage) for currentImage in imageFiles]
for currentImage in images:
currentScene = currentImage
segmentedMask, redMask, blueMask, greenMask = colorMasks(currentScene)
segmentedImage = segmentedMask
#RGBY Extraction
blue = segmentedMask[:,:,0]
green = segmentedMask[:,:,1]
red = segmentedMask[:,:,2]
blue = blue[np.where(blue > 170)]
green = green[np.where(green > 170)]
red = red[np.where(red > 170)]
yellowLow = np.uint8([0,100,100])
yellowHigh = np.uint8([100,255,255])
yellow = cv2.inRange(segmentedMask, yellowLow, yellowHigh)
greenMaskBlended = cv2.bitwise_and(currentScene, currentScene, mask = greenMask)
blueMaskBlended = cv2.bitwise_and(currentScene, currentScene, mask = blueMask)
redMaskBlended = cv2.bitwise_and(currentScene, currentScene, mask = redMask)
yellowMaskBlended = cv2.bitwise_and(currentScene, currentScene, mask = yellow)
contourBoundWrite(redMaskBlended, currentScene)
# contourBoundWrite(greenMaskBlended)
# contourBoundWrite(blueMaskBlended)
# contourBoundWrite(yellowMaskBlended)
if __name__=="__main__":
imageSegment()