I am attempting to store all of the frames of a video in a "std::vector<\cv::Mat>" (a very common goal across many forums I've found). The issue is that when I call VideoCapture::read(), or more specifically VideoCapture::retrieve(), I get a memory access violation. Here's my attempt so far:
#pragma once
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
class Video : public std::vector<cv::Mat>{
public:
//Constructors
Video(std::string name){ open(name); }
Video(){}
//OpenCV C++ version, BROKEN
size_t open(std::string name){
cv::VideoCapture source(name);
clear();
if(source.isOpened()){
cv::Mat grabbed;
//while(source.read(grabbed)){
while(source.grab()){
source.retrieve(grabbed); // <------ BROKEN LINE
std::cout << "Frame Parsed: " << source.get(CV_CAP_PROP_POS_FRAMES) << std::endl;
push_back(grabbed);
}
source.release();
}
return size();
}
};
In my frustration, I've also tried the C implementation, which has the same exception at cvQueryFrame().
//OpenCV C version, BROKEN
size_t open(std::string name){
clear();
CvCapture* capture = cvCaptureFromAVI((char*)name.c_str()); // read AVI video
if(capture){
IplImage* img;
int frameNum=0;
while(true){
img=cvQueryFrame( capture ); // <------- BROKEN LINE:
if(!img) break;
std::cout << "Frame Parsed: " << frameNum++ << std::endl;
cv::Mat m(img);
push_back(m);
}
}
return size();
}
I assume the C++ implementation uses C commands, so I guess it's to be expected. I was just hoping that this may provide more debug information. The closest issue that I've found is here, however his solution was a linkage problem; I've already made sure to provide opencv_**243d.dll in the project properties.
Additional information: 64-bit Windows, Visual Studio C++ 2010 Express (Compiling Win32 Debug mode, don't care about Release), OpenCV2.4.3 (as noted from the dlls). The actual exception is:
An unhandled exception of type 'System.AccessViolationException' occurred in MyProject.exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Finally, you just need this to test the code itself (of course, replace "HeadDemo.avi with whatever video you want that's in the same directory):
#include "video.h"
using namespace cv;
using namespace std;
int main(){
Video v("HeadDemo.avi");
return 1;
}