void SkinRGB(IplImage* rgb, IplImage* _dst)
{
assert(rgb->nChannels == && _dst->nChannels == ); static const int R = ;
static const int G = ;
static const int B = ; IplImage* dst = cvCreateImage(cvGetSize(_dst), , );
cvZero(dst); for (int h = ; h<rgb->height; h++)
{
unsigned char* prgb = (unsigned char*)rgb->imageData + h*rgb->widthStep;
unsigned char* pdst = (unsigned char*)dst->imageData + h*dst->widthStep;
for (int w = ; w<rgb->width; w++)
{
if ((prgb[R]> && prgb[G]> && prgb[B]> &&
prgb[R] - prgb[B]> && prgb[R] - prgb[G]>) ||//uniform illumination
(prgb[R]> && prgb[G]> && prgb[B]> &&
abs(prgb[R] - prgb[B]) <= && prgb[R]>prgb[B] && prgb[G]>prgb[B])//lateral illumination
)
{
memcpy(pdst, prgb, );
}
prgb += ;
pdst += ;
}
}
cvCopy(dst, _dst);
cvReleaseImage(&dst);
}
// skin detection in rg space
void cvSkinRG(IplImage* rgb, IplImage* gray)
{
assert(rgb->nChannels == && gray->nChannels == ); const int R = ;
const int G = ;
const int B = ; double Aup = -1.8423;
double Bup = 1.5294;
double Cup = 0.0422;
double Adown = -0.7279;
double Bdown = 0.6066;
double Cdown = 0.1766;
for (int h = ; h<rgb->height; h++)
{
unsigned char* pGray = (unsigned char*)gray->imageData + h*gray->widthStep;
unsigned char* pRGB = (unsigned char*)rgb->imageData + h*rgb->widthStep;
for (int w = ; w<rgb->width; w++)
{
int s = pRGB[R] + pRGB[G] + pRGB[B];
double r = (double)pRGB[R] / s;
double g = (double)pRGB[G] / s;
double Gup = Aup*r*r + Bup*r + Cup;
double Gdown = Adown*r*r + Bdown*r + Cdown;
double Wr = (r - 0.33)*(r - 0.33) + (g - 0.33)*(g - 0.33);
if (g<Gup && g>Gdown && Wr>0.004)
{
*pGray = ;
}
else
{
*pGray = ;
}
pGray++;
pRGB += ;
}
} }
// implementation of otsu algorithm
// author: onezeros#yahoo.cn
// reference: Rafael C. Gonzalez. Digital Image Processing Using MATLAB
void cvThresholdOtsu(IplImage* src, IplImage* dst)
{
int height = src->height;
int width = src->width; //histogram
float histogram[] = { };
for (int i = ; i<height; i++)
{
unsigned char* p = (unsigned char*)src->imageData + src->widthStep*i;
for (int j = ; j<width; j++)
{
histogram[*p++]++;
}
}
//normalize histogram
int size = height*width;
for (int i = ; i<; i++)
{
histogram[i] = histogram[i] / size;
} //average pixel value
float avgValue = ;
for (int i = ; i<; i++)
{
avgValue += i*histogram[i];
} int threshold;
float maxVariance = ;
float w = , u = ;
for (int i = ; i<; i++)
{
w += histogram[i];
u += i*histogram[i]; float t = avgValue*w - u;
float variance = t*t / (w*( - w));
if (variance>maxVariance)
{
maxVariance = variance;
threshold = i;
}
} cvThreshold(src, dst, threshold, , CV_THRESH_BINARY);
} void cvSkinOtsu(IplImage* src, IplImage* dst)
{
assert(dst->nChannels == && src->nChannels == ); IplImage* ycrcb = cvCreateImage(cvGetSize(src), , );
IplImage* cr = cvCreateImage(cvGetSize(src), , );
cvCvtColor(src, ycrcb, CV_BGR2YCrCb);
cvSplit(ycrcb, , cr, , ); cvThresholdOtsu(cr, cr);
cvCopy(cr, dst);
cvReleaseImage(&cr);
cvReleaseImage(&ycrcb);
} void cvSkinYUV(IplImage* src, IplImage* dst)
{
IplImage* ycrcb = cvCreateImage(cvGetSize(src), , );
//IplImage* cr=cvCreateImage(cvGetSize(src),8,1);
//IplImage* cb=cvCreateImage(cvGetSize(src),8,1);
cvCvtColor(src, ycrcb, CV_BGR2YCrCb);
//cvSplit(ycrcb,0,cr,cb,0); static const int Cb = ;
static const int Cr = ;
static const int Y = ; //IplImage* dst=cvCreateImage(cvGetSize(_dst),8,3);
cvZero(dst); for (int h = ; h<src->height; h++)
{
unsigned char* pycrcb = (unsigned char*)ycrcb->imageData + h*ycrcb->widthStep;
unsigned char* psrc = (unsigned char*)src->imageData + h*src->widthStep;
unsigned char* pdst = (unsigned char*)dst->imageData + h*dst->widthStep;
for (int w = ; w<src->width; w++)
{
if (pycrcb[Cr] >= && pycrcb[Cr] <= && pycrcb[Cb] >= && pycrcb[Cb] <= )
{
memcpy(pdst, psrc, );
}
pycrcb += ;
psrc += ;
pdst += ;
}
}
//cvCopyImage(dst,_dst);
//cvReleaseImage(&dst);
} void cvSkinHSV(IplImage* src, IplImage* dst)
{
IplImage* hsv = cvCreateImage(cvGetSize(src), , );
//IplImage* cr=cvCreateImage(cvGetSize(src),8,1);
//IplImage* cb=cvCreateImage(cvGetSize(src),8,1);
cvCvtColor(src, hsv, CV_BGR2HSV);
//cvSplit(ycrcb,0,cr,cb,0); static const int V = ;
static const int S = ;
static const int H = ; //IplImage* dst=cvCreateImage(cvGetSize(_dst),8,3);
cvZero(dst); for (int h = ; h<src->height; h++)
{
unsigned char* phsv = (unsigned char*)hsv->imageData + h*hsv->widthStep;
unsigned char* psrc = (unsigned char*)src->imageData + h*src->widthStep;
unsigned char* pdst = (unsigned char*)dst->imageData + h*dst->widthStep;
for (int w = ; w<src->width; w++)
{
if (phsv[H] >= && phsv[H] <= )
{
memcpy(pdst, psrc, );
}
phsv += ;
psrc += ;
pdst += ;
}
}
//cvCopyImage(dst,_dst);
//cvReleaseImage(&dst);
} int main()
{ //IplImage* img = cvLoadImage("C:\\C_C++ code\\Photo and video\\text009.jpg"); //随便放一张jpg图片在D盘或另行设置目录
//IplImage* dstRGB = cvCreateImage(cvGetSize(img), 8, 3);
//IplImage* dstRG = cvCreateImage(cvGetSize(img), 8, 1); //IplImage* dst_YUV = cvCreateImage(cvGetSize(img), 8, 3);
//IplImage* dst_HSV = cvCreateImage(cvGetSize(img), 8, 3); cvNamedWindow("inputimage", CV_WINDOW_AUTOSIZE);
cvShowImage("inputimage", img);
cvWaitKey();
SkinRGB(img, dstRGB);
cvNamedWindow("SkinRGB", CV_WINDOW_AUTOSIZE);
cvShowImage("SkinRGB", dstRGB);
cvWaitKey();
cvSkinRG(img, dstRG);
cvNamedWindow("cvSkinRG", CV_WINDOW_AUTOSIZE);
cvShowImage("cvSkinRG", dstRG);
cvWaitKey();
cvSkinOtsu(img, dst_crotsu);
cvNamedWindow("cvSkinOtsu", CV_WINDOW_AUTOSIZE);
cvShowImage("cvSkinOtsu", dst_crotsu);
cvWaitKey();
cvSkinYUV(img, dst_YUV);
cvNamedWindow("cvSkinYUV", CV_WINDOW_AUTOSIZE);
cvShowImage("cvSkinYUV", dst_YUV);
cvWaitKey();
cvSkinHSV(img, dst_HSV);
cvNamedWindow("cvSkinHSV", CV_WINDOW_AUTOSIZE);
cvShowImage("cvSkinHSV", dst_HSV);
cvWaitKey(); IplImage *src;
//VideoCapture video("C:\\C_C++ EX8 code\\Video\\MyVideo.wmv");
VideoCapture video();
if (!video.isOpened())
{
return -;
}
cv::Mat img, dstimg;
cv::Rect rect(, , , );
video >> img;
src = &(IplImage(img));
IplImage* dst_crotsu = cvCreateImage(cvGetSize(src), , );
while ()
{
video >> img;
src = &(IplImage(img));
cvSkinOtsu(src, dst_crotsu);
cvNamedWindow("cvSkinOtsu", CV_WINDOW_AUTOSIZE); Mat output = cvarrToMat(dst_crotsu);
cv::erode(output, output, cv::Mat());
cv::erode(output, output, cv::Mat());
cv::erode(output, output, cv::Mat());
cv::erode(output, output, cv::Mat());
cv::dilate(output, output, cv::Mat()); imshow("cvSkinOtsu", output); if (cv::waitKey() > )
{
break;
}
} cvWaitKey();
return ;
}

Opencv SkinOtsu皮肤检测的更多相关文章

  1. OpenCV探索之路(二十七):皮肤检测技术

    好久没写博客了,因为最近都忙着赶项目和打比赛==| 好吧,今天我打算写一篇关于使用opencv做皮肤检测的技术总结.那首先列一些现在主流的皮肤检测的方法都有哪些: RGB color space Yc ...

  2. OpenCV颜色转换和皮肤检测

    本笔记重点记录OpenCV中的颜色转换和利用色彩空间的特性进行皮肤检测 颜色转换 实现原理 之所以要引入色调/饱和度/亮度的色彩空间概念,是因为人们喜欢凭直觉分辨各种颜色,而它与这种方式吻合.实际上, ...

  3. OpenCv皮肤检测-HSV分离

    HSV皮肤检测 // 进行肤色检测 void SkinDetect(IplImage* src, IplImage* dst) { // 创建图像头 IplImage* hsv = cvCreateI ...

  4. OpenCV特征点检测------ORB特征

    OpenCV特征点检测------ORB特征 ORB是是ORiented Brief的简称.ORB的描述在下面文章中: Ethan Rublee and Vincent Rabaud and Kurt ...

  5. OpenCv椭圆皮肤模型

    Mat input_image; Mat output_mask; Mat output_image; void main() { VideoCapture cam(); if (!cam.isOpe ...

  6. opencv车道线检测

    opencv车道线检测 完成的功能 图像裁剪:通过设定图像ROI区域,拷贝图像获得裁剪图像 反透视变换:用的是老师给的视频,没有对应的变换矩阵.所以建立二维坐标,通过四点映射的方法计算矩阵,进行反透视 ...

  7. 【转载】opencv实现人脸检测

    全文转载自CSDN的博客(不知道怎么将CSDN的博客转到博客园,应该没这功能吧,所以直接复制全文了),转载地址如下 http://blog.csdn.net/lsq2902101015/article ...

  8. [PyImageSearch] Ubuntu16.04 使用深度学习和OpenCV实现物体检测

    上一篇博文中讲到如何用OpenCV实现物体分类,但是接下来这篇博文将会告诉你图片中物体的位置具体在哪里. 我们将会知道如何使用OpenCV‘s的dnn模块去加载一个预训练的物体检测网络,它能使得我们将 ...

  9. 【python+opencv】直线检测+圆检测

     Python+OpenCV图像处理—— 直线检测 直线检测理论知识: 1.霍夫变换(Hough Transform) 霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进 ...

随机推荐

  1. ZABBIX冗余架构构筑(Centos6.4+pacemaker+corosync+drbd)

    基本构成: 用pacemaker+corosync控制心跳和资源迁移 用drbd同步zabbix配置文件和mysql数据库 所有软件都用yum安装至默认路径 主机的drbd领域挂载至/drbd,备机不 ...

  2. ecshop目录结构

    ECShop 最新程序 的结构图及各文件相应功能介绍ECShop文件结构目录┣ activity.php 活动列表┣ affiche.php 广告处理文件┣ affiliate.php 生成商品列表┣ ...

  3. firefox 自定义快捷键

    firefox 更新到44或45,发现原来的更改快捷键的扩展没了!!!

  4. 9.8 noip模拟试题

    LazyChild黑OJ(blackoj.pas/c/cpp) LazyChild开了一家“善良OJ”.但大多数人都不知道,这其实是家黑OJ.亲爱的同学,请不要惊讶,古时候有黑店,现代为什么不能有黑O ...

  5. inet address example(socket)

    package com.opensource.socket; import java.net.Inet4Address; import java.net.Inet6Address; import ja ...

  6. 戏说HTML5(转)

    如果有非技术人员问你,HTML5是什么,你会怎么回答? 新的HTML规范... 给浏览器提供了牛逼能力,干以前不能干的事...(确切地说应该是给浏览器规定了许多新的接口标准,要求浏览器实现牛逼的功能. ...

  7. because of many connection errors; unblock with &amp;#39;mysqladmin flush-hosts

    环境:linux,mysql5.5.37 错误:Host is blocked because of many connection errors; unblock with 'mysqladmin ...

  8. 清除代码中的svn文件。

    @echo on color 2f mode con: cols=80 lines=25 @REM @echo 正在清理SVN文件,请稍候...... @rem 循环删除当前目录及子目录下所有的SVN ...

  9. select 标签判断value值相同 就selected高亮

    我写的是里面嵌套php标签 <select name="bq_id[]" multiple="multiple"> <volist name= ...

  10. Perfect Pth Powers pku-1730(筛+合数分解)

    题意:x可以表示为bp, 求这个p的最大值,比如 25=52, 64=26,  然后输入x 输出 p 就是一个质因子分解.算法.(表示数据上卡了2个小时.) 合数质因子分解模板. ]; ]; ; ;n ...