[英]How to crop a part of image in the different matrix, but the matrix doesn't change its size in openCV.
[英]openCV. copy or crop an image from real time feed from web cam, with out memory leak
我试图找到一种避免在while循环内使用cvCreateImage的方法,因为我已经意识到这将导致内存泄漏 。
我想类似这样 -虽然出了内存泄漏。
我不知道为什么这段代码不起作用。
我认为下面的代码可以运行,但是在运行时会中断。
if((capture = cvCreateCameraCapture(0)) == NULL) {
printf("connect cam first\n");
return -1;
}
IplImage *detecImg = cvCreateImage( cvSize(WIDTH, HEIGHT), 8, 1 );
IplImage *frameImage = NULL;
IplImage *notImage = NULL;
while(1){
cvWaitKey(1);
cvSplit(frameImage, a, b, c, NULL);
//detect objec from a,b,c.....output is "detecImg"
cvSetImageROI(detectImg, Roi); //Roi is changing depends on detection result
notImage=cvCloneImage(detectImg);//cvCloneImage,cvCopy not working...
cvNot(notImage, notImage);
copyNotImg = cvCloneImage(notImage);
... continues ...
}
如果我在下面使用此代码,它可以正常工作,但会泄漏一些内存。
if((capture = cvCreateCameraCapture(0)) == NULL) {
printf("connect cam first\n");
return -1;
}
IplImage *detecImg = cvCreateImage(cvSize(WIDTH, HEIGHT), 8, 1);
IplImage *frameImage = NULL;
IplImage *notImage = NULL;
while(1){
cvWaitKey(1);
cvSplit(frameImage, a, b, c, NULL);
//detect objec from a,b,c.....output is "detecImg"
cvSetImageROI( detectImg, Roi); //Roi is changing depends on detection result
notImage=cvCreateImage( cvSize(Roi.width, Roi.height), 8, 1 );
cvNot(notImage, notImage);
copyNotImg= cvCloneImage(notImage);
... continues ...
}
任何见识将不胜感激。
用cvCreateImage分配的任何图像都需要使用cvReleaseImage释放。 您要发布所有图像吗?
另外,您可以使用现代的C ++ OpenCV api来为您处理所有内存分配和释放。
在您的第一段代码中,您尝试将图像复制到空图像。 必须为映像分配足够的内存资源。 即
IplImage *notImage = NULL;
notImage=cvCloneImage(detectImg);
这里notImage为空。 它没有分配内存。 因此您的代码会中断。 而在第二种情况下,复制之前将内存分配给notImage。
为了避免内存泄漏,请尝试此
IplImage* notImage=cvCloneImage(detectImg);
但请记住最后要释放图像。
代替cvCloneImage,首先创建映像,然后使用cvCopyImage复制映像。新版本的opencv提供了更好,更简单的实现。
http://www.cprogramdevelop.com/4885055/上面的链接包含有关内存泄漏的一些信息,希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.