-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
104 lines (88 loc) · 3.94 KB
/
Copy pathCamera.cpp
File metadata and controls
104 lines (88 loc) · 3.94 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
/*
* @author Ricardo Garcia Rosas
*
* @section DESCRIPTION
*
* Couple methods to organize the image processing implemented with OpenCV.
*
*/
#include "Camera.h"
//Processes an image to be used by the HoughCircles OpenCV function.
//@param source image.
//@param gray scale threshold.
//@param morph element size.
//@return processed image.
Mat ImageProcessing(Mat source, int thres, int morphSize)
{
//TODO: Go over http://stackoverflow.com/questions/9860667/writing-robust-color-and-size-invariant-circle-detection-with-opencv-based-on
Mat grayScale, thresholded;
//Convert frame to Gray
cvtColor(source, grayScale, CV_BGR2GRAY);
// Blur out noise
//GaussianBlur(grayScale, grayScale, Size(9, 9), 2, 2);
//Threshold
threshold(grayScale, thresholded, thres, 255, THRESH_BINARY);
//Morph opening (remove small objects from foreground)
erode(thresholded, thresholded, getStructuringElement(MORPH_ELLIPSE, Size(morphSize,morphSize)));
dilate(thresholded, thresholded, getStructuringElement(MORPH_ELLIPSE, Size(morphSize,morphSize)));
//Morph closing (fill small holes from foreground)
dilate(thresholded, thresholded, getStructuringElement(MORPH_ELLIPSE, Size(morphSize,morphSize)));
erode(thresholded, thresholded, getStructuringElement(MORPH_ELLIPSE, Size(morphSize,morphSize)));
return thresholded;
}
Mat ImageProcessing(Mat source, int lowH, int highH, int lowS, int highS, int lowV, int highV, int morphSize)
{
//TODO: Go over http://stackoverflow.com/questions/9860667/writing-robust-color-and-size-invariant-circle-detection-with-opencv-based-on
Mat imgHSV, thresholded;
// Blur out noise
//GaussianBlur(source, source, Size(9, 9), 2, 2);
//Convert from BGR to HSV
cvtColor(source, imgHSV, COLOR_BGR2HSV);
//Threshold the image
inRange(imgHSV, Scalar(lowH,lowS,lowV), Scalar(highH,highS,highV), thresholded);
//Morph opening (remove small objects from foreground)
erode(thresholded, thresholded, getStructuringElement(MORPH_ELLIPSE, Size(morphSize,morphSize)));
dilate(thresholded, thresholded, getStructuringElement(MORPH_ELLIPSE, Size(morphSize,morphSize)));
//Morph closing (fill small holes from foreground)
dilate(thresholded, thresholded, getStructuringElement(MORPH_ELLIPSE, Size(morphSize,morphSize)));
erode(thresholded, thresholded, getStructuringElement(MORPH_ELLIPSE, Size(morphSize,morphSize)));
for(int rows = 0; rows < thresholded.rows; rows++) {
for(int cols = 0; cols <= 18; cols++) {
Vec3b point = thresholded.at<Vec3b>(Point(cols, rows));
point.val[0] = 255;
point.val[1] = 255;
point.val[2] = 255;
thresholded.at<Vec3b>(Point(cols, rows)) = point;
}
}
for(int rows = 0; rows < thresholded.rows; rows++) {
for(int cols = 612; cols <= thresholded.cols; cols++) {
Vec3b point = thresholded.at<Vec3b>(Point(cols, rows));
point.val[0] = 255;
point.val[1] = 255;
point.val[2] = 255;
thresholded.at<Vec3b>(Point(cols, rows)) = point;
}
}
return thresholded;
}
//Gets the ball position from the HoughCircles output and sets them to program variables.
//@param x position variable address pointer.
//@param y position variable address pointer.
//@param raidus variable address pointer.
//@param output vector from HoughCircles.
//@param source image to draw UI circles on.
//@return image with circles on top.
Mat GetBallPosition(int* xPosBall, int* yPosBall, int* radius, vector<Vec3f> circles, Mat source)
{
if(circles.size() > 0) {
*xPosBall = cvRound(circles[0][0]);
*yPosBall = cvRound(circles[0][1]);
*radius = cvRound(circles[0][2]);
Point center(*xPosBall, *yPosBall);
//Print ball position on frame
circle( source, center, 3, Scalar(0,255,0), -1, 8, 0);
circle( source, center, *radius, Scalar(0,0,255), 3, 8, 0);
}
return source;
}