Click here to Skip to main content
15,790,135 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i want to detect an Eye, i have some code where i can detect blue color object, so if i made changes(how i can?) then it would be possible for me to detect an eye. as below color has its own specific range value so, if i specify the eye color HSV value then can i detect EYE with this method.

In this below code i am going to detect BLUE Color Object, please tell me that where i do changes in my code so that i could get EYE using Open CV.
     IplImage* GetThresholdedImage(IplImage* img)
	// Convert the image into an HSV image
	IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3);
	cvCvtColor(img, imgHSV, CV_BGR2HSV);

	IplImage* imgThreshed = cvCreateImage(cvGetSize(img), 8, 1);
      //For detecting BLUE color i have this HSV value, 
	 cvInRangeS(imgHSV, cvScalar(112, 100, 100), cvScalar(124, 255, 255), imgThreshed);//this will not recognize the yellow color
	  return imgThreshed;
Updated 20-Jan-13 1:21am
nv3 20-Jan-13 10:30am    
Checking for a specific color value is not going to be very successful for eye detection. Remember, there are people with brown eyes, too!

1 solution

Detecting Eye is moreover a template matching problem which can be done through cross correlation of a template image and main image.

Here is a simple method in OpenCV C for detecting eye.

locate_eye(IplImage* img, IplImage* tpl, CvRect* window, CvRect* eye)
	IplImage*	tm;
	CvRect		win;
	CvPoint		minloc, maxloc, point;
	double		minval, maxval;
	int			w, h;

	/* get the centroid of eye */
	point = cvPoint(
		(*eye).x + (*eye).width / 2, 
		(*eye).y + (*eye).height / 2

	/* setup search window 
	   replace the predefined WIN_WIDTH and WIN_HEIGHT above 
	   for your convenient */
	win = cvRect(
		point.x - WIN_WIDTH / 2,
		point.y - WIN_HEIGHT / 2,

	/* make sure that the search window is still within the frame */
	if (win.x < 0)
		win.x = 0;
	if (win.y < 0)
		win.y = 0;
	if (win.x + win.width > img->width)
		win.x = img->width - win.width;
	if (win.y + win.height > img->height)
		win.y = img->height - win.height;

	/* create new image for template matching result where: 
	   width  = W - w + 1, and
	   height = H - h + 1 */
	w  = win.width  - tpl->width  + 1;
	h  = win.height - tpl->height + 1;
	tm = cvCreateImage(cvSize(w, h), IPL_DEPTH_32F, 1);

	/* apply the search window */
	cvSetImageROI(img, win);

	/* template matching */
	cvMatchTemplate(img, tpl, tm, CV_TM_SQDIFF_NORMED);
	cvMinMaxLoc(tm, &minval, &maxval, &minloc, &maxloc, 0);

	/* release things */

	/* only good matches */
	if (minval > TM_THRESHOLD)
		return 0;

	/* return the search window */
	*window = win;

	/* return eye location */
	*eye = cvRect(
		win.x + minloc.x,
		win.y + minloc.y,

	return 1;

where img is the main frame image, tpl is the template image whose size must be very small in comparison to the main frame, eye is rectangle defining eye template, window is the rectangle defining main frame. You can modify the method and parameters as par your requirements.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900