Skip to content

Instantly share code, notes, and snippets.

@Noxalus
Created May 29, 2015 08:41
Show Gist options
  • Save Noxalus/18de2603812e31e687de to your computer and use it in GitHub Desktop.
Save Noxalus/18de2603812e31e687de to your computer and use it in GitHub Desktop.
Detect red circles with OpenCV 2.4.11
Mat src(height, width, CV_8UC4, const_cast<unsigned char*>(imageData.data()));
Mat src_gray, hsv, final;
// Create the final image to display
cvtColor(src, final, CV_BGRA2RGBA);
// Remove useless alpha canal
cvtColor(src, src, CV_BGRA2BGR);
// Convert image to HSV
cvtColor(src, hsv, COLOR_BGR2HSV);
Mat lower_red_hue_range;
Mat upper_red_hue_range;
inRange(hsv, cv::Scalar(0, 100, 100), cv::Scalar(10, 255, 255), lower_red_hue_range);
inRange(hsv, cv::Scalar(160, 100, 100), cv::Scalar(179, 255, 255), upper_red_hue_range);
Mat red_hue_image;
addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
GaussianBlur(red_hue_image, red_hue_image, cv::Size(9, 9), 2, 2);
// Use the Hough transform to detect circles in the combined threshold image
std::vector<cv::Vec3f> circles;
HoughCircles(red_hue_image, circles, CV_HOUGH_GRADIENT, 1, red_hue_image.rows / 8, 100, 20, 0, 0);
@shaimaa93
Copy link

excellent job ! red color in the range of 0 to 10 and 160 to 180. can i know green and blue range ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment