用OpenCV库函数捕捉图像上传FTP服务器
前一段时间实验用到将摄像头捕捉的图像上传到一个网内的FTP服务器的功能,于是写了一个封装类。上传FTP服务器的功能主要使用了MFC中的CInternetSession和CFtpConnection类,具体步骤:
(1)创建CInternetSession对象,声明CFtpConnection变量;
(2)调用CIternetSession的GetFtpConnection函数获得CFtpConnection对象;
(3)调用CFtpConnection的Putfile方法上传文件或(4);
(4)调用CFtpConnection的OpenFile方法获得CInternetFile对象;
(5)调用CInternetFile的Read/Write方法;
(6)CInternet Exception异常处理。
利用OpenCV捕捉摄像头图像是在简单不过的事了这里里就不详细叙述了只写初几个关键函数
(1)CvCapture * capture = cvCaptureFromCAM(0) ;//初始化摄像头并获得一个capture
(2) IplImage * frame = cvQueryFrame( capture );//获得一帧Frame图像
(3)创建你想要的图像类型;
(4)cvSaveImage("image.jpg",image); //保存图像
(5)cvReleaseCapture( &capture ); //释放摄像头资源
源码:
#include "cv.h"
#include "highgui.h"
#include
#include
#include "afxinet.h"
//捕捉图像与图像Ftp上传类
class CFtpImage
{
public:
int CaptureImage();//int argc, char** argv);
int Ftp();
CFtpImage();
virtual ~CFtpImage();
private:
IplImage *image, *hsv, *hue , *mask, *backproject, *histimg;
IplImage* frame ;
CvHistogram *hist ;
CInternetSession* m_cInternetSession;
CFtpConnection* m_cFtpConnection;
};
// FtpImage.cpp: 实现文件
#include "FtpImage.h"
#include "afxinet.h"
#include "iostream.h"
#include "cv.h"
#include "highgui.h"
#include
#include
CFtpImage::CFtpImage()
{
m_cFtpConnection=NULL;
m_cInternetSession=new CInternetSession();
if(m_cInternetSession!=NULL)
{
cout<<"connecting..."<
int count=0;
while(m_cFtpConnection==NULL)
{
Sleep(60000);
m_cFtpConnection=m_cInternetSession->GetFtpConnection("192.168.1.199",NULL,NULL,21,FALSE);
//Sleep(50000);
count++;
if(count==5||m_cFtpConnection!=NULL) break;
}
}
//初始化捕捉图像的变量
image=0;
hsv=0;
hue=0;
mask=0;
backproject=0;
histimg=0;
frame=0;
hist=0;
}
CFtpImage::~CFtpImage()
{
if(m_cInternetSession!=NULL) m_cInternetSession->Close(); //注意一定要关闭连接
if(m_cInternetSession!=NULL) delete m_cInternetSession; //注意关闭的顺序
}
//ftp传输文件函数
int CFtpImage::Ftp()
{
if(m_cInternetSession!=NULL)
{
if(m_cFtpConnection!=NULL)
{
cout<<"connection success!"<
{
cout<<"putfile success!"<
}
else
{
cout<<"putfile timeout!"<
//cout<
}
else
{
cout<<"connection timeout!"<
}
}
else return -2; //Session初始化失败
}
//捕捉视频图像函数
int CFtpImage::CaptureImage()//int argc, char** argv)
{
int hdims = 16;
float hranges_arr[] = {0,180};
float* hranges = hranges_arr;
CvCapture* capture = cvCaptureFromCAM(0);
if( !capture )
{
fprintf(stderr,"Could not initialize capturing.../n");
return -1;
}
frame = cvQueryFrame( capture );
if( !image )
{
/* allocate all the buffers */
image = cvCreateImage( cvGetSize(frame), 8, 3 );
image->origin = frame->origin;
hsv = cvCreateImage( cvGetSize(frame), 8, 3 );
hue = cvCreateImage( cvGetSize(frame), 8, 1 );
mask = cvCreateImage( cvGetSize(frame), 8, 1 );
backproject = cvCreateImage( cvGetSize(frame), 8, 1 );
hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
histimg = cvCreateImage( cvSize(320,200), 8, 3 );
cvZero( histimg );
}
cvCopy( frame, image, 0 );
cvCvtColor( image, hsv, CV_BGR2HSV );
cvSaveImage("image.jpg",image); //保存图像
cvReleaseCapture( &capture );
return 1;
}