#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
using namespace std;
using namespace cv;
class ASDCVFrameSequencer
{
protected: //parent Variable
CvCapture *capture;
public:
void getFrameCaption(char *caption);
virtual IplImage *getNextImage();
virtual void close();
virtual bool isOpen();
};
//open camera
class ASDFrameSequencerWebCam : public ASDCVFrameSequencer
{
public:
virtual bool open(int cameraIndex);
};
//open video file
class ASDFrameSequencerVideoFile : public ASDCVFrameSequencer
{
public:
virtual bool open(const char *fileName);
};
//read sequencer image
class ASDFrameSequencerImageFile : public ASDCVFrameSequencer
{
private:
char sFileNameMask[2048];
int nCurrentIndex, nStartIndex, nEndIndex;
public:
virtual void open(const char *fileNameMask, int startIndex, int endIndex);
virtual void getFrameCaption(char *caption);
virtual IplImage *getNextImage();
virtual void close();
virtual bool isOpen();
};
//-------------------- ASDCVFrameSequencer -----------------------//
//camera video all same methods
bool ASDCVFrameSequencer::isOpen() //did camera open
{
return (capture != NULL);
};
IplImage* ASDCVFrameSequencer::getNextImage()
{
IplImage *image;
image = cvQueryFrame(capture);
if (image != NULL)
{
return cvCloneImage(image);
}
else
{
return NULL;
}
};
void ASDCVFrameSequencer::close() //close camera
{
if (capture != NULL)
{
cvReleaseCapture(&capture);
}
};
void ASDCVFrameSequencer::getFrameCaption(char *caption) {
return;
};
//-------------------- ASDFrameSequencerWebCam -----------------------//
//read camera
bool ASDFrameSequencerWebCam::open(int cameraIndex)
{
capture = cvCaptureFromCAM(cameraIndex);
if (!capture)
{
return false;
}
else
{
return true;
}
};
//-------------------- ASDFrameSequencerVideoFile -----------------------//
//read video file
bool ASDFrameSequencerVideoFile::open(const char *fileName)
{
capture = cvCaptureFromFile(fileName);
if (!capture)
{
return false;
}
else
{
return true;
}
};
//-------------------- ASDFrameSequencerImageFile -----------------------//
// read sequencer image
void ASDFrameSequencerImageFile::open(const char *fileNameMask, int startIndex, int endIndex)
{
nCurrentIndex = startIndex-1;
nStartIndex = startIndex;
nEndIndex = endIndex;
std::sprintf(sFileNameMask, "%s", fileNameMask);
};
void ASDFrameSequencerImageFile::getFrameCaption(char *caption) {
std::sprintf(caption, sFileNameMask, nCurrentIndex);
};
IplImage* ASDFrameSequencerImageFile::getNextImage()
{
char fileName[2048];
nCurrentIndex++;
if (nCurrentIndex > nEndIndex)
return NULL;
std::sprintf(fileName, sFileNameMask, nCurrentIndex);
IplImage* img = cvLoadImage(fileName);
return img;
};
void ASDFrameSequencerImageFile::close()
{
nCurrentIndex = nEndIndex+1;
};
bool ASDFrameSequencerImageFile::isOpen()
{
return (nCurrentIndex <= nEndIndex);
};
/********************************************/
//put font information
//CvFont base_font;
//cvInitFont( &base_font, CV_FONT_VECTOR0, 0.5, 0.5);
static void putTextWithShadow(IplImage *img, const char *str, CvPoint point, CvFont *font, CvScalar color = CV_RGB(255, 255, 128))
{
cvPutText(img, str, cvPoint(point.x-1,point.y-1), font, CV_RGB(0, 0, 0));
cvPutText(img, str, point, font, color);
};
/*************************************/
//have one point data
#define ASD_RGB_SET_PIXEL(pointer, r, g, b) { (*pointer) = (unsigned char)b; (*(pointer+1)) = (unsigned char)g; (*(pointer+2)) = (unsigned char)r; }
#define ASD_RGB_GET_PIXEL(pointer, r, g, b) {b = (unsigned char)(*(pointer)); g = (unsigned char)(*(pointer+1)); r = (unsigned char)(*(pointer+2));}
//read mask and draw src_image by mask point
//ok
static void displayBuffer(IplImage *rgbDestImage, IplImage *buffer, int rValue, int gValue, int bValue)
{
int x, y, nWidth, nHeight;
double destX, destY, dx, dy;
uchar c;
unsigned char *pSrc;
//capture size of image
nWidth = buffer->width;
nHeight = buffer->height;
//calc Proportion
dx = double(rgbDestImage->width)/double(nWidth);
dy = double(rgbDestImage->height)/double(nHeight);
destX = 0;
for (x = 0; x < nWidth; x++)
{
destY = 0;
for (y = 0; y < nHeight; y++)
{
c = ((uchar*)(buffer->imageData + buffer->widthStep*y))[x];
if (c) //see data
{
pSrc = (unsigned char *)rgbDestImage->imageData + rgbDestImage->widthStep*int(destY) + (int(destX)*rgbDestImage->nChannels);
ASD_RGB_SET_PIXEL(pSrc, rValue, gValue, bValue); //set_data
}
destY += dy;
}
destX += dx;
}
};
/*************************************/
int main(int argc, char** argv )
{
IplImage *img, *filterMask = NULL;
CvAdaptiveSkinDetector filter(1, CvAdaptiveSkinDetector::MORPHING_METHOD_ERODE_ERODE);
ASDCVFrameSequencer *sequencer;
CvFont base_font;
char caption[2048], s[256], windowName[256];
//calc time
long int clockTotal = 0, numFrames = 0;
std::clock_t clock;
//program init
if (argc < 4) //ok
{
std::sprintf(caption, "%s", "ImageSize");
//read camera
sequencer = new ASDFrameSequencerWebCam();
(dynamic_cast<ASDFrameSequencerWebCam*>(sequencer))->open(-1);
if (! sequencer->isOpen())
{
std::cout << std::endl << "Error: Cannot initialize the default Webcam" << std::endl << std::endl;
}
}
else //ok
{
//read file
sequencer = new ASDFrameSequencerImageFile();
(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
}
//define windows name //ok
std::sprintf(windowName, "%s", "Adaptive Skin Detection Algorithm for Video Sequences");
//creat window
cvNamedWindow(windowName, CV_WINDOW_AUTOSIZE);
//init front
cvInitFont( &base_font, CV_FONT_VECTOR0, 0.5, 0.5);
std::cout << "Press ESC to stop." << std::endl << std::endl;
//main program
while ((img = sequencer->getNextImage()) != 0)
{
numFrames++;
if (filterMask == NULL) //save MASK_IMAGE
{
filterMask = cvCreateImage( cvSize(img->width, img->height), IPL_DEPTH_8U, 1);
}
clock = std::clock();//calc start time
filter.process(img, filterMask); // DETECT SKIN Color segmentation
clockTotal += (std::clock() - clock); //calc end time and add
displayBuffer(img, filterMask, 0, 255, 0);
//no think
sequencer->getFrameCaption(caption);
//
std::sprintf(s, "%s - %d x %d", caption, img->width, img->height);
putTextWithShadow(img, s, cvPoint(10, img->height-35), &base_font);
//ok
std::sprintf(s, "Average processing time per frame: %5.2fms", (double(clockTotal*1000/CLOCKS_PER_SEC))/numFrames);
putTextWithShadow(img, s, cvPoint(10, img->height-15), &base_font);
cvShowImage (windowName, img);
cvReleaseImage(&img); //clear img data
if (cvWaitKey(1) == 27)
break;
}
//delete point
sequencer->close();
delete sequencer;
cvReleaseImage(&filterMask);
cvDestroyWindow(windowName);
return 1;
}
#include "opencv2/opencv.hpp"
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
using namespace std;
using namespace cv;
class ASDCVFrameSequencer
{
protected: //parent Variable
CvCapture *capture;
public:
void getFrameCaption(char *caption);
virtual IplImage *getNextImage();
virtual void close();
virtual bool isOpen();
};
//open camera
class ASDFrameSequencerWebCam : public ASDCVFrameSequencer
{
public:
virtual bool open(int cameraIndex);
};
//open video file
class ASDFrameSequencerVideoFile : public ASDCVFrameSequencer
{
public:
virtual bool open(const char *fileName);
};
//read sequencer image
class ASDFrameSequencerImageFile : public ASDCVFrameSequencer
{
private:
char sFileNameMask[2048];
int nCurrentIndex, nStartIndex, nEndIndex;
public:
virtual void open(const char *fileNameMask, int startIndex, int endIndex);
virtual void getFrameCaption(char *caption);
virtual IplImage *getNextImage();
virtual void close();
virtual bool isOpen();
};
//-------------------- ASDCVFrameSequencer -----------------------//
//camera video all same methods
bool ASDCVFrameSequencer::isOpen() //did camera open
{
return (capture != NULL);
};
IplImage* ASDCVFrameSequencer::getNextImage()
{
IplImage *image;
image = cvQueryFrame(capture);
if (image != NULL)
{
return cvCloneImage(image);
}
else
{
return NULL;
}
};
void ASDCVFrameSequencer::close() //close camera
{
if (capture != NULL)
{
cvReleaseCapture(&capture);
}
};
void ASDCVFrameSequencer::getFrameCaption(char *caption) {
return;
};
//-------------------- ASDFrameSequencerWebCam -----------------------//
//read camera
bool ASDFrameSequencerWebCam::open(int cameraIndex)
{
capture = cvCaptureFromCAM(cameraIndex);
if (!capture)
{
return false;
}
else
{
return true;
}
};
//-------------------- ASDFrameSequencerVideoFile -----------------------//
//read video file
bool ASDFrameSequencerVideoFile::open(const char *fileName)
{
capture = cvCaptureFromFile(fileName);
if (!capture)
{
return false;
}
else
{
return true;
}
};
//-------------------- ASDFrameSequencerImageFile -----------------------//
// read sequencer image
void ASDFrameSequencerImageFile::open(const char *fileNameMask, int startIndex, int endIndex)
{
nCurrentIndex = startIndex-1;
nStartIndex = startIndex;
nEndIndex = endIndex;
std::sprintf(sFileNameMask, "%s", fileNameMask);
};
void ASDFrameSequencerImageFile::getFrameCaption(char *caption) {
std::sprintf(caption, sFileNameMask, nCurrentIndex);
};
IplImage* ASDFrameSequencerImageFile::getNextImage()
{
char fileName[2048];
nCurrentIndex++;
if (nCurrentIndex > nEndIndex)
return NULL;
std::sprintf(fileName, sFileNameMask, nCurrentIndex);
IplImage* img = cvLoadImage(fileName);
return img;
};
void ASDFrameSequencerImageFile::close()
{
nCurrentIndex = nEndIndex+1;
};
bool ASDFrameSequencerImageFile::isOpen()
{
return (nCurrentIndex <= nEndIndex);
};
/********************************************/
//put font information
//CvFont base_font;
//cvInitFont( &base_font, CV_FONT_VECTOR0, 0.5, 0.5);
static void putTextWithShadow(IplImage *img, const char *str, CvPoint point, CvFont *font, CvScalar color = CV_RGB(255, 255, 128))
{
cvPutText(img, str, cvPoint(point.x-1,point.y-1), font, CV_RGB(0, 0, 0));
cvPutText(img, str, point, font, color);
};
/*************************************/
//have one point data
#define ASD_RGB_SET_PIXEL(pointer, r, g, b) { (*pointer) = (unsigned char)b; (*(pointer+1)) = (unsigned char)g; (*(pointer+2)) = (unsigned char)r; }
#define ASD_RGB_GET_PIXEL(pointer, r, g, b) {b = (unsigned char)(*(pointer)); g = (unsigned char)(*(pointer+1)); r = (unsigned char)(*(pointer+2));}
//read mask and draw src_image by mask point
//ok
static void displayBuffer(IplImage *rgbDestImage, IplImage *buffer, int rValue, int gValue, int bValue)
{
int x, y, nWidth, nHeight;
double destX, destY, dx, dy;
uchar c;
unsigned char *pSrc;
//capture size of image
nWidth = buffer->width;
nHeight = buffer->height;
//calc Proportion
dx = double(rgbDestImage->width)/double(nWidth);
dy = double(rgbDestImage->height)/double(nHeight);
destX = 0;
for (x = 0; x < nWidth; x++)
{
destY = 0;
for (y = 0; y < nHeight; y++)
{
c = ((uchar*)(buffer->imageData + buffer->widthStep*y))[x];
if (c) //see data
{
pSrc = (unsigned char *)rgbDestImage->imageData + rgbDestImage->widthStep*int(destY) + (int(destX)*rgbDestImage->nChannels);
ASD_RGB_SET_PIXEL(pSrc, rValue, gValue, bValue); //set_data
}
destY += dy;
}
destX += dx;
}
};
/*************************************/
int main(int argc, char** argv )
{
IplImage *img, *filterMask = NULL;
CvAdaptiveSkinDetector filter(1, CvAdaptiveSkinDetector::MORPHING_METHOD_ERODE_ERODE);
ASDCVFrameSequencer *sequencer;
CvFont base_font;
char caption[2048], s[256], windowName[256];
//calc time
long int clockTotal = 0, numFrames = 0;
std::clock_t clock;
//program init
if (argc < 4) //ok
{
std::sprintf(caption, "%s", "ImageSize");
//read camera
sequencer = new ASDFrameSequencerWebCam();
(dynamic_cast<ASDFrameSequencerWebCam*>(sequencer))->open(-1);
if (! sequencer->isOpen())
{
std::cout << std::endl << "Error: Cannot initialize the default Webcam" << std::endl << std::endl;
}
}
else //ok
{
//read file
sequencer = new ASDFrameSequencerImageFile();
(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
}
//define windows name //ok
std::sprintf(windowName, "%s", "Adaptive Skin Detection Algorithm for Video Sequences");
//creat window
cvNamedWindow(windowName, CV_WINDOW_AUTOSIZE);
//init front
cvInitFont( &base_font, CV_FONT_VECTOR0, 0.5, 0.5);
std::cout << "Press ESC to stop." << std::endl << std::endl;
//main program
while ((img = sequencer->getNextImage()) != 0)
{
numFrames++;
if (filterMask == NULL) //save MASK_IMAGE
{
filterMask = cvCreateImage( cvSize(img->width, img->height), IPL_DEPTH_8U, 1);
}
clock = std::clock();//calc start time
filter.process(img, filterMask); // DETECT SKIN Color segmentation
clockTotal += (std::clock() - clock); //calc end time and add
displayBuffer(img, filterMask, 0, 255, 0);
//no think
sequencer->getFrameCaption(caption);
//
std::sprintf(s, "%s - %d x %d", caption, img->width, img->height);
putTextWithShadow(img, s, cvPoint(10, img->height-35), &base_font);
//ok
std::sprintf(s, "Average processing time per frame: %5.2fms", (double(clockTotal*1000/CLOCKS_PER_SEC))/numFrames);
putTextWithShadow(img, s, cvPoint(10, img->height-15), &base_font);
cvShowImage (windowName, img);
cvReleaseImage(&img); //clear img data
if (cvWaitKey(1) == 27)
break;
}
//delete point
sequencer->close();
delete sequencer;
cvReleaseImage(&filterMask);
cvDestroyWindow(windowName);
return 1;
}