gpt4 book ai didi

c++ - 使用 SetData() 分配数据后 cvReleaseImage() 出现段错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:52 26 4
gpt4 key购买 nike

我正在尝试从相机获取单张图像,对它们进行一些处理并释放使用的内存。我已经使用类似于以下代码的代码做了很长一段时间:

char* img_data = new char[ len ]; // I retrieve len from the camera.

// Grab the actual image from the camera:
// It fills the previous buffer with the image data.
// It gives width and height.

CvSize size;
size.width = width;
size.height = height;
IplImage* img = cvCreateImageHeader( size, 8, 1 );
img->imageData = img_data;

// Do the processing

cvReleaseImage( &img );

这段代码运行良好。我最近读了here (在 imageData 描述中)我不应该将数据直接分配给 img->imageData 而是使用 SetData() 代替,如下所示:

cvSetData( img, img_data, width );

但是,当我这样做时,我在调用 cvReleaseImage() 时遇到了段错误。

我做错了什么?

谢谢。

编辑: 我已经尝试编译并运行@karlphillip 建议的程序,但我确实使用 cvSetData 遇到了段错误,但它在直接分配数据时运行良好。我正在使用 Debian 6 和 OpenCV 2.3.1。

最佳答案

我也遇到了这个问题,但相信根据我收集的信息已经解决了 this comment ;即使用 cvReleaseImageHeader() 而不是 cvReleaseImage()。

例如:

unsigned int width = 100;
unsigned int height = 100;
unsigned int channels = 1;
unsigned char* imageData = (unsigned char*)malloc(width*height*channels);
// set up the image data

IplImage *img = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, channels);
cvSetData(img, imageData, width*channels);

// use img

cvReleaseImageHeader(&img);

// free(imageData) when finished

关于c++ - 使用 SetData() 分配数据后 cvReleaseImage() 出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9032223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com