首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV NSImage到IplImage转换器过程中的泄漏

OpenCV NSImage到IplImage转换器过程中的泄漏
EN

Stack Overflow用户
提问于 2010-07-21 20:01:27
回答 1查看 1.1K关注 0票数 2

当我使用cvCreateImage而不是cvCreateImageHeader时,它是304Kb和107b的泄漏,但当我改变它时,它变成只有107位。你能帮我找一下漏水的地方吗?

代码语言:javascript
复制
+ (IplImage *) nsImageToIplImage:(NSImage *)image {
    // NSImage to IplImage

    NSBitmapImageRep *orig = [[image representations] objectAtIndex: 0];
    // a copy or else the color-channel shift that we do later on will affect the original NSImage!

    NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[orig representationUsingType:NSTIFFFileType properties:NULL]];
    int depth       = [rep bitsPerSample];
    int channels    = [rep samplesPerPixel];
    int height      = [rep size].height;
    int width       = [rep size].width;

    // note- channels had better be "3", or else the loop down below will act pretty funky...
    // NSTIFFFileType seems to always give three-channel images, so I think it's okay...


    IplImage *to_return = cvCreateImageHeader(cvSize(width, height), depth, channels);
    cvSetImageData(to_return, [rep bitmapData], [rep bytesPerRow]);



    // Reorder BGR to RGB
    // no, I don't know why it's in BGR after cvSetData
    for (int i = 0; i < to_return->imageSize; i += 3) {
        uchar tempR, tempG, tempB;
        tempR = to_return->imageData[i];
        tempG = to_return->imageData[i+1];
        tempB = to_return->imageData[i+2];

        to_return->imageData[i] = tempR;
        to_return->imageData[i+1] =tempG;
        to_return->imageData[i+2] = tempB;

    }




    return to_return;
}
EN

回答 1

Stack Overflow用户

发布于 2011-03-17 01:55:48

这是你给cvSetImageData的电话。当您调用cvCreateImage时,它同时分配头部和图像数据。cvCreateImageHeader仅分配图像标头。

当您调用cvSetImageData时,它不会将数据复制到结构中。相反,它只是将指针设置为指向您提供的任何数据。因此,如果先调用cvCreateImage,然后调用cvSetImageData,则cvCreateImage分配的图像数据将会丢失。

这样做的一个相当糟糕的副作用是,用户可能会取消对cvReleaseImage的调用,这实际上会尝试释放rep bitmapData中的数据。一种更好的方法是简单地调用cvCreateImage,然后将代表bitmapData中的所有数据复制到其中。

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3298941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档