OpenCv皮肤检测-HSV分离

HSV皮肤检测

// 进行肤色检测
void SkinDetect(IplImage* src, IplImage* dst)
{
// 创建图像头
IplImage* hsv = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, );//用于存图像的一个中间变量,是用来分通道用的,分成hsv通道
IplImage* tmpH1 = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, );//通道的中间变量,用于肤色检测的中间变量
IplImage* tmpS1 = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, );
IplImage* tmpH2 = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, );
IplImage* tmpS3 = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, );
IplImage* tmpH3 = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, );
IplImage* tmpS2 = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, );
IplImage* H = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, );
IplImage* S = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, );
IplImage* V = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, ); IplImage* src_tmp1 = cvCreateImage(cvGetSize(src), , ); // 高斯模糊
cvSmooth(src, src_tmp1, CV_GAUSSIAN, , ); //高斯模糊 // hue色度,saturation饱和度,value纯度
cvCvtColor(src_tmp1, hsv, CV_BGR2HSV);//颜色转换
cvSplit(hsv, H, S, V, );//分为3个通道 cvInRangeS(H, cvScalar(0.0, 0.0, , ), cvScalar(20.0, 0.0, , ), tmpH1);
cvInRangeS(S, cvScalar(75.0, 0.0, , ), cvScalar(200.0, 0.0, , ), tmpS1);
cvAnd(tmpH1, tmpS1, tmpH1, ); // Red Hue with Low Saturation
// Hue 0 to 26 degree and Sat 20 to 90
cvInRangeS(H, cvScalar(0.0, 0.0, , ), cvScalar(13.0, 0.0, , ), tmpH2);
cvInRangeS(S, cvScalar(20.0, 0.0, , ), cvScalar(90.0, 0.0, , ), tmpS2);
cvAnd(tmpH2, tmpS2, tmpH2, ); // Red Hue to Pink with Low Saturation
// Hue 340 to 360 degree and Sat 15 to 90
cvInRangeS(H, cvScalar(170.0, 0.0, , ), cvScalar(180.0, 0.0, , ), tmpH3);
cvInRangeS(S, cvScalar(15.0, 0.0, , ), cvScalar(., 0.0, , ), tmpS3);
cvAnd(tmpH3, tmpS3, tmpH3, ); // Combine the Hue and Sat detections
cvOr(tmpH3, tmpH2, tmpH2, );
cvOr(tmpH1, tmpH2, tmpH1, ); cvCopy(tmpH1, dst); cvReleaseImage(&hsv);
cvReleaseImage(&tmpH1);
cvReleaseImage(&tmpS1);
cvReleaseImage(&tmpH2);
cvReleaseImage(&tmpS2);
cvReleaseImage(&tmpH3);
cvReleaseImage(&tmpS3);
cvReleaseImage(&H);
cvReleaseImage(&S);
cvReleaseImage(&V);
cvReleaseImage(&src_tmp1);
} int main(int argc, char* argv[])
{
/*
IplImage *src = cvLoadImage("C:\\C_C++ code\\Photo and video\\text009.jpg", CV_LOAD_IMAGE_COLOR);
//
cvNamedWindow("src", CV_WINDOW_AUTOSIZE);
cvShowImage("src", src);
// cvNamedWindow("flip", CV_WINDOW_AUTOSIZE);
IplImage *dsc = cvCreateImage(cvGetSize(src), src->depth, 1); // 以x轴翻转
SkinDetect(src, dsc);
// cvFlip(src, dsc, 1);
cvShowImage("flip", dsc);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&src);
printf("Hello World!\n"); */
VideoCapture video();
IplImage *src, *dst;
Mat img,bigimg;
double scalar = ;
video >> img;
src = &(IplImage(img));
dst = cvCreateImage(cvGetSize(src), , );
while ()
{
video >> img;
src = &(IplImage(img));
SkinDetect(src, dst);
img = cvarrToMat(dst);
//腐蚀与膨胀 erode(img, img, Mat());
erode(img, img, Mat());
//erode(img, img, Mat()); dilate(img, img, Mat());
dilate(img, img, Mat());
dilate(img, img, Mat()); dilate(img, img, Mat());
dilate(img, img, Mat());
resize(img, bigimg, Size(img.cols*scalar, img.rows*scalar), , , );
imshow("x", bigimg);
if (waitKey() > )
break;
}
waitKey();
return ;
}
上一篇:SQL SERVER 连接查询(join...on...)


下一篇:linux网络编程九:splice函数,高效的零拷贝