opencv 人脸背景是否亮度过强检测

笔记hsv图判断图像亮度

在人脸检测中,如果人脸背景亮度过强(自然光照或灯光导致),相机无法自适应调节面部亮度,就会导致人脸很暗,影响面部特征的提取.
检测光照强度,判断环境是否符合实验要求.
DetectLandmarkPoints(),人脸68点检测.该接口后续添加.

#include 
using namespace std;
using namespace cv;
int main()
{    
    cv::Mat testframe = imread("backlight.png");
    bool BackLightFlag = false;
    if (testframe.empty())
    {
        cout << "image imread fail;" << endl;
        return 1;
    }
    std::vector<cv::Point2f> landmarks;
    landmarks = DetectLandmarkPoints();//人脸68个特征点检测
    cv::Rect mat_back_light_rect(std::max(float(0), landmarks[0].x - abs(landmarks[0].x - landmarks[16].x)), std::max(float(0), landmarks[19].y - abs(landmarks[19].y - landmarks[33].y) / 2), std::max(float(0), abs(landmarks[0].x - landmarks[16].x)) * 3, std::max(float(0), abs(landmarks[19].y - landmarks[51].y)));;
    if (mat_back_light_rect.x + mat_back_light_rect.width > testframe.cols || mat_back_light_rect.y + mat_back_light_rect.height > testframe.rows)
    {
        cout << "face roi out of image bounds;" << endl;
        return 1;
    }
    cv::Mat mat_back_light_roi = testframe(mat_back_light_rect);
    cv::Mat mat_back_light_roi_hsv;
    cvtColor(mat_back_light_roi, mat_back_light_roi_hsv, cv::COLOR_RGB2HSV);//转换成hsv      
    int count_v = 0;

    for (int i = 0; i < mat_back_light_roi.cols; i++)
    {
        for (int j = 0; j < mat_back_light_roi.rows; j++)
        {
            if ((i< mat_back_light_roi.cols / 3 || i>mat_back_light_roi.cols / 3 * 2) && static_cast<int>(mat_back_light_roi_hsv.at<cv::Vec3b>(j, i)[2]) > 200)//取hsv通道值时记得转换类型
            {
                count_v++;
            }
        }
    }
    if (count_v > mat_back_light_roi.cols * 2 / 3 * mat_back_light_roi.rows / 3)//过亮面积大于总面积的1/3判断为异常
    {
        BackLightFlag = true;
        cout << "BackLight Exception;" << endl;
    }
}

你可能感兴趣的:(人脸背景亮度检测,opencv,hsv,人脸识别)