hough circle detection problem

asked 2015-04-01 15:56:56 -0600

Volkan gravatar image


i want to detect outer circle is it possible ?

video is here


#include <iostream>
#include <stdio.h>
#include <vector>
#include <thread>
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"

using namespace cv;

Mat src, src_gray;
Mat dst, detected_edges;

int threshold_value = 11;
int threshold_type = 0;;
int  max_value = 255;
int  max_type = 4;

const char * window_name = "CCC";
string trackbar_type = "Tbin";
string trackbar_value = "Value";

int main(int argc, char *argv[])
    VideoCapture cap;

    cap = VideoCapture("D:/SYDNIA/1.AVI");

    if (!cap.isOpened())  // if not success, exit program
        std::cout << " !! --->> camera problem " << std::endl;
        return -1;
    cvMoveWindow(window_name, 5, 5);

    int MAX = 130;
    createTrackbar("MAX", window_name, &MAX, 300);
    int MIN = 100;
    createTrackbar("MIN", window_name, &MIN, 300);
    int BLACKLEVEL = 47;

    for (;;) {

        if (!cap.read(src))
            std::cout << "GRAB FAILURE" << std::endl;

        cvtColor(src, src_gray, CV_RGB2GRAY);

        blur(src_gray, src_gray, Size(15, 15));

        threshold(src_gray, dst, 11, 255, 0); //tareshold
        vector<Vec3f> circles;
        HoughCircles(dst, circles, CV_HOUGH_GRADIENT, 1, dst.rows, 20, 7, MIN, MAX);

        string status = "";
        for (size_t i = 0; i < circles.size(); i++)

            Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));

            bool ok = false;

            int r = src.at<Vec3b>(center.y, center.x)[0];
            int g = src.at<Vec3b>(center.y, center.x)[1];
            int b = src.at<Vec3b>(center.y, center.x)[2];

            if ((r<BLACKLEVEL) && (g<BLACKLEVEL) && (b<BLACKLEVEL))ok = true;

            if (ok)
                int radius = cvRound(circles[0][2]);
                circle(src, center, 2, Scalar(30, 255, 140), -1, 3, 0);
                circle(src, center, radius, Scalar(30, 255, 0), 3, 8, 0);
                status = "2";
                status = "0";


        imshow(window_name, src);
        imshow("HSV", dst);

        if (waitKey(1) == 27)break;


    return 0;


source picture :

image description

code output center:

image description

is it possible to make it :

image description

edit retag flag offensive close merge delete


May be you can use only thresholding. Calculate ellipse moment and use a projection to enlarge your shape. You don't detect outside circle but you use a parametric model to find it.

LBerger gravatar imageLBerger ( 2015-04-02 02:42:41 -0600 )edit

Interesting problem. But do you really need the second circle? I guess you want to fill the truck (?) automatically, so you only need the xy-coordinates of the hole. Do you know the z-position of the hole (e.g. the height of the truck?) Could you specify the problem a bit better? Could you use two cameras for a stereo setup?

FooBar gravatar imageFooBar ( 2015-04-02 03:55:24 -0600 )edit

yes i want to fill truck automatically but we dont know Z(the height of the truck) its variable so circle center is not correct stereo setup is good for me ?

Volkan gravatar imageVolkan ( 2015-04-02 04:41:07 -0600 )edit

If your camera is not at vertical of hhole, you 've got an ellipse with axis a and b. (hole is in the truck is supposed to be a circular disc). If all truck have got same size then I think ratio a/b give you distance camera-truck. Ratio a/b give you too how many pixels you have to enlarge your ellipse to find outside ellipse.

Truck must follow same trajectory relative to camera axis

LBerger gravatar imageLBerger ( 2015-04-02 05:04:58 -0600 )edit

A stereo setup would simplfy the problem. Do you know the radius of the hole? In this case you could just find the upmost point off the black hole in both images. If you have two cameras, you can compute the 3d coordinate of this point in 3d. And your final goal has just a distance of the radius from this point. This also works with a single camera: The upper half of the black area is an ellipse (not mathematically exact, but good enough). If you know the radius, you can again compute the position of your hole.

FooBar gravatar imageFooBar ( 2015-04-02 06:43:45 -0600 )edit

radius of hole between 500-700 (cm) but is it impossible to work with one camera

Volkan gravatar imageVolkan ( 2015-04-02 13:58:01 -0600 )edit