OpenCv椭圆皮肤模型

时间:2022-11-23 19:22:30
Mat input_image;
Mat output_mask;
Mat output_image; void main()
{
VideoCapture cam();
if (!cam.isOpened())
return; namedWindow("input image");
namedWindow("output mask");
namedWindow("output image");
//椭圆皮肤模型
Mat skinCrCbHist = Mat::zeros(Size(, ), CV_8UC1);
ellipse(skinCrCbHist, Point(, 155.6), Size(23.4, 15.2), 43.0, 0.0, 360.0, Scalar(, , ), -); while (true) {
cam >> input_image;
if (input_image.empty())
return; Mat ycrcb_image;
output_mask = Mat::zeros(input_image.size(), CV_8UC1);
cvtColor(input_image, ycrcb_image, CV_BGR2YCrCb); //首先转换成到YCrCb空间
for (int i = ; i < input_image.cols; i++) //利用椭圆皮肤模型进行皮肤检测
for (int j = ; j < input_image.rows; j++){
Vec3b ycrcb = ycrcb_image.at<Vec3b>(j, i);
if (skinCrCbHist.at<uchar>(ycrcb[], ycrcb[]) > )
output_mask.at<uchar>(j, i) = ;
}
input_image.copyTo(output_image, output_mask);
cv::erode(output_mask, output_mask, cv::Mat());
cv::erode(output_mask, output_mask, cv::Mat());
cv::erode(output_mask, output_mask, cv::Mat());
cv::erode(output_mask, output_mask, cv::Mat());
cv::dilate(output_mask, output_mask, cv::Mat());
imshow("input image", input_image);
imshow("output mask", output_mask);
imshow("output image", output_image);
output_image.setTo();
if ( == waitKey())
return;
}
return;
}

第二种:

Mat input_image;
Mat output_mask;
Mat output_image;
Mat mask;
void main()
{
VideoCapture cam();
if (!cam.isOpened())
return; ///椭圆皮肤模型
Mat skinCrCbHist = Mat::zeros(Size(, ), CV_8UC1);
ellipse(skinCrCbHist, Point(, 155.6), Size(23.4, 15.2), 43.0, 0.0, 360.0, Scalar(, , ), -); Mat element = getStructuringElement(MORPH_RECT, Size(, ), Point(-, -)); while (true)
{
cam >> input_image;
if (input_image.empty())
return; Mat ycrcb_image;
output_mask = Mat::zeros(input_image.size(), CV_8UC1);
cvtColor(input_image, ycrcb_image, CV_BGR2YCrCb); //首先转换成到YCrCb空间 for (int i = ; i < input_image.rows; i++) //利用椭圆皮肤模型进行皮肤检测
{
uchar* p = (uchar*)output_mask.ptr<uchar>(i);
Vec3b* ycrcb = (Vec3b*)ycrcb_image.ptr<Vec3b>(i);
for (int j = ; j < input_image.cols; j++)
{
if (skinCrCbHist.at<uchar>(ycrcb[j][], ycrcb[j][]) > )
p[j] = ;
}
} morphologyEx(output_mask, output_mask, MORPH_CLOSE, element); vector< vector<Point> > contours; // 轮廓
vector< vector<Point> > filterContours; // 筛选后的轮廓
vector< Vec4i > hierarchy; // 轮廓的结构信息
contours.clear();
hierarchy.clear();
filterContours.clear(); findContours(output_mask, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
// 去除伪轮廓
for (size_t i = ; i < contours.size(); i++)
{
if (fabs(contourArea(Mat(contours[i]))) > &&fabs(arcLength(Mat(contours[i]),true))<) //判断手进入区域的阈值
filterContours.push_back(contours[i]);
} output_mask.setTo();
drawContours(output_mask, filterContours, -, Scalar(, , ), CV_FILLED); //8, hierarchy); input_image.copyTo(output_image, output_mask);
imshow("input image", input_image);
imshow("output image", output_image); output_image.setTo();
if ( == waitKey())
return;
}
return;
}