Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hough Circle with trackbars

There are a few parameters in the HoughCirclefunction. Is there a way to use the trackbars to change these parameters? so that I would not need to run the program every time i want to change them.

Thank you.

using opencv 3.0.0 C++ on Ms VS 2013 on Win 8 laptop

#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;
}

I have the cout expression to help me with locating the bug. In the window, it is showing like this enter image description here CMD window

And there is a problem with the HoughCircle function.

The code stops at number 2, and after like 10 seconds, it jump to 3 and loop over. I can't see what is the problem. The code itself(without talking about the detection part) is working fine before adding the trackbar. Or somehow the program just not responding.

What could be the problem

Thank you!

Hough Circle with trackbars

There are a few parameters in the HoughCirclefunction. Is there a way to use the trackbars to change these parameters? so that I would not need to run the program every time i want to change them.

Thank you.

using opencv 3.0.0 C++ on Ms VS 2013 on Win 8 laptop

#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;
100;
    createTrackbar("param1", "Trackbar Window", &param1, 300);

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

    int minR = 0;
    createTrackbar("minRadius", "Trackbar Window", &minR, 300);

    int maxR = 0;
    createTrackbar("maxRadius", "Trackbar Window", &maxR, 300);


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

     Mat src;

    VideoCapture capture;

    capture.open("movingBall.wmv");
//capture.open("GOPR0496.MP4");
    capture.open(0);
    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); minR, maxR); // 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);
        putText(src, "Circle Found", Point(0, 50), 1,2,Scalar(0, 255, 0),2);
    }

    /*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);
    imshow("image1", src);
    cout << "5" << endl;
    cvWaitKey(33);
    }
    return 0;
}

I have the cout expression to help me with locating the bug. In the window, it is showing like this enter image description here CMD window

And there is a problem with the HoughCircle function.

The code stops at number 2, and after like 10 seconds, it jump to 3 and loop over. I can't see what is the problem. The code itself(without talking about the detection part) is working fine before adding the trackbar. Or somehow the program just not responding.

What could be the problem

Thank you!