Ask Your Question
1

hough circle transform error

asked 2016-04-26 06:55:06 -0600

Dasein gravatar image

updated 2016-04-26 07:27:42 -0600

Hello guys , I am trying to do hough circle transform in order to find pupil in eyeROI.But It doesnt work ..Do you have any idea about how to fix this problem ? Here is the code :

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace std;
using namespace cv;

/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";
String window_name2 = "right eye";
String window_name3 = "left eye";



Rect shrinkRect(Rect rect, int width_percent, int height_percent)
{
    if (width_percent > 100) width_percent = 100;
    if (height_percent > 100) height_percent = 100;

    Rect newrect;
    newrect.width = (rect.width * width_percent) / 100;
    newrect.height = (rect.height * height_percent) / 100;
    newrect.x = rect.x + (rect.width - newrect.width) / 2;
    newrect.y = rect.y + (rect.height - newrect.height) / 2;

    return newrect;
}

Rect expandRect(Rect rect, int width_percent, int height_percent)
{
    // not tested strongly
    Rect newrect;
    newrect.width = rect.width + ((rect.width * width_percent) / 100);
    newrect.height = rect.height + ((rect.height * height_percent) / 100);
    newrect.x = rect.x + (rect.width - newrect.width) / 2;
    newrect.y = rect.y + (rect.height - newrect.height) / 2;

    return newrect;
}

void updateEyeRects(vector<Rect> eyes, Rect& leftEyeRect, Rect& rightEyeRect)
{
    if (eyes.size() < 2) return;
    Rect intersection = eyes[0] & eyes[1];
    if (intersection.width > 1) return;

    if (eyes[0].x < eyes[1].x)
    {
        leftEyeRect = eyes[0];
        rightEyeRect = eyes[1];
    }
    else
    {
        leftEyeRect = eyes[1];
        rightEyeRect = eyes[0];
    }
}

std::vector<Rect> detectEyes(Mat frame, Rect faceRect)
{
    std::vector<Rect> faces;
    if (faceRect.width > 0)
    {
        faces.push_back(faceRect);
    }

    std::vector<Rect> eyes;
    Mat frame_gray;

    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);

    //-- Detect faces
    if (faces.size() == 0)
    {
        equalizeHist(frame_gray, frame_gray);
        face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0, Size(80, 80));
    }

    for (size_t i = 0; i < faces.size(); i++)
    {
        faces[i].height -= faces[i].height / 2;
        faces[i].y += faces[i].height / 3;
        Mat faceROI = frame_gray(faces[i]);
        equalizeHist(faceROI, faceROI);
        //-- In each face, detect eyes
        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

        for (size_t j = 0; j < eyes.size(); j++)
        {
            eyes[j].x += faces[i].x;
            eyes[j].y += faces[i].y;
        }
    }
    return eyes;
}






int main(int argc, char** argv)
{
    VideoCapture capture;
    Mat frame;

    //-- 1. Load the cascade
    if (!face_cascade.load(face_cascade_name))
    {
        printf("--(!)Error loading face cascade\n");
        return -1;
    };
    if (!eyes_cascade.load(eyes_cascade_name))
    {
        printf("--(!)Error loading eyes cascade\n");
        return -1;
    };

    // Read the video stream or open the web cam
    if (argc > 1)
        capture.open(argv[1]);
    else
        capture.open(0);

    if (!capture.isOpened())
    {
        printf("--(!)Error opening video capture\n");
        return -1;
    }
    Mat left_frame, right_frame;
    Rect leftEyeRect;
    Rect rightEyeRect;
    vector<Rect> eyes;
    int fail_count = 0;

    while (capture.read(frame))
    {
        double t = (double)getTickCount();
        if (frame.empty())
        {
            printf(" --(!) No captured frame -- Break!");
            break;
        }

        if ((eyes.size() < 2) & !(leftEyeRect.width & rightEyeRect.width))
        {
            putText(frame, "Searching for an eye pair..", Point(20, 20), 0, 0.6, Scalar(0, 255, 0));
            eyes = detectEyes(frame, Rect());
            updateEyeRects(eyes, leftEyeRect, rightEyeRect);
        }
        else
        {
            Rect eyesRect(leftEyeRect | rightEyeRect);
            eyesRect = expandRect(eyesRect, 30, 300);
            eyesRect = eyesRect & Rect(0, 0, frame.cols, frame ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-04-26 08:57:29 -0600

jmbapps gravatar image

updated 2016-04-26 09:01:18 -0600

Your parameters are way off and need to be fine tuned. You cant just put numbers in, you must understand what each parameter does so you can fine tune HoughCirlces. Instead of me typing for 30 mins....read this. http://answers.opencv.org/question/91...

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2016-04-26 06:55:06 -0600

Seen: 231 times

Last updated: Apr 26 '16