OpenCV 2.4.5运行时错误内存

时间:2013-08-01 16:42:15

标签: c++ opencv visual-studio-2012

每次我想使用函数Generate3DProfile时,条件是生成应该从当前索引开始,我在内存位置0x0024EFD4错误时得到cv :: Exception,直到if(!imgused [i])继续; 。但是当我对整个序列进行生成时没有错误,这是代码的重要部分:

void TPictureWindow::Generate3DProfile()
{
 char buffer[MAX_PATH];
 int value, newHeight, newWidth, refYpos = 0;

IplImage* transformB = cvCreateImage(cvSize(m_imgSequence[0]->width, NumImages), IPL_DEPTH_8U, 1);
IplImage* transformG = cvCreateImage(cvSize(m_imgSequence[0]->width, NumImages), IPL_DEPTH_8U, 1);
IplImage* transformR = cvCreateImage(cvSize(m_imgSequence[0]->width, NumImages), IPL_DEPTH_8U, 1);

cvZero(transformB);
cvZero(transformG);
cvZero(transformR);

for(int i = 0; i < NumImages; i++)
{   
    if(!imgused[i]) continue;

    for(int j = 0; j < edge[i][0]->total; j++)
    {
        MessageBox("hallo", "", MB_OK);
        CvPoint* pt1 = CV_GET_SEQ_ELEM(CvPoint, edge[i][0], j);
        CvPoint* pt2 = CV_GET_SEQ_ELEM(CvPoint, edge[i][1], j);

        value = (pt1->y-pt2->y)*1000*scaling;
        //sprintf_s(buff3,"%d", value);
        //MessageBox(buff3, "", MB_OK);
        if(value < 0) value = 0;
        transformB->imageData[i*transformB->widthStep+pt1->x] = value%256;
        transformG->imageData[i*transformG->widthStep+pt1->x] = value/256;
    }
}
IplImage* final = cvCreateImage(cvSize(m_imgSequence[0]->width, NumImages), IPL_DEPTH_8U, 3);
cvMerge(transformB, transformG, transformR, NULL, final);
cvFlip(final,final);

if(isFirst){
    cvSaveImage(outFileName, final);
    isFirst = false;
}else{

    //sprintf_s(buffer, "%s%04d%s", TempFileName, prevIndex, ".tif");
    IplImage* tempPrev = cvLoadImage(outFileName,CV_LOAD_IMAGE_COLOR);//name ändern
    if(imgIndex/100 == prevIndex/100){
        newHeight = tempPrev->height >= final->height*(imgIndex%100+1)?tempPrev->height : tempPrev->height+final->height;
        newWidth  = tempPrev->width;
        newYpos = (imgIndex/100)%2 > 0 ? newYpos-final->height : newYpos+final->height;
        if(newYpos < 0){
            newYpos = 0;
            refYpos = final->height;//fall dass es unten noch weitere blöcke gibt
        }
    }else{
            newHeight = tempPrev->height;
            newWidth  = tempPrev->width+final->width;
            newXpos += final->width;
     }
        IplImage* temp2 = cvCreateImage(cvSize(newWidth, newHeight), IPL_DEPTH_8U, 3);
        cvZero(temp2);
        cvSetImageROI(temp2, cvRect(0,refYpos, tempPrev->width, tempPrev->height));
        cvCopy(tempPrev, temp2);
        cvSetImageROI(temp2,cvRect(newXpos, newYpos, final->width, final->height));
        cvCopy(final, temp2);
        cvResetImageROI(temp2);
        cvSaveImage(outFileName, temp2);
  }
    //sprintf_s(buffer, "%s%04d%s", TempFileName, imgIndex, ".tif");

    PostMessage(READY_WITH, IMG_3D_PROFILE, 0);
}

更新部分:

哦,好吧,我发现应该是1或0的imgused的值是69,这很奇怪。在我的代码的其他部分这种使用它的方式工作。这里声明部分:类TPictureWindow:

public TWindow
{
 private:
 bool isBorders, isDoWhiteLine, isLoaded, isMultiple, useRoi4all, canBuild, isFirst;
 char TempFileName[MAX_PATH], outFileName[MAX_PATH];
 unsigned int NumImages, ImgWidth, ImgHeight;
 int refracidx, lini, actualPicIndex, newindex, imgIndex, abort, prevIndex, newXpos, newYpos;
 bool imgused[MAX_IMAGES];
 double scaling, deltaX, deltaY;

1 个答案:

答案 0 :(得分:1)

您还需要提供定义imgused的代码。此外,代码还存在其他一些问题。

作为使用OpenCV的C API的副作用,很容易泄漏内存:

    CvPoint* pt1 = CV_GET_SEQ_ELEM(CvPoint, edge[i][0], j);
    CvPoint* pt2 = CV_GET_SEQ_ELEM(CvPoint, edge[i][1], j);

对于每个循环,您都定义了CvPoint的这些指针,但是在每次迭代中超出范围时都不会删除它们。为了防止这种泄漏,你可以写:

for(int j = 0; j < edge[i][0]->total; j++)
    {
        MessageBox("hallo", "", MB_OK);
        CvPoint* pt1 = CV_GET_SEQ_ELEM(CvPoint, edge[i][0], j);
        CvPoint* pt2 = CV_GET_SEQ_ELEM(CvPoint, edge[i][1], j);

        value = (pt1->y-pt2->y)*1000*scaling;
        //sprintf_s(buff3,"%d", value);
        //MessageBox(buff3, "", MB_OK);
        if(value < 0) value = 0;
        transformB->imageData[i*transformB->widthStep+pt1->x] = value%256;
        transformG->imageData[i*transformG->widthStep+pt1->x] = value/256;
        delete pt1; //delete pointers
        delete pt2;
    }

您还需要在适当的位置发布与指针tempPrevtemp2相关的记忆:

    ...
    cvResetImageROI(temp2);
    cvSaveImage(outFileName, temp2);
    cvReleaseImage(&tempPrev);
    cvReleaseImage(&temp2);

您可以考虑使用C ++ API以避免内存管理问题。