Monochrome background Cutout method (green background cutout, skin color detection)--chroma Kye

Source: Internet
Author: User

First of all, my blog is based on the research of others ' blogs, creating my own project to practice

1, Skin color detection method (each method provides full source download link)

Download Link: Click Open link http://download.csdn.net/detail/chenxun2009/7731489

Skin tone extraction is a common method based on human-computer interaction. Because skin color is a major feature of the human body, it can quickly isolate its own feature areas from complex backgrounds. Introduce two common skin tone extracts:

(1) Skin tone extraction in HSV space

The HSV color space is a conical model, as shown in the figure on the right:

Hue (H) is the basic attribute of a color, which is the name of a color, such as red, yellow, etc.


According to the position of the standard color wheel on the right figure, take 360 degree value. (There are also 0~100% methods to determine) saturation (S) is the purity of the color, the higher the color purer, the lower the gray. The value is 0~100%. Lightness (V) is also called brightness, and the value is 0~100.
Based on the color of the HSV three component values, you can easily detect a picture of the color of the part. Here is the source code for the skin detection function:

void Skindetectionhsv (iplimage* pimage,int lower,int upper,iplimage* process) {iplimage* PIMAGEHSV = NULL;  
	iplimage* Pimageh = NULL;  
	iplimage* pimages = NULL;  
	iplimage* pimageprocessed = NULL;  
	iplimage* TmpH = NULL;  
	iplimage* tmps = NULL;  

	static iplimage* pyrimage = NULL;  
	Cvsize imgsize;  
	Imgsize.height = pimage->height;  

	Imgsize.width = Pimage->width;  
	Create want to use image and give them memory allocation PIMAGEHSV = Cvcreateimage (imgsize,ipl_depth_8u,3);  
	Pimageh = Cvcreateimage (imgsize,ipl_depth_8u,1);  
	Pimages = Cvcreateimage (imgsize,ipl_depth_8u,1);  
	Tmps = Cvcreateimage (imgsize,ipl_depth_8u,1);  
	TmpH = Cvcreateimage (imgsize,ipl_depth_8u,1);  
	pimageprocessed = Cvcreateimage (imgsize,ipl_depth_8u,1);  

	Pyrimage = Cvcreateimage (Cvsize (PIMAGE->WIDTH/2,PIMAGE->HEIGHT/2), ipl_depth_8u,1);  

	Convert RGB image to HSV image Cvcvtcolor (PIMAGE,PIMAGEHSV,CV_BGR2HSV); Then split HSV-three single channel imageS Cvcvtpixtoplane (pimagehsv,pimageh,pimages,null,null); The skin scalar range in H and S, do they and algorithm cvinranges (Pimageh,cvscalar (0.0,0.0,0,0), Cvscalar (lower,0.0,0  
	, 0), TmpH);  
	Cvinranges (Pimages,cvscalar (26,0.0,0,0), Cvscalar (upper,0.0,0,0), tmps);  
	Cvand (tmph,tmps,pimageprocessed,0);  
	Cvpyrdown (pimageprocessed,pyrimage,cv_gaussian_5x5);  
	Cvpyrup (pyrimage,pimageprocessed,cv_gaussian_5x5);  
	Erode and dilate Cverode (pimageprocessed,pimageprocessed,0,2);  

	Cvdilate (pimageprocessed,pimageprocessed,0,1);  
	Cvcopy (pimageprocessed,process,0);  
	Do clean cvreleaseimage (&pyrimage);  
	Cvreleaseimage (&PIMAGEHSV);  
	Cvreleaseimage (&pimageh);  
	Cvreleaseimage (&pimages);  
	Cvreleaseimage (&pyrimage);  
	Cvreleaseimage (&tmph);  
	Cvreleaseimage (&tmps);  
Cvreleaseimage (&pimageprocessed); }

(2) Skin tone extraction in YCRCB space

Full project Download: http://download.csdn.net/detail/chenxun2009/7731641
YCRCB is also a color space, can also be said to be YUV color space. Y is the component of brightness, and the color detection is more sensitive to brightness, the RGB image taken by the camera into the YCRCB space can be removed from the brightness of the effect of color detection. The following is a source code based on the YCRCB skin detection function:

  VOID&NBSP;SKINDETECTIONYCRCB (iplimage* imagergb,int lower,int upper,iplimage*  imgprocessed)    {              assert ( imagergb->nchannels==3);       IplImage* imageYCrCb = NULL;        IplImage* imageCb = NULL;        imageycrcb = cvcreateimage (Cvgetsize (IMAGERGB), 8,3);        Imagecb = cvcreateimage (Cvgetsize (IMAGERGB), 8,1);           Cvcvtcolor (IMAGERGB,IMAGEYCRCB,CV_BGR2YCRCB);       cvsplit (imageYCrCb,0,0, imagecb,0);//cb       for  (int h=0;h<imagecb->height;h++)             {            for  (INT&NBSP;W=0;W&Lt;imagecb->width;w++)                      {                unsigned char* p = (unsigned char*) (IMAGECB-&GT;IMAGEDATA+H*IMAGECB-&GT;WIDTHSTEP+W);                if  (*p<=upper& &*p>=lower)                            {                    *p=255;                }                            else                            {                    *p=0;                }           }       }        cvcopy (imagecb,imgprocessed,null);  }  

2, based on mixed Gaussian model removal background method

Gaussian model removal Background is also a common method of background removal, which is often used in video image detection. This method is suitable for dynamic video image feature detection because the foreground and background are separated from the model. The benchmark for separating foreground and background is to judge the rate of pixel change, which will be the background of slow learning, and the change is regarded as the foreground.
#include "stdafx.h" #include "cv.h" #include "highgui.h" #include "cxtypes.h" #include "cvaux.h" # Includ         e <iostream> using namespace std;   int _tmain (int argc, _tchar* argv[]) {//iplimage* pfirstframe = NULL;       iplimage* pframe = NULL;       iplimage* pfrimg = NULL;       iplimage* pbkimg = NULL; iplimage* firstimg = NULL;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.