1 | initial version |
This should be the equivalent C++ variant
#include "opencv2/opencv.hpp"
#include <stdio.h>
int main(int argc, char* argv[])
{
VideoCapture camera (0); // Use the default camera
Mat frame;
// capturing some extra frames seems to help stability
// do this for stability --> give number of iterations, depends on camera
n = 3;
for(int i = 0; i < n; i++){
capture >> frame;
}
// with default driver, PSEye is 640 x 480
Mat hsv_frame (Size(640, 480), CV_8UC3);
Mat thresholded (Size(640, 480), CV_8UC1);
Mat thresholded2 (Size(640, 480), CV_8UC1);
Scalar hsv_min (0, 50, 170); Scalar hsv_min2 (170, 50, 170);
Scalar hsv_max (10, 180, 256); Scalar hsv_max2 (256, 180, 256);
camera >> frame;
if( !frame.empty() ){
// color detection using HSV
cvtColor(frame, hsv_frame, CV_BGR2HSV);
// to handle color wrap-around, two halves are detected and combined
inRange(hsv_frame, hsv_min, hsv_max, thresholded);
inRange(hsv_frame, hsv_min2, hsv_max2, thresholded2);
bitwise_or(thresholded, thresholded2, thresholded);
imwrite("PATH/thresholded.jpg", thresholded);
// hough detector works better with some smoothing of the image
GaussianBlur(thresholded, thresholded, Size(9,9))
vector<Vec3f> circles;
HoughCircles(thresholded, circles, CV_HOUGH_GRADIENT, 2, thresholded->height / 4, 100, 40, 20, 200);
for (int i = 0; i < circles.size(); i++)
{
printf("Ball! x=%f y=%f r=%f\n\r", circles[i][0], circles[i][1], circles[i][2]);
circle(frame, Point((int)circles[i][0], (int)circles[i][1]), 3, CV_RGB(0, 255, 0), -1, 8, 0);
circle(frame, Point((int)circles[i][0], (int)circles[i][1]), (int)circles[i][2], CV_RGB(255, 0, 0), 3, 8, 0);
}
imwrite("PATH/frame.jpg", frame);
}
else {
printf("No frame retrieved! \n");
}
return 0;
}
2 | No.2 Revision |
This should be the equivalent C++ variant
#include "opencv2/opencv.hpp"
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
VideoCapture camera (0); // Use the default camera
Mat frame;
// capturing some extra frames seems to help stability
// do this for stability --> give number of iterations, depends on camera
int n = 3;
for(int i = 0; i < n; i++){
capture camera >> frame;
}
// with default driver, PSEye is 640 x 480
Mat hsv_frame (Size(640, 480), CV_8UC3);
Mat thresholded (Size(640, 480), CV_8UC1);
Mat thresholded2 (Size(640, 480), CV_8UC1);
Scalar hsv_min (0, 50, 170); Scalar hsv_min2 (170, 50, 170);
Scalar hsv_max (10, 180, 256); Scalar hsv_max2 (256, 180, 256);
camera >> frame;
if( !frame.empty() ){
// color detection using HSV
cvtColor(frame, hsv_frame, CV_BGR2HSV);
// to handle color wrap-around, two halves are detected and combined
inRange(hsv_frame, hsv_min, hsv_max, thresholded);
inRange(hsv_frame, hsv_min2, hsv_max2, thresholded2);
bitwise_or(thresholded, thresholded2, thresholded);
imwrite("PATH/thresholded.jpg", thresholded);
// hough detector works better with some smoothing of the image
GaussianBlur(thresholded, thresholded, Size(9,9))
Size(9,9), 0, 0);
vector<Vec3f> circles;
HoughCircles(thresholded, circles, CV_HOUGH_GRADIENT, 2, thresholded->height thresholded.rows / 4, 100, 40, 20, 200);
for (int i = 0; i < circles.size(); i++)
{
printf("Ball! x=%f y=%f r=%f\n\r", circles[i][0], circles[i][1], circles[i][2]);
circle(frame, Point((int)circles[i][0], (int)circles[i][1]), 3, CV_RGB(0, 255, 0), -1, 8, 0);
circle(frame, Point((int)circles[i][0], (int)circles[i][1]), (int)circles[i][2], CV_RGB(255, 0, 0), 3, 8, 0);
}
imwrite("PATH/frame.jpg", frame);
}
else {
printf("No frame retrieved! \n");
}
return 0;
}