HoughCircle与轨道栏opencv

时间:2015-09-19 03:27:58

标签: c++ opencv opencv3.0 hough-transform

HoughCircle功能中有一些参数。 有没有办法使用轨道栏来更改这些参数? 这样我每次想要更改它时都不需要运行程序。

谢谢。

在Win 8笔记本电脑上的VS 2013上使用opencv 3.0.0 C ++

#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) {

    //Create a window for trackbars
    namedWindow("Trackbar Window", CV_WINDOW_AUTOSIZE);

    //Create trackbar to change brightness
    int iSliderValue1 = 50;
    createTrackbar("Brightness", "Trackbar Window", &iSliderValue1, 100);

    //Create trackbar to change contrast
    int iSliderValue2 = 50;
    createTrackbar("Contrast", "Trackbar Window", &iSliderValue2, 100);

    int param1 = 10;
    createTrackbar("param1", "Trackbar Window", &param1, 300);

    int param2 = 10;
    createTrackbar("param2", "Trackbar Window", &param2, 300);

    cout << "All trackbars created" << endl;

    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);

    cout << "Vidoe opened" << endl;

    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);
        Mat dst;
        int iBrightness = iSliderValue1 - 50;
        double dContrast = iSliderValue2 / 50.0;
        src.convertTo(src, -1, dContrast, iBrightness);
        cout << "1" << endl;

        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;
    cout << "2" << endl;

    double dparam1 = param1 / 1.0;
    double dparam2 = param2 / 1.0;
    HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT,
        2,   // accumulator resolution (size of the image / 2)
        5,  // minimum distance between two circles
        dparam1, // Canny high threshold
        dparam2, // minimum number of votes
        0, 0); // min and max radius
    cout << "3" << endl;
    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);
    cout << "5" << endl;
    cvWaitKey(33);
    }
    return 0;
}

我有cout表达式来帮助我找到错误。 在窗口中,它显示为这样 enter image description here

HoughCircle功能存在问题。

代码在数字2处停止,在10秒之后,它跳转到3并循环。我看不出有什么问题。在添加轨迹栏之前,代码本身(不谈论检测部分)工作正常。或者某种程度上该程序没有响应。

可能是什么问题

谢谢

2 个答案:

答案 0 :(得分:0)

我认为houghCircle没有问题......但是你选择的参数给了很多圈子,这需要时间来处理这就是为什么它等到10秒再去3之前尝试增加参数2,它会带来更快的结果。

在这里,我尝试使用Python中的图像,您可以尝试从中移植...

import cv2
import numpy as np

img = cv2.imread('C:/Python34/images/2.jpg',0)
cv2.namedWindow('image')
def nothing(x):
    pass
cv2.createTrackbar('Param 1','image',0,100,nothing)
cv2.createTrackbar('Param 2','image',0,100,nothing)
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image',0,1,nothing)

while(1):
    cv2.imshow('image',img)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
    #To Get Parameter values from Trackbar Values
    para1 = cv2.getTrackbarPos('Param 1','image')
    para2 = cv2.getTrackbarPos('Param 2','image')
    s = cv2.getTrackbarPos(switch,'image')
    if s == 0:
        cv2.imshow('image', img)
    else:
    #For finding Hough Circles according to trackbar parameters
        circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,para1,para2,minRadius=0,maxRadius=0)
        circles = np.uint16(np.around(circles))
        #For drawing Hough Circles
        for i in circles[0,:]:
           cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
           cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)
           cv2.imshow('image', img)
        cv2.waitKey(0)
        img = cv2.imread('C:/Python34/images/2.jpg',0)


cv2.destroyAllWindows()

您可以使用上面的代码作为参考,首先它为开关创建一个窗口和轨道栏,为霍夫圈创建两个参数。 然后在while循环中,para1和para2将轨道栏的位置存储为canny参数的值。 然后在cv2.HoughCircles函数中使用它并绘制圆圈。 再次加载图像,以便每次更改参数时,都会在新图像上显示输出以避免混淆。

希望这可能有用。

答案 1 :(得分:-1)

您错过了一个图书馆math.h,并在标题中添加了#include <math.h>