Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ + OpenCV = Access violation reading location 0x02176000

This code actually used to work until now. I have no idea what is causing it to throw an error now (I don't actually remember making any changes to the code). Here it is (it reads an image from a file to OpenCV IplImage object and then converts it to jpeg buffer):

  IplImage* fIplImageHeader = cvLoadImage( filePath.c_str() );

  vector<int> p;
  p.push_back( CV_IMWRITE_JPEG_QUALITY );
  p.push_back( 75 ); // JPG quality
  vector<unsigned char> buf;
  cv::imencode( ".jpg", fIplImageHeader, buf, p ); // this line gives error

The full error is:

Unhandled exception at 0x638fee22 in Client.exe: 0xC0000005: Access violation reading location 0x02176000.

The fIplImageHeader contains a valid image which I can confirm by using:

cvShowImage( "Window", fIplImageHeader );

EDIT:

A longer snippet:

while ( l < 30 )
{
            // path to image
    std::stringstream sstm;
    string filePath;
    sstm << workingDirectory << "/temp/" << k << ".jpg";
    filePath = sstm.str();

    cout << filePath.c_str() << endl;

    // load image to IplImage
    IplImage* fIplImageHeader = cvLoadImage( filePath.c_str() );

    // convert to JPG
    vector<int> p;
    p.push_back( CV_IMWRITE_JPEG_QUALITY );
    p.push_back( 75 ); // JPG quality
    vector<unsigned char> buf;
    cv::imencode( ".jpg", fIplImageHeader, buf, p );

            // do stuff

    k++;
    l++;
    if (10 == k)
    {
        k = 0;
    }

    char key = cvWaitKey( 1000/30 );

    cvReleaseImage( &fIplImageHeader );
}
like image 304
Richard Knop Avatar asked Dec 01 '25 05:12

Richard Knop


2 Answers

The output buffer is supposed to be resized to the output image, but you have not specified an explicit size with the buf object. At least this is mentioned from your referenced doc page at http://opencv.willowgarage.com/documentation/cpp/reading_and_writing_images_and_video.html

Can you try setting an explicit size on buf? Right now in your code sample, it is just an empty vector.

EDIT: Yes, I think you're right, if I squint some more at the doc page, it seems to indicate that cv::imencode will do the allocation, so you shouldn't have to. If that's true then, is your input image really large, are you running out of memory? Also can you step into the debug version of cv::imencode?

EDIT: There's another code sample at this page http://opencv.willowgarage.com/documentation/cpp/core_basic_structures.html#Mat

IplImage* img = cvLoadImage("greatwave.jpg", 1);
Mat mtx(img); // convert IplImage* -> cv::Mat

Can you also try the "convert to cv::Mat" step, and pass that into cv::imencode?

like image 143
Chris O Avatar answered Dec 03 '25 18:12

Chris O


EDIT : My bad, I was not using that method... So I don't know what is going wrong...

I had the same exception using "imencode", I don't know if I have to resize the jpegBuf vector

void CWebcamWidget::putJpegImage(IplImage *iplImage) {
    // Sans compression jpeg
    //image = QImage((const uchar*)iplImage->imageData, iplImage->width, iplImage->height, QImage::Format_RGB888).rgbSwapped();

    // Still doesn't work using cv::vector...
    cv::vector<int> p;
    p.push_back(CV_IMWRITE_JPEG_QUALITY);
    p.push_back(75); // JPG quality
    cv::vector<uchar> jpegBuf;
    imencode(".jpg", iplImage, jpegBuf, p);

    // Conversion vector<uchar> => uchar*
    uchar *buf;
    memcpy(buf, &jpegBuf[0], sizeof(uchar)*jpegBuf.size());

    image = QImage((const uchar*)buf, iplImage->width, iplImage->height, QImage::Format_RGB888).rgbSwapped();
    imageLabel->setPixmap(QPixmap::fromImage(image));
}

I don't know if I really need to convert from vector to uchar*, maybe I can save some CPU time without it...

like image 22
Benjamin Le Cadre Avatar answered Dec 03 '25 19:12

Benjamin Le Cadre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!