-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorsensing.cpp
More file actions
141 lines (115 loc) · 6.36 KB
/
colorsensing.cpp
File metadata and controls
141 lines (115 loc) · 6.36 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
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
cv::VideoCapture capWebcam(0); // declare a VideoCapture object and associate to webcam, 0 => use 1st webcam
if (capWebcam.isOpened() == false)
std::cout << "error: capWebcam not accessed successfully\n\n"; // if not, print error message to std out
return(0); // and exit program
}
cv::Mat imgOriginal; // input image
cv::Mat imgHSV;
cv::Mat imgThreshLow;
cv::Mat imgThreshHigh;
cv::Mat imgThresh;
cv::Mat imgThreshLow1;
cv::Mat imgThreshLow2;
std::vector<cv::Vec3f> v3fCircles;
char charCheckForEscKey = 0;
while (charCheckForEscKey != 27 && capWebcam.isOpened())
{ // until the Esc key is pressed or webcam connection is lost
bool blnFrameReadSuccessfully = capWebcam.read(imgOriginal); // get next frame
if (!blnFrameReadSuccessfully || imgOriginal.empty())
{ // if frame not read successfully
std::cout << "error: frame not read from webcam\n"; // print error message to std out
break; // and jump out of while loop
}
cv::cvtColor(imgOriginal, imgHSV, CV_BGR2HSV);
cv::inRange(imgHSV, cv::Scalar(0, 142, 90), cv::Scalar(95, 255, 204), imgThreshLow);//red
cv::inRange(imgHSV, cv::Scalar(165, 155, 155), cv::Scalar(179, 255, 255), imgThreshHigh);
cv::add(imgThreshLow, imgThreshHigh, imgThresh);
cv::GaussianBlur(imgThresh, imgThresh, cv::Size(3, 3), 0);
cv::Mat structuringElement = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::dilate(imgThresh, imgThresh, structuringElement);
cv::erode(imgThresh, imgThresh, structuringElement);
// fill circles vector with all circles in processed image
cv::HoughCircles(imgThresh, // input image
v3fCircles, // function output (must be a standard template library vector
CV_HOUGH_GRADIENT, // two-pass algorithm for detecting circles, this is the only choice available
2, // size of image / this value = "accumulator resolution", i.e. accum res = size of image / 2
imgThresh.rows / 2, // min distance in pixels between the centers of the detected circles
100, // high threshold of Canny edge detector (called by cvHoughCircles)
40, // low threshold of Canny edge detector (set at 1/2 previous value)
9, // min circle radius (any circles with smaller radius will not be returned)
50); // max circle radius (any circles with larger radius will not be returned)
for (int i = 0; i < v3fCircles.size(); i++)
{ // for each circle . . .
// show ball position x, y, and radius to command line
std::cout << "red ball position x = " << v3fCircles[i][0] // x position of center point of circle
<< ", y = " << v3fCircles[i][1] // y position of center point of circle
<< ", radius = " << v3fCircles[i][2] << "\n"; // radius of circle
// draw small green circle at center of detected object
cv::circle(imgOriginal, // draw on original image
cv::Point((int)v3fCircles[i][0], (int)v3fCircles[i][1]), // center point of circle
3, // radius of circle in pixels
cv::Scalar(0, 255, 0), // draw pure green (remember, its BGR, not RGB)
CV_FILLED); // thickness, fill in the circle
// draw red circle around the detected object
cv::circle(imgOriginal, // draw on original image
cv::Point((int)v3fCircles[i][0], (int)v3fCircles[i][1]), // center point of circle
(int)v3fCircles[i][2], // radius of circle in pixels
cv::Scalar(0, 0, 255), // draw pure red (remember, its BGR, not RGB)
3); // thickness of circle in pixels
}
//////////////////////////// end for Red region here//////////////////////////////////
cv::Mat imgThresh1;
std::vector<cv::Vec3f> v3fCircles1; // 3 element vector of floats, this will be the pass by reference output of HoughCircles()
char charCheckForEscKey = 0;
cv::inRange(imgHSV, cv::Scalar(72, 90, 47), cv::Scalar(147, 255, 250), imgThresh1);
cv::GaussianBlur(imgThresh1, imgThresh1, cv::Size(3, 3), 0);
cv::Mat structuringElement1 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::dilate(imgThresh1, imgThresh1, structuringElement1);
cv::erode(imgThresh1, imgThresh1, structuringElement1);
cv::HoughCircles(imgThresh1, // input image
v3fCircles1, // function output (must be a standard template library vector
CV_HOUGH_GRADIENT, // two-pass algorithm for detecting circles, this is the only choice available
2, // size of image / this value = "accumulator resolution", i.e. accum res = size of image / 2
imgThresh1.rows / 2, // min distance in pixels between the centers of the detected circles
100, // high threshold of Canny edge detector (called by cvHoughCircles)
40, // low threshold of Canny edge detector (set at 1/2 previous value)
5, // min circle radius (any circles with smaller radius will not be returned)
100); // max circle radius (any circles with larger radius will not be returned)
for (int i = 0; i < v3fCircles1.size(); i++)
{ // for each circle . . .
// // show ball position x, y, and radius to command line
std::cout << "blue ball position x = " << v3fCircles1[i][0] // x position of center point of circle
<< ", y = " << v3fCircles1[i][1] // y position of center point of circle
<< ", radius = " << v3fCircles1[i][2] << "\n"; // radius of circle
// // draw small green circle at center of detected object
cv::circle(imgOriginal, // draw on original image
cv::Point((int)v3fCircles1[i][0], (int)v3fCircles1[i][1]), // center point of circle
3, // radius of circle in pixels
cv::Scalar(0, 255, 0), // draw pure green (remember, its BGR, not RGB)
CV_FILLED); // thickness, fill in the circle
// draw red circle around the detected object
cv::circle(imgOriginal, // draw on original image
cv::Point((int)v3fCircles1[i][0], (int)v3fCircles1[i][1]), // center point of circle
(int)v3fCircles1[i][2], // radius of circle in pixels
cv::Scalar(255, 0, 0), // draw pure red (remember, its BGR, not RGB)
3);
}
// declare windows
cv::namedWindow("imgOriginal", CV_WINDOW_AUTOSIZE);
cv::namedWindow("imgThresh", CV_WINDOW_AUTOSIZE);
// CV_WINDOW_AUTOSIZE is the default
cv::imshow("imgOriginal", imgOriginal);
cv::namedWindow("imgThresh1", CV_WINDOW_AUTOSIZE);
cv::imshow("imgThresh1", imgThresh1);
charCheckForEscKey = cv::waitKey(1);
}
return(0);
}