forked from QuinnThomas/grip-vision-2017
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrip.py
148 lines (121 loc) · 5.67 KB
/
grip.py
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
import cv2
class GripPipeline:
"""
An OpenCV pipeline generated by GRIP.
"""
def __init__(self):
"""initializes all values to presets or None if need to be set
"""
self.__resize_image_width = 640.0
self.__resize_image_height = 480.0
self.__resize_image_interpolation = cv2.INTER_CUBIC
self.resize_image_output = None
self.__hsv_threshold_input = self.resize_image_output
self.__hsv_threshold_hue = [64.74819835141408, 102.43323973242443]
self.__hsv_threshold_saturation = [73.38128949669625, 226.2798701211454]
self.__hsv_threshold_value = [189.41546412680646, 255.0]
self.hsv_threshold_output = None
self.__cv_erode_src = self.hsv_threshold_output
self.__cv_erode_kernel = None
self.__cv_erode_anchor = (-1, -1)
self.__cv_erode_iterations = 1.0
self.__cv_erode_bordertype = cv2.BORDER_CONSTANT
self.__cv_erode_bordervalue = (-1)
self.cv_erode_output = None
self.__find_contours_input = self.cv_erode_output
self.__find_contours_external_only = False
self.find_contours_output = None
self.__convex_hulls_contours = self.find_contours_output
self.convex_hulls_output = None
def process(self, source0):
"""
Runs the pipeline and sets all outputs to new values.
"""
# Step Resize_Image0:
self.__resize_image_input = source0
(self.resize_image_output) = self.__resize_image(self.__resize_image_input, self.__resize_image_width,
self.__resize_image_height, self.__resize_image_interpolation)
# Step HSV_Threshold0:
self.__hsv_threshold_input = self.resize_image_output
(self.hsv_threshold_output) = self.__hsv_threshold(self.__hsv_threshold_input, self.__hsv_threshold_hue,
self.__hsv_threshold_saturation, self.__hsv_threshold_value)
# Step CV_erode0:
self.__cv_erode_src = self.hsv_threshold_output
(self.cv_erode_output) = self.__cv_erode(self.__cv_erode_src, self.__cv_erode_kernel, self.__cv_erode_anchor,
self.__cv_erode_iterations, self.__cv_erode_bordertype,
self.__cv_erode_bordervalue)
# Step Find_Contours0:
self.__find_contours_input = self.cv_erode_output
(self.find_contours_output) = self.__find_contours(self.__find_contours_input,
self.__find_contours_external_only)
# Step Convex_Hulls0:
self.__convex_hulls_contours = self.find_contours_output
(self.convex_hulls_output) = self.__convex_hulls(self.__convex_hulls_contours)
return self.convex_hulls_output
@staticmethod
def __resize_image(input, width, height, interpolation):
"""Scales and image to an exact size.
Args:
input: A numpy.ndarray.
Width: The desired width in pixels.
Height: The desired height in pixels.
interpolation: Opencv enum for the type fo interpolation.
Returns:
A numpy.ndarray of the new size.
"""
return cv2.resize(input, ((int)(width), (int)(height)), 0, 0, interpolation)
@staticmethod
def __hsv_threshold(input, hue, sat, val):
"""Segment an image based on hue, saturation, and value ranges.
Args:
input: A BGR numpy.ndarray.
hue: A list of two numbers the are the min and max hue.
sat: A list of two numbers the are the min and max saturation.
lum: A list of two numbers the are the min and max value.
Returns:
A black and white numpy.ndarray.
"""
out = cv2.cvtColor(input, cv2.COLOR_BGR2HSV)
return cv2.inRange(out, (hue[0], sat[0], val[0]), (hue[1], sat[1], val[1]))
@staticmethod
def __cv_erode(src, kernel, anchor, iterations, border_type, border_value):
"""Expands area of lower value in an image.
Args:
src: A numpy.ndarray.
kernel: The kernel for erosion. A numpy.ndarray.
iterations: the number of times to erode.
border_type: Opencv enum that represents a border type.
border_value: value to be used for a constant border.
Returns:
A numpy.ndarray after erosion.
"""
return cv2.erode(src, kernel, anchor, iterations=(int)(iterations + 0.5),
borderType=border_type, borderValue=border_value)
@staticmethod
def __find_contours(input, external_only):
"""Sets the values of pixels in a binary image to their distance to the nearest black pixel.
Args:
input: A numpy.ndarray.
external_only: A boolean. If true only external contours are found.
Return:
A list of numpy.ndarray where each one represents a contour.
"""
if (external_only):
mode = cv2.RETR_EXTERNAL
else:
mode = cv2.RETR_LIST
method = cv2.CHAIN_APPROX_SIMPLE
im2, contours, hierarchy = cv2.findContours(input, mode=mode, method=method)
return contours
@staticmethod
def __convex_hulls(input_contours):
"""Computes the convex hulls of contours.
Args:
input_contours: A list of numpy.ndarray that each represent a contour.
Returns:
A list of numpy.ndarray that each represent a contour.
"""
output = []
for contour in input_contours:
output.append(cv2.convexHull(contour))
return output