1 /*M///////////////////////////////////////////////////////////////////////////////////////
\r
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
\r
5 // By downloading, copying, installing or using the software you agree to this license.
\r
6 // If you do not agree to this license, do not download, install, copy or use the software.
\r
8 // Copyright (C) 2009, Farhad Dadgostar
\r
9 // Intel Corporation and third party copyrights are property of their respective owners.
\r
11 // Redistribution and use in source and binary forms, with or without modification,
\r
12 // are permitted provided that the following conditions are met:
\r
14 // * Redistribution's of source code must retain the above copyright notice,
\r
15 // this list of conditions and the following disclaimer.
\r
17 // * Redistribution's in binary form must reproduce the above copyright notice,
\r
18 // this list of conditions and the following disclaimer in the documentation
\r
19 // and/or other materials provided with the distribution.
\r
21 // * The name of Intel Corporation may not be used to endorse or promote products
\r
22 // derived from this software without specific prior written permission.
\r
24 // This software is provided by the copyright holders and contributors "as is" and
\r
25 // any express or implied warranties, including, but not limited to, the implied
\r
26 // warranties of merchantability and fitness for a particular purpose are disclaimed.
\r
27 // In no event shall the Intel Corporation or contributors be liable for any direct,
\r
28 // indirect, incidental, special, exemplary, or consequential damages
\r
29 // (including, but not limited to, procurement of substitute goods or services;
\r
30 // loss of use, data, or profits; or business interruption) however caused
\r
31 // and on any theory of liability, whether in contract, strict liability,
\r
32 // or tort (including negligence or otherwise) arising in any way out of
\r
33 // the use of this software, even if advised of the possibility of such damage.
\r
42 #include "opencv2/contrib/contrib.hpp"
\r
43 #include "opencv2/highgui/highgui.hpp"
\r
45 void help(char **argv)
\r
47 std::cout << "\nThis program demonstrates the contributed flesh detector CvAdaptiveSkinDetector which can be found in contrib.cpp\n"
\r
48 << "Usage: " << std::endl <<
\r
49 argv[0] << " fileMask firstFrame lastFrame" << std::endl << std::endl <<
\r
50 "Example: " << std::endl <<
\r
51 argv[0] << " C:\\VideoSequences\\sample1\\right_view\\temp_%05d.jpg 0 1000" << std::endl <<
\r
52 " iterates through temp_00000.jpg to temp_01000.jpg" << std::endl << std::endl <<
\r
53 "If no parameter specified, this application will try to capture from the default Webcam." << std::endl <<
\r
54 "Please note: Background should not contain large surfaces with skin tone." <<
\r
55 "\n\n ESC will stop\n"
\r
56 "Using OpenCV version %s\n" << CV_VERSION << "\n"
\r
60 class ASDFrameHolder
\r
68 virtual ~ASDFrameHolder();
\r
69 virtual void assignFrame(IplImage *sourceImage, double frameTime);
\r
70 inline IplImage *getImage();
\r
71 inline double getTimeStamp();
\r
72 virtual void setImage(IplImage *sourceImage);
\r
75 class ASDFrameSequencer
\r
78 virtual ~ASDFrameSequencer();
\r
79 virtual IplImage *getNextImage();
\r
80 virtual void close();
\r
81 virtual bool isOpen();
\r
82 virtual void getFrameCaption(char *caption);
\r
85 class ASDCVFrameSequencer : public ASDFrameSequencer
\r
91 virtual IplImage *getNextImage();
\r
92 virtual void close();
\r
93 virtual bool isOpen();
\r
96 class ASDFrameSequencerWebCam : public ASDCVFrameSequencer
\r
99 virtual bool open(int cameraIndex);
\r
102 class ASDFrameSequencerVideoFile : public ASDCVFrameSequencer
\r
105 virtual bool open(const char *fileName);
\r
108 class ASDFrameSequencerImageFile : public ASDFrameSequencer {
\r
110 char sFileNameMask[2048];
\r
111 int nCurrentIndex, nStartIndex, nEndIndex;
\r
114 virtual void open(const char *fileNameMask, int startIndex, int endIndex);
\r
115 virtual void getFrameCaption(char *caption);
\r
116 virtual IplImage *getNextImage();
\r
117 virtual void close();
\r
118 virtual bool isOpen();
\r
121 //-------------------- ASDFrameHolder -----------------------//
\r
122 ASDFrameHolder::ASDFrameHolder( )
\r
128 ASDFrameHolder::~ASDFrameHolder( )
\r
130 cvReleaseImage(&image);
\r
133 void ASDFrameHolder::assignFrame(IplImage *sourceImage, double frameTime)
\r
137 cvReleaseImage(&image);
\r
141 image = cvCloneImage(sourceImage);
\r
142 timeStamp = frameTime;
\r
145 IplImage *ASDFrameHolder::getImage()
\r
150 double ASDFrameHolder::getTimeStamp()
\r
155 void ASDFrameHolder::setImage(IplImage *sourceImage)
\r
157 image = sourceImage;
\r
161 //-------------------- ASDFrameSequencer -----------------------//
\r
163 ASDFrameSequencer::~ASDFrameSequencer()
\r
168 IplImage *ASDFrameSequencer::getNextImage()
\r
173 void ASDFrameSequencer::close()
\r
178 bool ASDFrameSequencer::isOpen()
\r
183 void ASDFrameSequencer::getFrameCaption(char* /*caption*/) {
\r
187 IplImage* ASDCVFrameSequencer::getNextImage()
\r
191 image = cvQueryFrame(capture);
\r
195 return cvCloneImage(image);
\r
203 void ASDCVFrameSequencer::close()
\r
205 if (capture != NULL)
\r
207 cvReleaseCapture(&capture);
\r
211 bool ASDCVFrameSequencer::isOpen()
\r
213 return (capture != NULL);
\r
217 //-------------------- ASDFrameSequencerWebCam -----------------------//
\r
219 bool ASDFrameSequencerWebCam::open(int cameraIndex)
\r
223 capture = cvCaptureFromCAM(cameraIndex);
\r
236 //-------------------- ASDFrameSequencerVideoFile -----------------------//
\r
238 bool ASDFrameSequencerVideoFile::open(const char *fileName)
\r
242 capture = cvCaptureFromFile(fileName);
\r
254 //-------------------- ASDFrameSequencerImageFile -----------------------//
\r
256 void ASDFrameSequencerImageFile::open(const char *fileNameMask, int startIndex, int endIndex)
\r
258 nCurrentIndex = startIndex-1;
\r
259 nStartIndex = startIndex;
\r
260 nEndIndex = endIndex;
\r
262 std::sprintf(sFileNameMask, "%s", fileNameMask);
\r
265 void ASDFrameSequencerImageFile::getFrameCaption(char *caption) {
\r
266 std::sprintf(caption, sFileNameMask, nCurrentIndex);
\r
269 IplImage* ASDFrameSequencerImageFile::getNextImage()
\r
271 char fileName[2048];
\r
275 if (nCurrentIndex > nEndIndex)
\r
278 std::sprintf(fileName, sFileNameMask, nCurrentIndex);
\r
280 IplImage* img = cvLoadImage(fileName);
\r
285 void ASDFrameSequencerImageFile::close()
\r
287 nCurrentIndex = nEndIndex+1;
\r
290 bool ASDFrameSequencerImageFile::isOpen()
\r
292 return (nCurrentIndex <= nEndIndex);
\r
295 void putTextWithShadow(IplImage *img, const char *str, CvPoint point, CvFont *font, CvScalar color = CV_RGB(255, 255, 128))
\r
297 cvPutText(img, str, cvPoint(point.x-1,point.y-1), font, CV_RGB(0, 0, 0));
\r
298 cvPutText(img, str, point, font, color);
\r
301 #define ASD_RGB_SET_PIXEL(pointer, r, g, b) { (*pointer) = (unsigned char)b; (*(pointer+1)) = (unsigned char)g; (*(pointer+2)) = (unsigned char)r; }
\r
303 #define ASD_RGB_GET_PIXEL(pointer, r, g, b) {b = (unsigned char)(*(pointer)); g = (unsigned char)(*(pointer+1)); r = (unsigned char)(*(pointer+2));}
\r
305 void displayBuffer(IplImage *rgbDestImage, IplImage *buffer, int rValue, int gValue, int bValue)
\r
307 int x, y, nWidth, nHeight;
\r
308 double destX, destY, dx, dy;
\r
310 unsigned char *pSrc;
\r
312 nWidth = buffer->width;
\r
313 nHeight = buffer->height;
\r
315 dx = double(rgbDestImage->width)/double(nWidth);
\r
316 dy = double(rgbDestImage->height)/double(nHeight);
\r
319 for (x = 0; x < nWidth; x++)
\r
322 for (y = 0; y < nHeight; y++)
\r
324 c = ((uchar*)(buffer->imageData + buffer->widthStep*y))[x];
\r
328 pSrc = (unsigned char *)rgbDestImage->imageData + rgbDestImage->widthStep*int(destY) + (int(destX)*rgbDestImage->nChannels);
\r
329 ASD_RGB_SET_PIXEL(pSrc, rValue, gValue, bValue);
\r
338 int main(int argc, char** argv )
\r
340 IplImage *img, *filterMask = NULL;
\r
341 CvAdaptiveSkinDetector filter(1, CvAdaptiveSkinDetector::MORPHING_METHOD_ERODE_DILATE);
\r
342 ASDFrameSequencer *sequencer;
\r
344 char caption[2048], s[256], windowName[256];
\r
345 long int clockTotal = 0, numFrames = 0;
\r
346 std::clock_t clock;
\r
351 sequencer = new ASDFrameSequencerWebCam();
\r
352 (dynamic_cast<ASDFrameSequencerWebCam*>(sequencer))->open(-1);
\r
354 if (! sequencer->isOpen())
\r
356 std::cout << std::endl << "Error: Cannot initialize the default Webcam" << std::endl << std::endl;
\r
361 sequencer = new ASDFrameSequencerImageFile();
\r
362 (dynamic_cast<ASDFrameSequencerImageFile*>(sequencer))->open(argv[1], std::atoi(argv[2]), std::atoi(argv[3]) ); // A sequence of images captured from video source, is stored here
\r
365 std::sprintf(windowName, "%s", "Adaptive Skin Detection Algorithm for Video Sequences");
\r
367 cvNamedWindow(windowName, CV_WINDOW_AUTOSIZE);
\r
368 cvInitFont( &base_font, CV_FONT_VECTOR0, 0.5, 0.5);
\r
371 // c:\>CvASDSample "C:\VideoSequences\sample1\right_view\temp_%05d.jpg" 0 1000
\r
373 std::cout << "Press ESC to stop." << std::endl << std::endl;
\r
374 while ((img = sequencer->getNextImage()) != 0)
\r
378 if (filterMask == NULL)
\r
380 filterMask = cvCreateImage( cvSize(img->width, img->height), IPL_DEPTH_8U, 1);
\r
382 clock = std::clock();
\r
383 filter.process(img, filterMask); // DETECT SKIN
\r
384 clockTotal += (std::clock() - clock);
\r
386 displayBuffer(img, filterMask, 0, 255, 0);
\r
388 sequencer->getFrameCaption(caption);
\r
389 std::sprintf(s, "%s - %d x %d", caption, img->width, img->height);
\r
390 putTextWithShadow(img, s, cvPoint(10, img->height-35), &base_font);
\r
392 std::sprintf(s, "Average processing time per frame: %5.2fms", (double(clockTotal*1000/CLOCKS_PER_SEC))/numFrames);
\r
393 putTextWithShadow(img, s, cvPoint(10, img->height-15), &base_font);
\r
395 cvShowImage (windowName, img);
\r
396 cvReleaseImage(&img);
\r
398 if (cvWaitKey(1) == 27)
\r
402 sequencer->close();
\r
405 cvReleaseImage(&filterMask);
\r
407 cvDestroyWindow(windowName);
\r
409 std::cout << "Finished, " << numFrames << " frames processed." << std::endl;
\r