图片处理——基于openCV实现美颜相机

        今天是2017年最后一晚,希望大家元旦前夕玩得开心,准备迎接2018全新的一年,活出程序员的态度。

        最近发现有些女孩在朋友圈发的自拍照肤白貌美,甚至头上魔幻般地长出猫耳朵、猫鼻子、猫胡须,各种调皮搞怪。这一切归功于程序员们不懈努力,推动科技发展,最终科技提高生活品质。美颜相机、美妆相机、秒拍、天天P图们让自拍更加精彩,带有滤镜、美颜、宠萌等各种效果。讲真的,一开始我比较好奇的是宠萌效果,认真分析后总结出实现过程经过三个步骤:人脸检测——>计算放置位置——>绘制宠萌图标。按照国际惯例,先看下图片效果:


        其实,最关键是第一步:人脸检测。这里采用openCV开源库实现(如果不了解openCV这个计算机视觉开源库的,可以去官网学习:https://opencv.org/),先把openCVLibrary集成到项目里,使用训练好的haarcascade模型来初始化Detector,在摄像头每帧预览数据回调时,对图片区域搜索式扫描进行人脸检测。需要注意的是,Android使用的是Bitmap,而openCV使用的是Mat,两者需要进行转换。关键代码如下:

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        mGray = inputFrame.gray();
        // 检测人脸
        Rect[] object = mFaceDetector.detectObject(mGray, mObject);
        if(object != null && object.length > 0){
            //检测到人脸矩形
            Rect rect = object[0];
            //矩形标识
//            Imgproc.rectangle(mRgba, rect.tl(), rect.br(), mFaceDetector.getRectColor(), 3);
            if(beauty != null){
                //添加宠萌妆饰
                addBeauty((int)rect.tl().y, (int)(rect.tl().x+rect.br().x-beauty.cols())/2);
            }
        }
        //拍照一帧数据回调
        if(onPhotoTakenListener != null){
            onPhotoTakenListener.onPhotoTaken(mRgba);
        }
        return mRgba;
    }

        在检测到人脸后,得到人脸在图片的矩形位置,然后计算出宠萌特效放置的位置,y轴坐标为矩形的top,x轴坐标为(矩形left+矩形right-beauty宽度)/2。接着绘制图标:

    /**
     * 添加宠萌效果
     * @param offsetX x坐标偏移量
     * @param offsetY y坐标偏移量
     */
    private void addBeauty(int offsetX, int offsetY){
        offsetX -= 200;//高度校正
        if(offsetX < 0){
            offsetX = 0;
        }
        for (int x=0; x<beauty.rows(); x++){
            for (int y=0; y<beauty.cols(); y++){
                double[] array = beauty.get(x, y);
                if(array[0] != 0) {//过滤全黑像素
                    mRgba.put(x+offsetX, y+offsetY, array);
                }
            }
        }
    }
    /**
     * 获取宠萌妆饰list集合
     */
    private void getBeauty(){
        Drawable drawable1 = getResources().getDrawable(R.drawable.cat, null);
        Bitmap bitmap1 = ((BitmapDrawable) drawable1).getBitmap();
        bitmap1 = Bitmap.createScaledBitmap(bitmap1, 320, 320, true);
        Mat beauty1 = new Mat();
        Utils.bitmapToMat(bitmap1, beauty1);
        beautyList.add(beauty1);
        Drawable drawable2 = getResources().getDrawable(R.drawable.rabbit, null);
        Bitmap bitmap2 = ((BitmapDrawable) drawable2).getBitmap();
        bitmap2 = Bitmap.createScaledBitmap(bitmap2, 320, 320, true);
        Mat beauty2 = new Mat();
        Utils.bitmapToMat(bitmap2, beauty2);
        beautyList.add(beauty2);
    }
        在拍照时,回调数据格式是Mat,需要先转成Bitmap,然后保存图片:
    /**
     * 保存图片
     * @param frameData 帧数据
     */
    private void savePicture(Mat frameData){
        Bitmap bitmap = Bitmap.createBitmap(frameData.width(), frameData.height(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(frameData, bitmap);
        String fileName = PATH + File.separator + dataFormat.format(new Date(System.currentTimeMillis())) + ".jpg";
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(fileName);
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
        如果觉得静态图片的宠萌效果不够酷,那么来看下拍照预览的动态效果(GIF图处理比较模糊,大家勿喷):

        备注:人脸检测部分参考http://blog.csdn.net/q4878802/article/details/51841793

        接下来我会继续研究美颜、滤镜、抠图。欢迎热爱图片处理与多媒体开发的同行朋友相互交流,互相学习。

发布了63 篇原创文章 · 获赞 179 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/u011686167/article/details/78943824