2012年5月15日 星期二

Skin Detection

#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <cvcam.h>
int main()
{
 CvCapture * cap=cvCreateCameraCapture(0);
 IplImage * img=cvQueryFrame(cap);
 IplImage * img1=cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3);;
 IplImage * grey=cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
 cvNamedWindow("camera", 0);
 cvResizeWindow("camera", 320, 240);
 cvNamedWindow("skin", 0);
 cvResizeWindow("skin", 320, 240);
 int sx, sy;
 CvPoint FromPoint, ToPoint;
    CvScalar  Color;
   
    Color = CV_RGB(255,0,0);
 while(1){
  img=cvQueryFrame(cap);
  cvFlip(img, 0, 1);
  img1=cvCloneImage(img);
 
  cvCvtColor(img, img, CV_BGR2YCrCb);
  cvInRangeS(img, cvScalar(0,137,77), cvScalar(256,177,127),grey);
        

  
        cvErode(grey, grey,0, 5 );
        cvDilate(grey, grey,0, 5 );
       
     //cvFlip(grey, 0, 0);  
       
       //for (int y=1; y<grey->height-1; y++) {
  for (int y=grey->height-1; y>1; y--) { 
            uchar* ptr1=(uchar*) (grey->imageData +y*grey->widthStep);
            for (int x=1; x<grey->width-1; x++) {    
                 if (ptr1[x-1]==255 && ptr1[x]==255 && ptr1[x+1]==255) {
                     sx=x; sy=y;
                     //x=grey->width+1; y=grey->height+1;
                     x=grey->width+1; y=0;
                     }  
                 }
        }
       
        printf("%d %d\n", sx, sy);
       
       // sx=50; sy=50;
     
        FromPoint=cvPoint(sx-10,sy);
        ToPoint=cvPoint(sx+10,sy);
        cvLine(img1, FromPoint, ToPoint, Color, 3, CV_AA, 0);
   
   
        FromPoint=cvPoint(sx,sy-10);
        ToPoint=cvPoint(sx,sy+10);
        cvLine(img1, FromPoint, ToPoint, Color, 3, CV_AA, 0);
       
      
        cvShowImage("camera", img1);
       
     
        cvShowImage("skin", grey);
     
  cvWaitKey(33);
 }
 return 0;
}

沒有留言:

張貼留言