Skip to content

Instantly share code, notes, and snippets.

@hecomi
Created September 7, 2013 19:24
Show Gist options
  • Save hecomi/6478469 to your computer and use it in GitHub Desktop.
Save hecomi/6478469 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <opencv2/opencv.hpp>
int main(int argc, char* argv[])
{
// 画像読み込み
auto img = cv::imread("dummy_input.png");
if (!img.data) return -1;
// グレースケール化
cv::Mat gray_img;
cv::cvtColor(img, gray_img, CV_BGR2GRAY);
// ネガポジ反転
gray_img = ~gray_img;
// 平滑化
// cv::medianBlur(gray_img, gray_img, 3);
cv::GaussianBlur(gray_img, gray_img, cv::Size(11, 11), 3, 3);
// 2値化
cv::threshold(gray_img, gray_img, 0, 255, cv::THRESH_BINARY|cv::THRESH_OTSU);
// 円の検出
std::vector<cv::Vec3f> circles;
cv::HoughCircles(gray_img, circles, CV_HOUGH_GRADIENT, 1, 10, 10, 5, 5, 10);
// 円の描画
for (const auto& circle : circles) {
cv::Point center(cv::saturate_cast<int>(circle[0]), cv::saturate_cast<int>(circle[1]));
int radius = cv::saturate_cast<int>(circle[2]);
cv::circle(img, center, radius, cv::Scalar(0, 0, 255), 2);
}
// 表示
const auto window_name = "image";
cv::namedWindow(window_name, CV_WINDOW_AUTOSIZE);
cv::imshow(window_name, img);
// 入力待ち
cv::waitKey();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment