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.rows);
eyes = detectEyes(frame, eyesRect);
if (eyes.size() < 2)
{
fail_count++;
}
else updateEyeRects(eyes, leftEyeRect, rightEyeRect);
}
left_frame = frame(leftEyeRect);
right_frame = frame(rightEyeRect);
cvtColor(left_frame, left_frame, CV_BGR2GRAY);
cvtColor(right_frame, right_frame, CV_BGR2GRAY);
equalizeHist(left_frame, left_frame);
equalizeHist(right_frame, right_frame);
threshold(left_frame, left_frame, 40, 255, THRESH_BINARY);
threshold(right_frame, right_frame, 40, 255, THRESH_BINARY);
Mat kernel = Mat::ones(Size(2, 2), left_frame.type());
Mat kernel2 = Mat::ones(Size(2, 2), right_frame.type());
dilate(left_frame, left_frame, kernel, Point(-1, -1), 2, 0, Scalar(255, 255, 255));
dilate(right_frame, right_frame, kernel, Point(-1, -1), 2, 0, Scalar(255, 255, 255));
erode(left_frame, left_frame, kernel, Point(-1, -1), 2, 0, Scalar(255, 255, 255));
erode(right_frame, right_frame, kernel, Point(-1, -1), 2, 0, Scalar(255, 255, 255));
GaussianBlur( left_frame, left_frame, Size(9, 9), 2, 2 );
vector<vec3f> circles;
/// Apply the Hough Transform to find the circles HoughCircles( left_frame, circles, CV_HOUGH_GRADIENT, 1,
left_frame.rows/8, 200, 100, 0, 0 );
/// Draw the circles detected 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 center circle( left_frame, center, 3, Scalar(0,255,0), -1, 8, 0 ); // circle outline circle( left_frame, center, radius, Scalar(0,0,255), 3, 8, 0 ); }
if (fail_count > 10)
{
fail_count = 0;
leftEyeRect = Rect();
rightEyeRect = Rect();
}
t = (double)getTickCount() - t;
double fps = getTickFrequency() / t;
static double avgfps = 0;
static int nframes = 0;
nframes++;
double alpha = nframes > 50 ? 0.01 : 1. / nframes;
avgfps = avgfps*(1 - alpha) + fps*alpha;
putText(frame, format("fps: %.1f - %d", avgfps, nframes), Point(20, 30),
FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0, 255, 0), 2);
imshow(window_name, frame);
if (!right_frame.empty()){
imshow(window_name2, right_frame);
}
if (!left_frame.empty()) {
imshow(window_name3, left_frame);
}
//-- bail out if escape was pressed
int c = waitKey(1);
if ((char)c == 27)
{
break;
}
}
return 0;
}