opencv hough circle function

时间:2015-09-17 06:35:18

标签: opencv hough-transform

我正在使用此代码检测视频文件中的圆圈

#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
using namespace cv;

int main(int argc, char** argv) {


using namespace cv;



    Mat src;
    VideoCapture capture;
    capture.open("movingBall.wmv");
    capture.read(src);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
    capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);


if (!src.data) {
    std::cout << "ERROR:\topening image" << std::endl;
    return -1;
}
cv::namedWindow("image1", CV_WINDOW_AUTOSIZE);
cv::namedWindow("image2", CV_WINDOW_AUTOSIZE);

while (true){
capture.read(src);

cv::imshow("image1", src);

Mat src_gray2;
cvtColor(src, src_gray2, CV_BGR2GRAY);

GaussianBlur(src_gray2, src_gray2, cv::Size(9, 9), 2, 2);

vector<Vec3f> circles;

HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT,
    2,   // accumulator resolution (size of the image / 2)
    5,  // minimum distance between two circles
    70, // Canny high threshold
    100, // minimum number of votes
    0, 0); // min and max radius

std::cout << circles.size() << std::endl;
std::cout << "end of test" << std::endl;


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 outline
    circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);
}

/*std::vector<cv::Vec3f>::
    const_iterator itc = circles.begin();

while (itc != circles.end()) {

    cv::circle(src_gray2,
        cv::Point((*itc)[0], (*itc)[1]), // circle centre
        (*itc)[2],       // circle radius
        cv::Scalar(0,0,0), // color
        2);              // thickness

    ++itc;
}*/

cv::imshow("image2", src_gray2);

cvWaitKey(33);
}
return 0;
}

我面临的问题是,尽管我更改了HoughCircle参数,但我无法在视频文件中检测到任何圆圈。

是否有可能创建一个跟踪栏来监控这些参数?

对于图像,我可以看到被检测到的圆圈,但它不是我想要的圆圈,这仍然可以。

在Win 8笔记本电脑上使用OPENCV3.0.0和MS Visual Studio 2013

测试视频 https://youtu.be/Bf9chPN1Q8w

测试图像 https://www.google.com.sg/search?q=5.8ghz+antenna&espv=2&biw=1366&bih=665&source=lnms&tbm=isch&sa=X&ved=0CAYQ_AUoAWoVChMI_OGkk8P9xwIVTxuOCh2P-gcz#imgrc=lGGyH8F4_t0Y2M%3A

0 个答案:

没有答案