滤波函数
中值滤波
medianBlur(frame, detectmat, 5);
平均滤波
blur(frame, detectmat, Size(5, 5));
高斯滤波(最后一个是方差 越大越模糊)
GaussianBlur(frame, detectmat, Size(5, 5),0);
sobel的边缘检测函数
Sobel(gray, dx, CV_16S, 1, 0, 3);//x方向的 因为是1,0
convertScaleAbs(dx, dx);//转换绝对值
Sobel(gray, dy, CV_16SC1, 0, 1, 3);//y方向的 因为0,1
convertScaleAbs(dy, dy);
Sobel(gray, dx_add_dy1, CV_16SC1, 1, 1, 3);//尝试一下1,1 发现并不是简单的都叠加
convertScaleAbs(dx_add_dy1, dx_add_dy1);//效果不好 为啥?
addWeighted(dx, 0.5, dy, 0.5, 0, dx_add_dy2);只能互相单独做 然后再叠加的方式 效果很好
利用高斯滤波对人脸进行平滑处理,学习mask的应用
人脸阈值
//肤色h
double i_minH = 0;
double i_maxH = 20;
//肤色颜色饱和度s
double i_minS = 43;
double i_maxS = 255;
//肤色颜色亮度v
double i_minV = 55;
double i_maxV = 255;
将原图转化为HSV类型,找出人脸并并给maskmat(这里是一个区域,人脸所包含是roi)
cvtColor(frame, hsvMat, COLOR_BGR2HSV);
frame.copyTo(maskMat);
cv::inRange(hsvMat, Scalar(i_minH, i_minS, i_minV), Scalar(i_maxH, i_maxS, i_maxV), maskMat);
对原图进行高斯滤波
GaussianBlur(frame, guss_Mat, Size(5, 5), 3, 0);
将高斯滤波后的人脸区域替换到原图上
frame.copyTo(objectMat);
guss_Mat.copyTo(objectMat, maskMat);
imshow("磨皮后", objectMat);
完整代码:文章来源:https://www.toymoban.com/news/detail-629654.html
#include <opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main() {
VideoCapture cap(0);
while (1) {
Mat frame;
Mat detectmat;
Mat dx;
Mat dy;
Mat dx_add_dy1;
Mat dx_add_dy2;
Mat gray;
cap >> frame;
cvtColor(frame, gray, COLOR_BGR2GRAY);
frame.copyTo(detectmat);
//medianBlur(frame, detectmat, 5);
//blur(frame, detectmat, Size(5, 5));
//GaussianBlur(frame, detectmat, Size(5, 5),0); //最后一个是方差 越大越模糊
Sobel(gray, dx, CV_16S, 1, 0, 3);
convertScaleAbs(dx, dx);
Sobel(gray, dy, CV_16SC1, 0, 1, 3);
convertScaleAbs(dy, dy);
Sobel(gray, dx_add_dy1, CV_16SC1, 1, 1, 3);
convertScaleAbs(dx_add_dy1, dx_add_dy1);//效果不好 为啥?
addWeighted(dx, 0.5, dy, 0.5, 0, dx_add_dy2);
//imshow("frame", frame);
//imshow("detect", detectmat);
imshow("dx", dx);
imshow("dy", dy);
imshow("dx_add_dy1", dx_add_dy1);//效果不好
imshow("dx_add_dy2", dx_add_dy2);
/
Mat hsvMat;
Mat maskMat;
Mat objectMat;
Mat guss_Mat;
//肤色h
double i_minH = 0;
double i_maxH = 20;
//肤色颜色饱和度s
double i_minS = 43;
double i_maxS = 255;
//肤色颜色亮度v
double i_minV = 55;
double i_maxV = 255;
cvtColor(frame, hsvMat, COLOR_BGR2HSV);
frame.copyTo(maskMat);
cv::inRange(hsvMat, Scalar(i_minH, i_minS, i_minV), Scalar(i_maxH, i_maxS, i_maxV), maskMat);
GaussianBlur(frame, guss_Mat, Size(5, 5), 3, 0);
frame.copyTo(objectMat);
guss_Mat.copyTo(objectMat, maskMat);
imshow("磨皮后", objectMat);
waitKey(30);
}
return 0;
}
文章来源地址https://www.toymoban.com/news/detail-629654.html
到了这里,关于OPENCV C++(五)滤波函数+sobel边缘检测+人脸磨皮mask的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!