-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVision.cpp
More file actions
171 lines (130 loc) · 4.73 KB
/
Vision.cpp
File metadata and controls
171 lines (130 loc) · 4.73 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// Created by Tjipkevdh on 15-6-2015.
//
#include "Vision.h"
void Vision::recognizeBlueBalloon()
{
//threshold...
// Dont change this values pl0x :3
//inRange(image, Scalar(0,90,90), Scalar(30,260,260), image);
inRange(image, Scalar(90, 150,90), Scalar(130,260,260),image);
}
void Vision::recognizeRedBalloon() {
//image 1
//inRange(image, Scalar(120,100,100), Scalar(140,260,260), image);
inRange(image, Scalar(140,0,0), Scalar(160,260,260),image2);
inRange(image,Scalar(160,50,100),Scalar(180,260,260), image);
}
void Vision::fillEdgeImage(Mat edgeIn, cv::Mat &filledEdgesOut)
{
Mat edgesNeg = edgeIn.clone();
floodFill(edgesNeg, Point(0,0), CV_RGB(255,255,255));
bitwise_not(edgesNeg, edgesNeg);
filledEdgesOut = (edgesNeg | edgeIn);
return;
}
void Vision::findBiggestContour()
{
int largest_area=0;
int largest_contour_index=0;
Rect bounding_rect;
for( int i = 0; i < contours.size(); i++ ) // iterate through each contour.
{
double a = contourArea( contours[i],false); // Find the area of contour
if(a>largest_area){
largest_area=a;
largest_contour_index=i; //Store the index of largest contour
bounding_rect=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
}
}
Scalar color(255,255,255);
drawContours( image, contours,largest_contour_index, color, CV_FILLED, 8, hierarchy );// Draw the largest contour using previously stored index.
Point center(bounding_rect.x + bounding_rect.size().width/2, bounding_rect.y + bounding_rect.size().height/2);
rectangle(image, bounding_rect, Scalar(0,255,0),1, 8,0);
*x = center.x;
*y = center.y;
//std::cout << "center of countour is " << center.x << "+ " << center.y << std::endl;
}
void Vision::liveStream()
{
long duration = 100;
if(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count() - ms.count() >= duration)
{
ms = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
imwrite("stream" + std::to_string(streamcount) + ".jpg", image);
std::cout << "streamcount: " << streamcount<< std::endl;
streamcount++;
}
}
void Vision::startVision()// todo: WaitKey(0); achter imshow maakt het werkend :p
{
cout << "vision gestart" << endl;
if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( erosion_type,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
//VideoCapture cap(0);
VideoCapture videocapture(0);
//videocapture.open(0);
if(!videocapture.isOpened()) // check if we succeeded
{
std::cout << "camera not opened :(" << std::endl;
return;
}
else
std::cout << "cam opened: " << std::endl;
while(1)
{// todo: cout uitgecomment hier
// cout << "zit nu in while loopyswoopy" << std::endl;
// Getting the image from the camm..
videocapture >> image;
//waitKey(0);
//cout << "hier zou een image geshowed moeten worden" << endl;
//imshow("testbestand", image);
//getchar();
/*liveStream();
if(streamcount == 10)
{
streamcount = 1;
}*/
Size size(image.cols/2,image.rows/2);
//std::cout << size << endl;
resize(image,image,size);
cvtColor(image,image,CV_BGR2HSV);
if(*balloon==1)
{
recognizeBlueBalloon();
}
else
{
recognizeRedBalloon();
}
fillEdgeImage(image,image);
/// Apply the erosion operation
erode( image, image, element);
dilate(image,image,element);
//namedWindow("Display Image2", WINDOW_AUTOSIZE );
//imshow("Display Image2", image2);
Canny(image,image,100,200,3);
findContours(image, contours,hierarchy,CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,0));
findBiggestContour();
//cout << "x: " << x << ", y: " << y << "." << endl;
// if(*x > 0) {
// std::cout << "ziet ballon :D, x: " << *x << ", y: " << *y << std::endl;
// }
// else if(*y > 0)
// {
// std::cout << "ziet ballon :D, x: " << *x << ", y: " << *y << std::endl;;
// }
// else
// {
// std::cout << "kijkt rond" << std::endl;
// }
//namedWindow("Display Image", WINDOW_AUTOSIZE );
//imshow("Display Image", image);
}
//waitKey(0);
return;
}