C ++ + OpenCV =访问冲突读取位置0x02176000 [英] C++ + OpenCV = Access violation reading location 0x02176000
问题描述
此代码实际上一直可以使用到现在.我不知道是什么原因导致它现在引发错误(我实际上不记得对代码进行任何更改).就在这里(它将图像从文件读取到OpenCV IplImage对象,然后将其转换为jpeg缓冲区):
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
完整错误是:
Unhandled exception at 0x638fee22 in Client.exe: 0xC0000005: Access violation reading location 0x02176000.
fIplImageHeader包含一个有效的图像,我可以使用以下图像进行确认:
The fIplImageHeader contains a valid image which I can confirm by using:
cvShowImage( "Window", fIplImageHeader );
更长的代码段:
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 );
}
推荐答案
应该将输出缓冲区的大小调整为输出图像的大小,但是尚未使用buf
对象指定显式的大小.至少您在 http://opencv.willowgarage.com/documentation/cpp/reading_and_writing_images_and_video.html
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
您可以尝试在buf
上设置显式尺寸吗?现在,在您的代码示例中,它只是一个空向量.
Can you try setting an explicit size on buf
? Right now in your code sample, it is just an empty vector.
是的,我认为您是对的,如果我在文档页面上斜视了更多内容,似乎表明cv::imencode
将进行分配,因此您不必这样做.如果是这样,那么您的输入图像是否真的很大,是否用完了内存?您还可以进入cv::imencode
的调试版本吗?
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
?
此页面上还有另一个代码示例 http://opencv.willowgarage .com/documentation/cpp/core_basic_structures.html#Mat
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
您还可以尝试执行转换为cv :: Mat"步骤并将其传递给cv::imencode
吗?
Can you also try the "convert to cv::Mat" step, and pass that into cv::imencode
?
这篇关于C ++ + OpenCV =访问冲突读取位置0x02176000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!