整理的opencv2.4.5—sample—adaptiveskindetector.cpp文件更简洁形式

该博客展示了如何将OpenCV2.4.5中的adaptiveskindetector.cpp文件进行简化,包括定义了一个ASDCVFrameSequencer基类,以及其派生类ASDFrameSequencerWebCam、ASDFrameSequencerVideoFile和ASDFrameSequencerImageFile,分别用于读取摄像头、视频文件和图片序列。同时,实现了一种自适应皮肤检测算法并展示在图像上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值