-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vision.pde
117 lines (95 loc) · 3.37 KB
/
Vision.pde
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
PImage src, hueImage, colorFilteredImage, colorFilteredImageB;
ArrayList<Contour> contours = new ArrayList<Contour>(0);
//Range of Hue values for our filter
int hueAColorLow = 170;
int hueAColorHigh = 180;
int hueBColorLow = 100;
int hueBColorHigh = 110;
Rectangle rA = new Rectangle();
Rectangle rB = new Rectangle();
void vision() {
rectMode(CORNER);
// <2> Load the new frame of our movie in to OpenCV
opencv.loadImage(cam);
// Tell OpenCV to use color information
opencv.useColor();
opencv.flip(OpenCV.HORIZONTAL);
src = opencv.getSnapshot();
image(src, 0, 0); // <8> Display background images
opencv.useColor(HSB); // <3> Tell OpenCV to work in HSV color space.
opencv.setGray(opencv.getH().clone()); // <4> Copy the Hue channel of our image into the gray channel
hueImage = opencv.getSnapshot();
//opencv.loadImage(hueImage);
opencv.erode();
opencv.erode();
opencv.erode();
opencv.erode();
opencv.erode();
opencv.erode();
rA = processColor(hueAColorLow, hueAColorHigh); // <5> Filter the image based on the range of hue values
// image(colorFilteredImage, src.width, 0);
opencv.loadImage(hueImage);
//opencv.useColor(HSB);
opencv.erode();
opencv.erode();
opencv.erode();
opencv.erode();
opencv.erode();
opencv.erode();
rB = processColor(hueBColorLow, hueBColorHigh);
//image(colorFilteredImage, src.width, src.height);
// <11> Draw the bounding box of our object
noFill();
strokeWeight(2);
stroke(255, 0, 0);
if (modo == 0)
rect(rA.x, rA.y, rA.width, rA.height);
//ellipse(r.x + r.width/2, r.y + r.height/2, r.width, r.height);
noStroke();
fill(255, 0, 0);
ellipse(rA.x + rA.width/2, rA.y + rA.height/2, rCircle, rCircle); // <12> Draw a dot in the middle
// <11> Draw the bounding box of our object
noFill();
strokeWeight(2);
stroke(0, 0, 255);
if (modo == 0)
rect(rB.x, rB.y, rB.width, rB.height);
//ellipse(r.x + r.width/2, r.y + r.height/2, r.width, r.height);
noStroke();
fill(0, 0, 255);
ellipse(rB.x + rB.width/2, rB.y + rB.height/2, rCircle, rCircle); // <12> Draw a dot in the middle
}
Rectangle processColor(int hueLow, int hueHigh) {
Rectangle r = new Rectangle();
contours = new ArrayList<Contour>(0);
opencv.inRange(hueLow, hueHigh);
// <6> Get the processed image for reference.
colorFilteredImage = opencv.getSnapshot();
contours = opencv.findContours(true, true); // <7> Find contours in our range image.'true' sorts descending
// <9> Check to make sure we've found any contours
if (contours.size() > 0) {
// <9> Get the first contour, which will be the largest one
Contour biggestContour = contours.get(0);
// <10> Find the bounding box of the largest contour, and hence our object.
r = biggestContour.getBoundingBox();
}
return r;
}
void updateAColor() {
color c = get(int(objA.x), int(objA.x));
colorA = c;
println(" Color detectado: R: " + red(c) + " G: " + green(c) + " B: " + blue(c));
int hue = int(map(hue(c), 0, 255, 0, 180));
println(" Hue a detectar: " + hue);
hueAColorLow = hue - 10;
hueAColorHigh = hue + 10;
}
void updateBColor() {
color c = get(int(objB.x), int(objB.x));
colorB = c;
println(" Color detectado: R: " + red(c) + " G: " + green(c) + " B: " + blue(c));
int hue = int(map(hue(c), 0, 255, 0, 180));
println(" Hue a detectar: " + hue);
hueBColorLow = hue - 10;
hueBColorHigh = hue + 10;
}