Ask Your Question

Aggieboy's profile - activity

2020-02-04 10:29:46 -0600 received badge  Famous Question (source)
2017-08-04 10:22:09 -0600 received badge  Notable Question (source)
2015-12-29 12:24:38 -0600 received badge  Popular Question (source)
2013-03-27 16:36:11 -0600 commented answer VideoCapture.retrieve() memory access violation

Well I figured out the answer, but this website won't let me answer my own question. If you post this for me, I can choose it as an answer. I wanted to test your hypothesis concerning the 0xc0000005 exception. I compiled my OpenCV library natively (I was using a prebuilt solution perviuosly) and found that the problem was caused by this. One way to fix the project is to downgrade to OpenCV2.1, but a workaround can be found with this using VirtualDub to re-compress your video using a native windows dll rather than OpenCV's ffmpeg (or at least that's how I understand it).

2013-03-27 13:46:14 -0600 commented answer VideoCapture.retrieve() memory access violation

Here is an example of the code that I'm using. Am I misunderstanding your solution?

2013-03-27 03:20:41 -0600 commented answer VideoCapture.retrieve() memory access violation

But I'm getting the memory access violation during the read, not during the push_back(). I can comment out the push_back() and still get the exception. Additionally, push_back() should run a copy constructor with "const Mat&" type as the input, although I'd have to double check (might be different depending on how they implemented it, but they should have the copy construction run clone()).

2013-03-26 23:20:48 -0600 received badge  Organizer (source)
2013-03-26 18:56:21 -0600 received badge  Editor (source)
2013-03-26 18:53:37 -0600 asked a question VideoCapture.retrieve() memory access violation

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

Edit: Forgot to mention that if you comment out the problem lines, the code works fine but the vector is filled with empty cv::Mat for each frame since the data was only grabbed and never retrieved.