Created
December 4, 2016 11:23
-
-
Save Sinitca-Aleksandr/7fb240b596351e16bb83d3b7c045a15e to your computer and use it in GitHub Desktop.
Пример применения функции HoughCircles
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 "stdafx.h" | |
#include "opencv2/highgui/highgui.hpp" | |
#include "opencv2/imgcodecs.hpp" | |
#include "opencv2/imgproc/imgproc.hpp" | |
#include <iostream> | |
#include <stdio.h> | |
using namespace cv; | |
int main(int argc, char** argv) | |
{ | |
Mat src, src_gray; | |
// Загрузка изображения | |
src = imread("icon.jpg", 1); | |
if (!src.data) | |
{ | |
return -1; | |
} | |
// Корвертирование изображения в градации серого | |
cvtColor(src, src_gray, CV_BGR2GRAY); | |
// Фильтрация шума, для избежания ложного обнаружения круга | |
GaussianBlur(src_gray, src_gray, Size(9, 9), 2, 2); | |
std::vector <Vec3f> circles; | |
// Применение Hough Transform для нахождения кругов | |
HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows / 8, 200, 100, 0, 0); | |
// Отрисовка найденых кругов | |
for (size_t i = 0; i < circles.size(); i++) | |
{ | |
Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); | |
int radius = cvRound(circles[i][2]); | |
// Центр окружности | |
circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0); | |
// Граница окружности | |
circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0); | |
} | |
// Вывод результата | |
namedWindow("Hough Circle Transform", CV_WINDOW_AUTOSIZE); | |
imshow("Hough Circle Transform", src); | |
waitKey(0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment