C ++ + OpenCV = Access Read Access Violations 0x02176000

This code has really been used so far. I have no idea what causes him to throw an error now (I really donโ€™t remember making any changes to the code). Here it is (it reads the image from the file into an OpenCV IplImage object and then converts it to a 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

Full error:

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

There is a valid image in fIplImageHeader that I can confirm using:

cvShowImage( "Window", fIplImageHeader );

EDIT:

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 );
}
+3
source share
3 answers

, , buf. , http://opencv.willowgarage.com/documentation/cpp/reading_and_writing_images_and_video.html

buf? .

EDIT: , , , doc, , cv::imencode , . , , ? cv::imencode?

: http://opencv.willowgarage.com/documentation/cpp/core_basic_structures.html#Mat

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

"convert to cv:: Mat" cv::imencode?

+2

: , ... , ...

, "imencode", , 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));
}

, uchar *, , ...

+2

Debug OpenCV 3.0 Visual Studio 2015. Visual Studio 2013, , . . imencode, :

bool writeImageToDisk(const std::string& filename, cv::Mat image) {
    std::vector<uchar> imageData;
    bool result = cv::imencode(".jpg", image, imageData);
    if (!result) {
        return false;
    }
    ...

openvc_world300.dll .lib, OpenCV. vc12, , Visual Studio 2013. Debug, DLL Visual ++ 2013 .

After upgrading to OpenCV 3.2, this problem was fixed for me without touching anything else, so there might be an error or problem with distributed binaries.

0
source

Source: https://habr.com/ru/post/1780929/


All Articles