OpenCV的一些功能的封装
写OpenCV程序的时候,感觉很多时候都会用到同样的功能,但是又需要写一大段的代码,感觉很不方面,接触C++之后,突然发现可以吧这些东西封装到一个类里面去,把接口定义的丰富一点,这样在后面时候的时候感觉挺方便的,不知道这样的可行性如何,但是感觉还是很不错的。
下面就是我定义的一个类,叫做Functionable,其中的头文件很源文件代码如下:
functionable.h:
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <cv.h>
#include <highgui.h>
class Functionable
{
public:
// Functionable();
void doShowImage(const char * filename, int waitKey, const char *windowName,int windowFlag);
void doPlayAvi(const char *filename, int waitKey, const char *windowName, int windowFlag, int endKey );
IplImage * doSmooth( IplImage * src );
IplImage * doPyrDown( IplImage * src, int filter = 7,int multiple = 1);
//The function doPyrDown didn't pass the test
IplImage * doCanny( IplImage * sr, double lowThresh,double highThresh, double aperture );
void doCamera( int cameraNumber, int waitKey, const char * windowName, int windowFlag, int endKey , int isSavaFile = 0, char * saveFileName = NULL );
private:
};
这是functionable.cpp:
#include "functionable.h"
//function that use to show an image without some input
void Functionable::doShowImage(const char *filename, int waitKey, const char *windowName,int windowFlag){
IplImage * img = cvLoadImage( filename );
cvNamedWindow( windowName ,windowFlag );
cvShowImage( windowName , img );
cvWaitKey( waitKey );
cvReleaseImage( &img );
cvDestroyWindow( windowName );
}
//function that use to play an movie which's type is avi
void Functionable::doPlayAvi(const char *filename, int waitKey, const char *windowName, int windowFlag, int endKey )
{
cvNamedWindow( windowName ,windowFlag );
CvCapture * capture = cvCreateFileCapture( filename );
IplImage * frame;
while( 1 ){
frame = cvQueryFrame( capture );
if( !frame ){
cout << "can not query frame!\n";
break;
}
cvShowImage( windowName , frame );
char c = cvWaitKey( waitKey );
if( c == endKey )
break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( windowName );
}
//let the image smoothed
IplImage * Functionable::doSmooth( IplImage * src )
{
IplImage * dst = cvCreateImage( cvGetSize( src ),src->depth,src->nChannels);
cvSmooth( src, dst, CV_GAUSSIAN,3,3);
return dst;
}
//minify the picture
IplImage * Functionable::doPyrDown( IplImage * src, int filter ,int multiple )
{
assert( src->width % multiple == 0 && src->height % multiple == 0);
IplImage * dst = cvCreateImage(
cvSize(src->width / multiple,src->height / multiple ),
src->depth,
src->nChannels
);
cvPyrDown( src,dst,filter );
return dst;
}
//function the do canny for a picture
IplImage * Functionable::doCanny( IplImage * src, double lowThresh,double highThresh, double aperture )
{
if( src->nChannels != 1 ){
cout << "Can not canny the picture\n";
return 0;
}
IplImage * dst = cvCreateImage(
cvGetSize(src),
IPL_DEPTH_8U,
1
);
cvCanny( src, dst, lowThresh,highThresh, aperture );
return dst;
}
//function that use to getVideo from camera
void Functionable::doCamera( int cameraNumber, int waitKey, const char * windowName, int windowFlag, int endKey , int isSavaFile , char * saveFileName )
{
CvCapture* capture;
capture = cvCreateCameraCapture( cameraNumber );
assert( capture != NULL );
cvNamedWindow( windowName, windowFlag );
IplImage * frame;
while( 1 ){
frame = cvQueryFrame( capture );
if( !frame ){
cout << "can not query frame!\n";
break;
}
cvShowImage( windowName , frame );
char c = cvWaitKey( waitKey );
if( c == endKey )
break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( windowName );
}
功能很不完善,有的函数还没有写完,继续...
浙公网安备 33010602011771号