Ask Your Question

Access violation when using imwrite to save an imageMat

asked 2015-10-21 16:52:20 -0500

Hi everybody!

I'm having an extremely annoying issue when tryig to save a PGN file with the depth value of a Kinect frame. I already have the millimeters Matrix in format.

 cv::Mat imageMat = cv::Mat(height, width, CV_16UC1);

I'm having a problem when I execute this:

vector<int> pars;
vector<unsigned char> image;
char *fmt = (char *)".png";
cv::imencode(fmt, imageMat, image, pars);
cv::imwrite("data.png", imageMat, pars);

The problem is actually in the last two lines. They both returns the same "Access violation while reading location at..."

Could anybody help me? I'm using OpenCV 3.0 on Visual Studio 2013.

Thank you all!

edit retag flag offensive close merge delete



you are trying to save an empty vector... You need to put something in that vector, first

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-22 02:52:25 -0500 )edit

why the imencode() ?

berak gravatar imageberak ( 2015-10-22 03:29:41 -0500 )edit

I'm filling imageMat with depth values of a Kinect depth frame. For that, I'm using the following code:

    int i = 0;
    int j = 0;
            while (curr < dataEnd) {

        if (j == imageMat.cols){
            j = 0;

        // Get depth in millimeters
        USHORT depth = NuiDepthPixelToDepth(*curr++);<unsigned short>(i, j) = depth;

germano.gcf gravatar imagegermano.gcf ( 2015-10-22 10:09:31 -0500 )edit

i'm pretty sure, your writing code is correct, and the culprit is your (weird) filling code. what is dataEnd ?

berak gravatar imageberak ( 2015-10-23 04:52:50 -0500 )edit

In my case c++ Code Generation settings were wrong Should have been Multithreaded DEBUG Dll MT

Roman_Bob gravatar imageRoman_Bob ( 2019-09-19 09:48:21 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2015-10-22 10:19:55 -0500

dlavy88 gravatar image

This is the piece of code I used to save a png file:

//Save the output image
cv::Mat frame0; //output of my program
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); // compression technique
compression_params.push_back(98); //compression quality
bool bSuccess = imwrite("hands.png", frame0, compression_params); 

I don't really need to use imencode just those 4 lines.

Hope it helps.

edit flag offensive delete link more


Thanks for the help. But it didn't work. It keeps returning to me this message:

    Unhandled exception at 0x000007FEE4DDE975 (opencv_world300.dll) in... Access Violation reading location...
germano.gcf gravatar imagegermano.gcf ( 2015-10-22 11:26:08 -0500 )edit

answered 2016-06-07 10:38:34 -0500

Hey there,

I am programming with VS 2013 and was having the same problem. I simply had to change from Debug to Release right below Analyze. This might help you.

edit flag offensive delete link more

answered 2017-12-14 02:58:19 -0500

I had the same issue, the problem was that I had the wrong opencv lib file in Project properties > Linker > Input > Additional Dependencies

You have to use these:

  • opencv_world300d.lib for the debug mode
  • opencv_world300.lib for the release mode
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-10-21 16:52:20 -0500

Seen: 1,422 times

Last updated: Dec 14 '17