Я инкапсулирую захваченное изображение в классе «Захват», возможно, вы могли бы попробовать.Ниже приведены main.cpp, capture.h и capture.cpp, наслаждайтесь:)
main.cpp
#include "capture.h"
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
// --- read file --- //
//Capture capture("test.wmv", windowSize, windowSize);
// --- read camera --- //
CvSize windowSize = cvSize(640, 480);
Capture capture(2, windowSize, windowSize);
while (1) {
capture.captureNext();
for (int i = 0; i < capture.channelNum; ++i) {
ostringstream oss;
oss << i;
string winName = "WINDOW-" + (oss.str());
cvShowImage(winName.c_str(), capture.channelframeList[i]);
}
int c = cvWaitKey(30);
if ( (char) c == 27 ) { // 'Esc' to terminate
break;
}
}
return 0;
}
caputure.h
#ifndef _CAPTURE_H_
#define _CAPTURE_H_
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
enum VIDEO_TYPE {
CAMMERA = 0,
VIDEOFILE
};
class Capture {
public:
Capture(int num, CvSize dispSize, CvSize resolutionSize);
Capture(string fileName, CvSize dispSize, CvSize resolutionSize);
int channelNum;
vector<IplImage*> channelframeList;
void captureNext();
~Capture();
private:
string m_fileName;
vector<CvCapture*> m_channelList;
CvSize m_resolutioSize;
CvSize m_displaySize;
void initChannelList(VIDEO_TYPE type);
void initChannelImgList(CvSize sz);
IplImage* getNextVideoFrame(CvCapture* pCapture);
};
#endif
capture.cpp
#include "capture.h"
Capture::Capture(int num, CvSize dispSize, CvSize resolutionSize) {
channelNum = num;
m_fileName = "";
m_resolutioSize = resolutionSize;
m_channelList.resize(channelNum);
channelframeList.resize(channelNum);
initChannelList(CAMMERA);
initChannelImgList(dispSize);
}
Capture::Capture(string fileName, CvSize dispSize, CvSize resolutionSize) {
channelNum = 1;
m_fileName = fileName;
m_resolutioSize = resolutionSize;
m_channelList.resize(channelNum);
channelframeList.resize(channelNum);
initChannelList(VIDEOFILE);
initChannelImgList(dispSize);
}
void Capture::captureNext() {
for (int i = 0; i < channelNum; ++i) {
IplImage* nextFrame = getNextVideoFrame(m_channelList[i]);
IplImage* channelFrame = channelframeList[i];
cvResize(nextFrame, channelFrame);
}
}
void Capture::initChannelList(VIDEO_TYPE type) {
if (type == CAMMERA) {
for (int i = 0; i < channelNum; ++i) {
m_channelList[i] = cvCreateCameraCapture(i);
//set resolution
cvSetCaptureProperty(m_channelList[i], CV_CAP_PROP_FRAME_WIDTH, m_resolutioSize.width);
cvSetCaptureProperty(m_channelList[i], CV_CAP_PROP_FRAME_HEIGHT, m_resolutioSize.height);
if ( !(m_channelList[i]) ) {
cout << "failed to initialize video capture" << endl;
exit(EXIT_FAILURE);
}
}
} else if (type == VIDEOFILE) {
const char* fileNameChar = m_fileName.c_str();
m_channelList[0] = cvCreateFileCapture(fileNameChar);
if ( !(m_channelList[0]) ) {
cout << "failed to initialize video capture" << endl;
exit(EXIT_FAILURE);
}
}
}
void Capture::initChannelImgList(CvSize sz) {
for (int i = 0; i < channelNum; ++i)
channelframeList[i] = cvCreateImage(sz, 8, 3);
}
IplImage* Capture::getNextVideoFrame(CvCapture* pCapture) {
IplImage* nextFrame = cvQueryFrame(pCapture);
if (!nextFrame) {
cout << "failed to get a video frame" << endl;
exit(EXIT_FAILURE);
}
return nextFrame;
}
Capture::~Capture() {
for (int i = 0; i < channelNum; ++i) {
cvReleaseImage( &(channelframeList[i]) );
cvReleaseCapture(&m_channelList[i]);
}
}