灰度图和彩色图区分 代码方案

原理: 计算每个像素点的三通道差值,所有差值相加,小于 30 即为灰度图,大于30即为彩色图

float caculate_residual_pixel(cv::Mat face_img) {
    //threadhold  建议 30 
    int channels = face_img.channels();
    int rows = face_img.rows;
    int cols = face_img.cols;
    
    if (channels == 3) {
        int ele_cols = cols*channels;
        float sum = 0;
        for (size_t i = 0; i < rows; i++)
        {
            uchar * ptr = face_img.ptr<uchar>(i);
            for (size_t j = 0; j < ele_cols; j += 3)
            {
                sum += abs(ptr[j] - ptr[j + 1]) + abs(ptr[j] - ptr[j + 2]) + abs(ptr[j + 1] - ptr[j + 2]);
            }
        }
        return sum / (rows*cols);
    }
    return -1;
}
void bgr_gray_classify() {
    string gray_path = "E:\\work\\anti_spoofing\\data\\CASIA_ANTI\\data\\data_picture\\anti_print\\gray\\gray\\";
    string bgr_path = "E:\\work\\anti_spoofing\\data\\CASIA_ANTI\\data\\data_picture\\really\\real_faces\\celebra\\celebra1\\";

    string file_path[] = {gray_path, bgr_path};

    glasssix::libfacewrapper face_detect = glasssix::libfacewrapper(3);
    
    vector<vector<float>> value(2);

    for (size_t temp = 0; temp < 2; temp++)
    {
        vector<string> files, file_names;
        util::getFiles(file_path[temp], files, file_names, ".jpg");

        int length = file_names.size();
        cv::Mat img, gray;
        for (size_t i = 0; i < length; i++)
        {
            img = imread(files[i]);
            cvtColor(img, gray, CV_BGR2GRAY);
            vector<Rect> rects;
            int status = getFaceRect(face_detect, gray, rects);
            if (status > 0) {
                value[temp].push_back(caculate_residual_pixel(img(rects[0])));
            }
        }
        float value_mean = 0;
        int value_size = value[temp].size();
        for (size_t i = 0; i < value_size; i++)
        {
            value_mean += value[temp][i];
        }
        value_mean = value_mean / value_size;
        std::cout << " value mean is:" << value_mean << std::endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NineDays66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值