0

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

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,这很奇怪。在我的代码的其他部分,这种使用方式是有效的。这里的声明部分:class 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;
4

1 回答 1

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;
    }

您还需要在适当的位置释放tempPrev与指针相关的内存:temp2

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

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

于 2013-08-01T17:09:06.503 回答