智能证件照制作——基于人脸检测与自动人像分割轻松制作个人证件照(C++实现)

本文介绍了一个基于SCRFD人脸检测及图像处理技术的自动化证件照生成系统。该系统使用ncnn作为移动端推理加速库,实现了从人脸检测、图像裁剪到背景替换等功能。支持多种平台,包括Windows、Android和iOS。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

1.关于证件照,有好多种制作办法,最常见的是使用PS来做图像处理,或者下载各种证件照相关的APP,一键制作,基本的步骤是先按人脸为基准切出适合的尺寸,然后把人像给抠出来,对人像进行美化处理,然后替换上要使用的背景色,比如蓝色或红色。
2.我这里也按着上面的步骤来用代码实现,先是人脸检测,剪切照片,替换背景色,美化和修脸暂时还没有时间写完。
3.因为是考虑到要移植到移动端(安卓和iOS),这里使用了ncnn做推理加速库,之前做过一些APP,加速库都选了ncnn,不管在安卓或者iOS上,性能都是不错的。
4.我的开发环境是win10, vs2019, opencv4.5, ncnn,如果要启用GPU加速,所以用到VulkanSDK,实现语言是C++。
5.先上效果图,对于背景纯度的要求不高,如果使用场景背景复杂的话,也可以完美抠图。
原始图像:
在这里插入图片描述
在这里插入图片描述
原图:
在这里插入图片描述
自动剪切出来的证件照:
在这里插入图片描述

原图:
在这里插入图片描述

自动剪切出来的证件照:
在这里插入图片描述

一.项目创建

1.使用vs2019新建一个C++项目,把OpenC和NCNN库导入,NCNN可以下载官方编译好的库,我也会在后面上传我使用的库和源码以及用到的模型。
2.如果要启用GPU推理,就要安装VulkanSDK,安装的步骤可以参考我之前的博客。

二.人脸检测

1.人脸检测这里面使用 SCRFD ,它带眼睛,鼻子,嘴角五个关键点的坐标,这个可以用做证件照参考点,人脸检测库这个也可以用libfacedetection,效果都差不多,如果是移动端最好选择SCRFD。

代码实现:
推理代码

#include "scrfd.h"

#include <string.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <ncnn/cpu.h> //安卓才用到

static inline float intersection_area(const FaceObject& a, const FaceObject& b)
{
   
    cv::Rect_<float> inter = a.rect & b.rect;
    return inter.area();
}

static void qsort_descent_inplace(std::vector<FaceObject>& faceobjects, int left, int right)
{
   
    int i = left;
    int j = right;
    float p = faceobjects[(left + right) / 2].prob;

    while (i <= j)
    {
   
        while (faceobjects[i].prob > p)
            i++;

        while (faceobjects[j].prob < p)
            j--;

        if (i <= j)
        {
   
            // swap
            std::swap(faceobjects[i], faceobjects[j]);

            i++;
            j--;
        }
    }

//     #pragma omp parallel sections
    {
   
//         #pragma omp section
        {
   
            if (left < j) qsort_descent_inplace(faceobjects, left, j);
        }
//         #pragma omp section
        {
   
            if (i < right) qsort_descent_inplace(faceobjects, i, right);
        }
    }
}

static void qsort_descent_inplace(std::vector<FaceObject>& faceobjects)
{
   
    if (faceobjects.empty())
        return;

    qsort_descent_inplace(faceobjects, 0, faceobjects.size() - 1);
}

static void nms_sorted_bboxes(const std::vector<FaceObject>& faceobjects, std::vector<int>& picked, float nms_threshold)
{
   
    picked.clear();

    const int n = faceobjects.size();

    std::vector<float> areas(n);
    for (int i = 0; i < n; i++)
    {
   
        areas[i] = faceobjects[i].rect.area();
    }

    for (int i = 0; i < n; i++)
    {
   
        const FaceObject& a = faceobjects[i];

        int keep = 1;
        for (int j = 0; j < (int)picked.size(); j++)
        {
   
            const FaceObject& b = faceobjects[picked[j]];

            // intersection over union
            float inter_area = intersection_area(a, b);
            float union_area = areas[i] + areas[picked[j]] - inter_area;
            //             float IoU = inter_area / union_area
            if (inter_area / union_area > nms_threshold)
                keep = 0;
        }

        if (keep)
            picked.push_back(i);
    }
}

static ncnn::Mat generate_anchors(int base_size, const ncnn::Mat& ratios, const ncnn::Mat& scales)
{
   
    int num_ratio = ratios.w;
    int num_scale = scales.w;

    ncnn::Mat anchors;
    anchors.create(4, num_ratio * num_scale);

    const float cx = 0;
    const float cy = 0;

    for (int i = 0; i < num_ratio; i++)
    {
   
        float ar = ratios[i];

        int r_w = round(base_size / sqrt(ar));
        int r_h = round(r_w * ar); //round(base_size * sqrt(ar));

        for (int j = 0; j < num_scale; j++)
        {
   
            float scale = scales[j];

            float rs_w = r_w * scale;
            float rs_h = r_h * scale;

            float* anchor = anchors.row(i * num_scale + j);

            anchor[0] = cx - rs_w * 0.5f;
            anchor[1] = cy - rs_h * 0.5f;
            anchor[2] = cx + rs_w * 0.5f;
            anchor[3] = cy + rs_h * 0.5f;
        }
    }

    return anchors;
}

static void generate_proposals(const ncnn::Mat& anchors, int feat_stride, const ncnn::Mat& score_blob, const ncnn::Mat& bbox_blob, const ncnn::Mat& kps_blob, float prob_threshold, std::vector<FaceObject>& faceobjects)
{
   
    int w = score_blob.w;
    int h = score_blob.h;

    // generate face proposal from bbox deltas and shifted anchors
    const int num_anchors = anchors.h;

    for (int q = 0; q < num_anchors; q++)
    {
   
        const float* anchor = anchors.row(q);

        const ncnn::Mat score = score_blob.channel(q);
        const ncnn::Mat bbox = bbox_blob.channel_range(q * 4, 4);

        // shifted anchor
        float anchor_y = anchor[1];

        float anchor_w = anchor[2] - anchor[0];
        float anchor_h = anchor[3] - anchor[1];

        for (int i = 0; i < h; i++)
        {
   
            float anchor_x = anchor[0];

            for (int j = 0; j < w; j++)
            {
   
                int index = i * w + j;

                float prob = score[index];

                
OpenCV是一个广泛用于计算机视觉任务的强大库,它在Python中尤其受欢迎。要利用OpenCV制作智能证件照,可以按照以下步骤进行: 1. **人脸检测**:首先,你需要安装如`face_recognition`或`dlib`这样的库来进行人脸检测。它们包含了预训练的人脸检测模型,例如Haar cascades分类器或者MTCNN(Multi-Task Cascaded Convolutional Networks),可以在图片或视频帧中找到人脸位置。 ```python import cv2 import face_recognition # 加载图像 img = cv2.imread('input.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_recognition.face_locations(gray) ``` 2. **自动人像分割**:定位到人脸后,可以使用OpenCV的边缘检测算法(如Canny或GrabCut)将背景和人脸分离出来。对于复杂的背景去除,可以尝试使用更高级的技术,如基于深度学习的实例分割模型(比如Mask R-CNN)。 3. **调整尺寸**:创建模板尺寸,通常证件照需要固定的大小和比例,例如35mm*45mm。然后,你可以使用`cv2.resize()`函数对人脸裁剪并调整至适当大小。 4. **添加边框和背景**:确定好证件照的基本布局后,可以添加边框、背景颜色以及必要的文字信息(如姓名、性别等)。 ```python # 创建白色背景 blank = np.zeros((height, width, 3), dtype="uint8") # 添加边框和文字 border_color = (255, 255, 255) cv2.rectangle(blank, ... , border_color) cv2.putText(blank, "姓名", ..., fontFace=cv2.FONT_HERSHEY_SIMPLEX, color=(0, 0, 0), thickness=2) # 合并前景和背景 output = cv2.bitwise_and(blank, blank, mask=mask) result = cv2.add(output, img) ``` 5. **保存结果**:最后,使用`cv2.imwrite()`保存处理后的证件照片。 ```python cv2.imwrite("output.jpg", result) ```
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

知来者逆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值