人眼定位识别

2018-06-10  本文已影响0人  影醉阏轩窗
  • 本系列历程启发于“禾路老师”的视频课程,学习到两个重要知识点:实战和自己的库!
  • 本系列历程多源于answer.opencv论坛的一些牛人的解答,作为小白只是代码的搬运工。

言归正传,请看项目要求:

  • 求取找到人眼位置
  • 根据人眼特征确定
  • 简单的小例子,实用价值不大,但是对于其他项目有帮助!
image

思路分析一:

思路分析二:

代码实现:

  1. 去拉普拉斯金字塔,自己手动构造。
  2. 利用眼睛特征进行筛选
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;

int main(int argc, const char** argv)
{
    Mat matFrontFace = imread("abc.png");
    Mat gray; Mat temp;
    double minPixelValue, maxPixelValue;
    Point minPixelPoint, maxPixelPoint;
    cvtColor(matFrontFace, gray, COLOR_BGR2GRAY);
    // Laplacian pyramid
    pyrDown(gray, temp);
    pyrUp(temp, temp);
    temp = gray - temp;
    // 找两个最亮的地方,且距离不能低于一个阈值
    size_t count = 0;
    vector<Point2i> point;
    do
    {
        //寻找最大值
        minMaxLoc(temp, &minPixelValue, &maxPixelValue, &minPixelPoint, &maxPixelPoint);
        if (count == 0)
        {
            point.push_back(maxPixelPoint);
            circle(matFrontFace, maxPixelPoint, 10, Scalar(0, 0, 255), 2);
            temp.at<uchar>(maxPixelPoint.y, maxPixelPoint.x) = 0;
            count++;
        }
        else
        {
            //---两只眼睛高度差别低于5,距离在35-100之内
            float dis = sqrt(pow(maxPixelPoint.x - point[0].x, 2) + pow(maxPixelPoint.y - point[0].y, 2));
            if (dis > 35 && dis<100 && abs(maxPixelPoint.y - point[0].y)<5)
            {
                point.push_back(maxPixelPoint);
                circle(matFrontFace, maxPixelPoint, 10, Scalar(0, 0, 255), 2);
                count++;
            }
            else
            {
                temp.at<uchar>(maxPixelPoint.y, maxPixelPoint.x) = 0;
            }
        }
    } while (count!=2);
    
}
人眼定位结果图 拉普拉斯金字塔

参考资料:

上一篇 下一篇

猜你喜欢

热点阅读