Created
October 20, 2015 21:59
-
-
Save brendan-w/19c0f1584e8b44f87a7e to your computer and use it in GitHub Desktop.
Quick and dirty OpenCV program for counting christmas lights
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include <opencv2/features2d/features2d.hpp> | |
using namespace std; | |
using namespace cv; | |
int min_dist = 0; | |
int min_area = 0; | |
int max_area = 139; | |
int threshold_type = 3; | |
int threshold_value = 0; | |
int lower_hue = 0; | |
int upper_hue = 0; | |
int lower_threshold = 0; | |
int upper_threshold = 255; | |
char* window_name = "Light Counter"; | |
char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted"; | |
char* trackbar_value = "Value"; | |
Mat src, dst; | |
SimpleBlobDetector* detector; | |
void Threshold_Demo( int, void* ) | |
{ | |
/* 0: Binary | |
1: Binary Inverted | |
2: Threshold Truncated | |
3: Threshold to Zero | |
4: Threshold to Zero Inverted | |
*/ | |
// stopped using this in favor of inRange() | |
// threshold(src, dst, threshold_value, 255, threshold_type); | |
// inRange(src, Scalar(lower_hue, lower_threshold, lower_threshold), Scalar(upper_hue, upper_threshold, upper_threshold), dst); | |
inRange(src, Scalar(35, lower_threshold, lower_threshold), Scalar(119, upper_threshold, upper_threshold), dst); | |
//make things a little more blobby | |
blur( dst, dst, Size( 6, 6 ), Point(-1,-1) ); | |
SimpleBlobDetector::Params params; | |
params.minDistBetweenBlobs = (float) min_dist; | |
params.filterByInertia = false; | |
params.filterByConvexity = false; | |
params.filterByColor = false; | |
params.filterByCircularity = false; | |
params.filterByArea = true; | |
params.minArea = (float) min_area; | |
params.maxArea = (float) max_area; | |
//not exactly efficient, but it's midnight | |
detector = new SimpleBlobDetector(params); | |
vector<KeyPoint> keypoints; | |
detector->detect(dst, keypoints); | |
delete detector; | |
drawKeypoints(dst, keypoints, dst); | |
std::cout << keypoints.size() << std::endl; | |
imshow(window_name, dst); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
if( argc != 2) | |
{ | |
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl; | |
return -1; | |
} | |
src = imread(argv[1], CV_LOAD_IMAGE_COLOR); | |
if(! src.data ) | |
{ | |
cout << "Could not open or find the image" << std::endl ; | |
return -1; | |
} | |
//convert to HSV space | |
cvtColor(src, src, CV_BGR2HSV); | |
namedWindow(window_name, WINDOW_AUTOSIZE); | |
createTrackbar( "min distance", | |
window_name, &min_dist, | |
500, Threshold_Demo ); | |
createTrackbar( "min area", | |
window_name, &min_area, | |
500, Threshold_Demo ); | |
createTrackbar( "max area", | |
window_name, &max_area, | |
500, Threshold_Demo ); | |
//these are controls for tuning the inRange function above | |
// createTrackbar( "Lower", | |
// window_name, &lower_threshold, | |
// 255, Threshold_Demo ); | |
// createTrackbar( "Upper", | |
// window_name, &upper_threshold, | |
// 255, Threshold_Demo ); | |
// createTrackbar( "Lower Hue", | |
// window_name, &lower_hue, | |
// 179, Threshold_Demo ); | |
// createTrackbar( "Upper Hue", | |
// window_name, &upper_hue, | |
// 179, Threshold_Demo ); | |
// I was playing with threshold(), but ultimately used inRange() instead | |
// createTrackbar( trackbar_type, | |
// window_name, &threshold_type, | |
// 4, Threshold_Demo ); | |
// createTrackbar( trackbar_value, | |
// window_name, &threshold_value, | |
// 255, Threshold_Demo ); | |
Threshold_Demo(0, NULL); | |
waitKey(0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment