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
43 #include <highgui.h>
\r
45 class ASDFrameHolder
\r
53 virtual ~ASDFrameHolder();
\r
54 virtual void assignFrame(IplImage *sourceImage, double frameTime);
\r
55 inline IplImage *getImage();
\r
56 inline double getTimeStamp();
\r
57 virtual void setImage(IplImage *sourceImage);
\r
60 class ASDFrameSequencer
\r
63 virtual ~ASDFrameSequencer();
\r
64 virtual IplImage *getNextImage();
\r
65 virtual void close();
\r
66 virtual bool isOpen();
\r
67 virtual void getFrameCaption(char *caption);
\r
70 class ASDCVFrameSequencer : public ASDFrameSequencer
\r
76 virtual IplImage *getNextImage();
\r
77 virtual void close();
\r
78 virtual bool isOpen();
\r
81 class ASDFrameSequencerWebCam : public ASDCVFrameSequencer
\r
84 virtual bool open(int cameraIndex);
\r
87 class ASDFrameSequencerVideoFile : public ASDCVFrameSequencer
\r
90 virtual bool open(const char *fileName);
\r
93 class ASDFrameSequencerImageFile : public ASDFrameSequencer {
\r
95 char sFileNameMask[2048];
\r
96 int nCurrentIndex, nStartIndex, nEndIndex;
\r
99 virtual void open(const char *fileNameMask, int startIndex, int endIndex);
\r
100 virtual void getFrameCaption(char *caption);
\r
101 virtual IplImage *getNextImage();
\r
102 virtual void close();
\r
103 virtual bool isOpen();
\r
106 //-------------------- ASDFrameHolder -----------------------//
\r
107 ASDFrameHolder::ASDFrameHolder( )
\r
113 ASDFrameHolder::~ASDFrameHolder( )
\r
115 cvReleaseImage(&image);
\r
118 void ASDFrameHolder::assignFrame(IplImage *sourceImage, double frameTime)
\r
122 cvReleaseImage(&image);
\r
126 image = cvCloneImage(sourceImage);
\r
127 timeStamp = frameTime;
\r
130 IplImage *ASDFrameHolder::getImage()
\r
135 double ASDFrameHolder::getTimeStamp()
\r
140 void ASDFrameHolder::setImage(IplImage *sourceImage)
\r
142 image = sourceImage;
\r
146 //-------------------- ASDFrameSequencer -----------------------//
\r
148 ASDFrameSequencer::~ASDFrameSequencer()
\r
153 IplImage *ASDFrameSequencer::getNextImage()
\r
158 void ASDFrameSequencer::close()
\r
163 bool ASDFrameSequencer::isOpen()
\r
168 void ASDFrameSequencer::getFrameCaption(char *caption) {
\r
172 IplImage* ASDCVFrameSequencer::getNextImage()
\r
176 image = cvQueryFrame(capture);
\r
180 return cvCloneImage(image);
\r
188 void ASDCVFrameSequencer::close()
\r
190 if (capture != NULL)
\r
192 cvReleaseCapture(&capture);
\r
196 bool ASDCVFrameSequencer::isOpen()
\r
198 return (capture != NULL);
\r
202 //-------------------- ASDFrameSequencerWebCam -----------------------//
\r
204 bool ASDFrameSequencerWebCam::open(int cameraIndex)
\r
208 capture = cvCaptureFromCAM(cameraIndex);
\r
221 //-------------------- ASDFrameSequencerVideoFile -----------------------//
\r
223 bool ASDFrameSequencerVideoFile::open(const char *fileName)
\r
227 capture = cvCaptureFromFile(fileName);
\r
239 //-------------------- ASDFrameSequencerImageFile -----------------------//
\r
241 void ASDFrameSequencerImageFile::open(const char *fileNameMask, int startIndex, int endIndex)
\r
243 nCurrentIndex = startIndex-1;
\r
244 nStartIndex = startIndex;
\r
245 nEndIndex = endIndex;
\r
247 std::sprintf(sFileNameMask, "%s", fileNameMask);
\r
250 void ASDFrameSequencerImageFile::getFrameCaption(char *caption) {
\r
251 std::sprintf(caption, sFileNameMask, nCurrentIndex);
\r
254 IplImage* ASDFrameSequencerImageFile::getNextImage()
\r
256 char fileName[2048];
\r
260 if (nCurrentIndex > nEndIndex)
\r
263 std::sprintf(fileName, sFileNameMask, nCurrentIndex);
\r
265 IplImage* img = cvLoadImage(fileName);
\r
270 void ASDFrameSequencerImageFile::close()
\r
272 nCurrentIndex = nEndIndex+1;
\r
275 bool ASDFrameSequencerImageFile::isOpen()
\r
277 return (nCurrentIndex <= nEndIndex);
\r
280 void putTextWithShadow(IplImage *img, const char *str, CvPoint point, CvFont *font, CvScalar color = CV_RGB(255, 255, 128))
\r
282 cvPutText(img, str, cvPoint(point.x-1,point.y-1), font, CV_RGB(0, 0, 0));
\r
283 cvPutText(img, str, point, font, color);
\r
286 #define ASD_RGB_SET_PIXEL(pointer, r, g, b) { (*pointer) = (unsigned char)b; (*(pointer+1)) = (unsigned char)g; (*(pointer+2)) = (unsigned char)r; }
\r
288 #define ASD_RGB_GET_PIXEL(pointer, r, g, b) {b = (unsigned char)(*(pointer)); g = (unsigned char)(*(pointer+1)); r = (unsigned char)(*(pointer+2));}
\r
290 void displayBuffer(IplImage *rgbDestImage, IplImage *buffer, int rValue, int gValue, int bValue)
\r
292 int x, y, nWidth, nHeight;
\r
293 double destX, destY, dx, dy;
\r
295 unsigned char *pSrc;
\r
297 nWidth = buffer->width;
\r
298 nHeight = buffer->height;
\r
300 dx = double(rgbDestImage->width)/double(nWidth);
\r
301 dy = double(rgbDestImage->height)/double(nHeight);
\r
304 for (x = 0; x < nWidth; x++)
\r
307 for (y = 0; y < nHeight; y++)
\r
309 c = ((uchar*)(buffer->imageData + buffer->widthStep*y))[x];
\r
313 pSrc = (unsigned char *)rgbDestImage->imageData + rgbDestImage->widthStep*int(destY) + (int(destX)*rgbDestImage->nChannels);
\r
314 ASD_RGB_SET_PIXEL(pSrc, rValue, gValue, bValue);
\r
323 int main(int argc, char** argv )
\r
325 IplImage *img, *filterMask = NULL;
\r
326 CvAdaptiveSkinDetector filter(1, CvAdaptiveSkinDetector::MORPHING_METHOD_ERODE_DILATE);
\r
327 ASDFrameSequencer *sequencer;
\r
329 char caption[2048], s[256], windowName[256];
\r
330 long int clockTotal = 0, numFrames = 0;
\r
331 std::clock_t clock;
\r
335 std::cout << "Usage: " << std::endl <<
\r
336 argv[0] << " fileMask firstFrame lastFrame" << std::endl << std::endl <<
\r
337 "Example: " << std::endl <<
\r
338 argv[0] << " C:\\VideoSequences\\sample1\\right_view\\temp_%05d.jpg 0 1000" << std::endl <<
\r
339 " iterates through temp_00000.jpg to temp_01000.jpg" << std::endl << std::endl <<
\r
340 "If no parameter specified, this application will try to capture from the default Webcam." << std::endl <<
\r
341 "Please note: Background should not contain large surfaces with skin tone." <<
\r
344 sequencer = new ASDFrameSequencerWebCam();
\r
345 (dynamic_cast<ASDFrameSequencerWebCam*>(sequencer))->open(-1);
\r
347 if (! sequencer->isOpen())
\r
349 std::cout << std::endl << "Error: Cannot initialize the default Webcam" << std::endl << std::endl;
\r
354 sequencer = new ASDFrameSequencerImageFile();
\r
355 (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
358 std::sprintf(windowName, "%s", "Adaptive Skin Detection Algorithm for Video Sequences");
\r
360 cvNamedWindow(windowName, CV_WINDOW_AUTOSIZE);
\r
361 cvInitFont( &base_font, CV_FONT_VECTOR0, 0.5, 0.5);
\r
364 // c:\>CvASDSample "C:\VideoSequences\sample1\right_view\temp_%05d.jpg" 0 1000
\r
366 std::cout << "Press ESC to stop." << std::endl << std::endl;
\r
367 while ((img = sequencer->getNextImage()) != 0)
\r
371 if (filterMask == NULL)
\r
373 filterMask = cvCreateImage( cvSize(img->width, img->height), IPL_DEPTH_8U, 1);
\r
375 clock = std::clock();
\r
376 filter.process(img, filterMask); // process the frame
\r
377 clockTotal += (std::clock() - clock);
\r
379 displayBuffer(img, filterMask, 0, 255, 0);
\r
381 sequencer->getFrameCaption(caption);
\r
382 std::sprintf(s, "%s - %d x %d", caption, img->width, img->height);
\r
383 putTextWithShadow(img, s, cvPoint(10, img->height-35), &base_font);
\r
385 std::sprintf(s, "Average processing time per frame: %5.2fms", (double(clockTotal*1000/CLOCKS_PER_SEC))/numFrames);
\r
386 putTextWithShadow(img, s, cvPoint(10, img->height-15), &base_font);
\r
388 cvShowImage (windowName, img);
\r
389 cvReleaseImage(&img);
\r
391 if (cvWaitKey(1) == 27)
\r
395 sequencer->close();
\r
398 cvReleaseImage(&filterMask);
\r
400 cvDestroyWindow(windowName);
\r
402 std::cout << "Finished, " << numFrames << " frames processed." << std::endl;
\r