opencv - 在视频中跟踪眼瞳

标签 opencv computer-vision eye-tracking

我正在进行一个旨在追踪眼瞳的项目。为此,我制作了一个可以捕捉眼睛图像的头戴式系统。完成了硬件部分,我对软件感兴趣。我正在使用 opencv。请让我知道跟踪学生的最有效方法是什么。 Houghcircles 表现不佳。

在那之后,我也尝试过使用 HSV 过滤器,这是代码和 链接到原始图像和处理过的图像的屏幕截图。请帮我解决这个问题。该链接还包含我在此代码中使用的眼瞳视频。

https://picasaweb.google.com/118169326982637604860/16November2011?authuser=0&authkey=Gv1sRgCPKwwrGTyvX1Aw&feat=directlink

代码:

include "cv.h"

include"highgui.h"

IplImage* GetThresholdedImage(IplImage* img)
{

    IplImage *imgHSV=cvCreateImage(cvGetSize(img),8,3);
    cvCvtColor(img,imgHSV,CV_BGR2HSV);
    IplImage *imgThresh=cvCreateImage(cvGetSize(img),8,1);
    cvInRangeS(imgHSV,cvScalar(0, 84, 0, 0),cvScalar(179, 256, 11, 0),imgThresh);
    cvReleaseImage(&imgHSV);
    return imgThresh;
}

void main(int *argv,char **argc)
{

    IplImage *imgScribble= NULL;
    char c=0;
    CvCapture *capture;
    capture=cvCreateFileCapture("main.avi");

    if(!capture)
    {
        printf("Camera could not be initialized");
        exit(0);
    }
    cvNamedWindow("Simple");
    cvNamedWindow("Thresholded");

    while(c!=32)
    {
        IplImage *img=0;
        img=cvQueryFrame(capture);
        if(!img)
            break;
        if(imgScribble==NULL)
            imgScribble=cvCreateImage(cvGetSize(img),8,3);

        IplImage *timg=GetThresholdedImage(img);
        CvMoments *moments=(CvMoments*)malloc(sizeof(CvMoments));
        cvMoments(timg,moments,1);

        double moment10 = cvGetSpatialMoment(moments, 1, 0);
        double moment01 = cvGetSpatialMoment(moments, 0, 1);
        double area = cvGetCentralMoment(moments, 0, 0);

        static int posX = 0;
        static int posY = 0;

        int lastX = posX;
        int lastY = posY;

        posX = moment10/area;
        posY = moment01/area;
         // Print it out for debugging purposes
        printf("position (%d,%d)\n", posX, posY);
        // We want to draw a line only if its a valid position
        if(lastX>0 && lastY>0 && posX>0 && posY>0)
        {
            // Draw a yellow line from the previous point to the current point
            cvLine(imgScribble, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(0,255,255), 5);
        }
        // Add the scribbling image and the frame...

        cvAdd(img, imgScribble, img);

        cvShowImage("Simple",img);
        cvShowImage("Thresholded",timg);
        c=cvWaitKey(3);
        cvReleaseImage(&timg);
        delete moments;

    }
    //cvReleaseImage(&img);
    cvDestroyWindow("Simple");
    cvDestroyWindow("Thresholded");

}

我能够跟踪眼睛并精确找到瞳孔的中心坐标。

首先,我对头戴式摄像头拍摄的图像进行了阈值处理。之后我使用了轮廓查找算法,然后我找到了所有轮廓的质心。这给了我眼睛瞳孔的中心坐标,这种方法实时运行良好,并且可以非常准确地检测眨眼。

现在,我的目标是将此功能嵌入到游戏(赛车游戏)中。其中,如果我向左/向右看,汽车就会向左/向右移动,如果我眨眼,汽车就会减速。我现在该怎么办???我需要一个游戏引擎来做到这一点吗?

听说有一些兼容visual studio 2010的开源游戏引擎(unity等)。可行吗???如果是,我应该如何进行?

最佳答案

我是 SimpleCV 的开发者之一。我们维护一个用于计算机视觉的开源 python 库。您可以在SimpleCV.org下载。 . SimpleCV 非常适合通过破解命令行来解决这些类型的问题。我只用了几行代码就可以提取瞳孔。给你:

img = Image("eye4.jpg") # load the image
bm = BlobMaker() # create the blob extractor
# invert the image so the pupil is white, threshold the image, and invert again
# and then extract the information from the image
blobs = bm.extractFromBinary(img.invert().binarize(thresh=240).invert(),img)

if(len(blobs)>0): # if we got a blob
    blobs[0].draw() # the zeroth blob is the largest blob - draw it
    locationStr = "("+str(blobs[0].x)+","+str(blobs[0].y)+")"
    # write the blob's centroid to the image
    img.dl().text(locationStr,(0,0),color=Color.RED)
    # save the image
    img.save("eye4pupil.png")
    # and show us the result.
    img.show()

Here are the results.

因此,您的下一步是使用某种跟踪器(例如卡尔曼滤波器)来稳健地跟踪瞳孔。您可能希望将眼睛建模为球体并在球坐标(即 theta 和 phi)中跟踪瞳孔的质心。您还需要编写一些代码来检测眨眼事件,这样系统就不会在用户眨眼时变得异常不稳定。我建议使用精明的边缘检测器来找到图像中最大的水平线,并假设它们是眼睑。我希望这对您有所帮助,请告诉我们您的工作进展情况。

关于opencv - 在视频中跟踪眼瞳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8145725/

相关文章:

python - 如何解释 matchtemplate 输出? (开放式计算机,Python)

Python使用均方根法拟合多条直线

c++ - 包含从源代码编译的 ffmpeg 时从源代码编译 opencv 时出现链接错误

python - 检测圆形形状opencv

swift - ARKit中的FaceTracking – 如何在屏幕上显示 "lookAtPoint"

linux - 安装ridgerun sdk - Leopard board DM

python - OpenCV python 的 API : FlannBasedMatcher

python - 错误:(-210) warpMatrix 必须是函数 cv::findTransformECC 中的单 channel 浮点矩阵

c - 通过 OpenCV 跟踪打印眼睛坐标

algorithm - 眼动追踪 : finding the pupil (x, y)