我使用cvGetSeqElem时访问违规读取位置

时间:2016-12-12 10:35:15

标签: c opencv

我的问题是当我使用cvGetSeqElem时访问违规读取位置。

IplImage* debugImLBP = NULL;
IplImage* debugImLBPOut = NULL;
debugImLBP = cvLoadImage("B.png",0);
//detect face :
CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad( "haarcascade_frontalface_alt2.xml" );
CvSeq faces;
detect_and_draw_objects( debugImLBP, cascade, 1, &faces );  // on this moment I have faces that are not empty

CvRect face_rect = *(CvRect*)cvGetSeqElem( &faces, 0); // here I have access violation

enter image description here 函数detect_and_draw_objects:

void detect_and_draw_objects( IplImage* image, CvHaarClassifierCascade* cascade, int do_pyramids, CvSeq* facesDetect )
{
IplImage* small_image = image;
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* faces;
int i, scale = 1;
/* if the flag is specified, down-scale the input image to get a
performance boost w/o loosing quality (perhaps) */
if( do_pyramids )
{
    small_image = cvCreateImage( cvSize(image->width/2,image->height/2), IPL_DEPTH_8U,1);
    cvPyrDown( image, small_image, CV_GAUSSIAN_5x5 );
    scale = 2;
}
/* use the fastest variant */
faces = cvHaarDetectObjects( small_image, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING );/* draw all the rectangles */

for( i = 0; i < faces->total; i++ )
{
// extract the rectanlges only
CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i);
cvRectangle( image, cvPoint(face_rect.x*scale,face_rect.y*scale), cvPoint((face_rect.x+face_rect.width)*scale, (face_rect.y+face_rect.height)*scale), CV_RGB(255,0,0), 3 );
}
*facesDetect = *faces;

if( small_image != image )
    cvReleaseImage( &small_image );
cvReleaseMemStorage( &storage );
}

enter image description here 我不明白为什么在detect_and_draw_objects中没有访问冲突以及为什么在“我的主要”中面部不是空的。

PS:在我公司,有义务使用opencv 1。 感谢

1 个答案:

答案 0 :(得分:0)

cvCreateMemStorage创建一个容器,该容器将保存随后分配的存储块的列表。对cvReleaseMemStorage的调用将破坏容器及其包含的内存块(或将其移动到父容器的空闲块列表中)。 cvHaarDetectObjects使用该容器中的内存块分配其结果序列。因此,释放存储容器后,cvHaarDetectObjects的返回值不再有效。
CvSeq包含从同一存储容器分配的其他块,这些块将被释放。即*facesDetect*faces的浅表副本,不是深表副本。                     –伊恩·雅培