opencv实现检测瞳孔轮廓

用的是opencv3.4.0 已经实现人眼检测。想已经在检测出人眼的区域找出瞳孔的轮廓并计算面积。但老师运行不出来。是findContours()参数问题还是其他的啊?望大神们给个建议

int main()
{
CascadeClassifier faceCascader, eyeCascader;
String filename1 = "D:/opencv/newbuild/install/etc/haarcascades/haarcascade_frontalface_alt.xml";
String filename2 = "D:/opencv/newbuild/install/etc/haarcascades/haarcascade_eye.xml";
//vector pt; //绘制轨迹
double nowFrameNum, blinkFrameNum, frq;
nowFrameNum = blinkFrameNum = 0;
if (!faceCascader.load(filename1))
{
printf("can not load the face feature data \n");
return -1;
}
if (!eyeCascader.load(filename2))
{
printf("can not load the eye feature data \n");
return -1;
}
namedWindow("input", WINDOW_AUTOSIZE);
VideoCapture capture;
capture.open(0);
Mat frame, gray, src;
vector faces, eyes;

while (capture.read(frame))
{
    flip(frame, frame, 1);
    src = frame;
    cvtColor(frame, gray, COLOR_BGR2GRAY);
    //直方图均衡
    equalizeHist(gray, gray);
    faceCascader.detectMultiScale(gray, faces, 1.1, 3, 0, Size(50, 50));
    Rect roi, rect;
    for (int i = 0; i < faces.size(); i++)
    {
        roi.x = faces[static_cast<int>(i)].x;
        roi.y = faces[static_cast<int>(i)].y;
        roi.width = faces[static_cast<int>(i)].width;
        roi.height = faces[static_cast<int>(i)].height / 2.0;
        Mat faceROI = frame(roi);
        eyeCascader.detectMultiScale(faceROI, eyes, 1.1, 7, 0, Size(20, 20));
        for (int j = 0; j < eyes.size(); j++)
        {
            rect.x = faces[static_cast<int>(i)].x + eyes[j].x + eyes[j].width / 2.0;
            rect.y = faces[static_cast<int>(i)].y + eyes[j].y + eyes[j].height / 2.0;
            rect.width = eyes[j].width;
            rect.height = eyes[j].height;
            circle(frame, Point(rect.x, rect.y), rect.width / 2.0, Scalar(0, 0, 255), 2, 8);

Mat eyesROI = frame(rect);
vector> contours;
vector hireachy;

    //Mat kernal = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
    //morphologyEx(eyesROI,eyesROI, CV_MOP_OPEN, kernal, Point(-1, -1));
    if (rect.width != 0 && roi.width != 0) 
    {
        cvtColor(eyesROI, eyesROI, CV_BGR2GRAY);
        findContours(eyesROI, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0,0));
    } 
        }
        rectangle(frame, faces[static_cast<int>(i)], Scalar(0, 255, 0), 2, 8, 0);
    }
    char q = waitKey(10);
    //Esc退出
    if (q == 27)
    {
        break;
    }

    //double area = contourArea(eyesROI);


    imshow("input", frame);
    nowFrameNum++;
    if (rect.width == 0) blinkFrameNum++;
    if (int(nowFrameNum) % 10 == 0)
    {
        cout << roi.area() << endl;
        cout << "眨眼频率为: " << blinkFrameNum << "  " << nowFrameNum << endl;
        frq = blinkFrameNum / nowFrameNum;
        cout << setprecision(2) << frq << endl;
        //cout << "area is " << area << endl;
    }
}
waitKey(0);
capture.release();
return 0;

}


Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问