泄漏在OpenCV NSImage到IplImage转换器过程 [英] Leak in OpenCV NSImage To IplImage converter procedure

查看:288
本文介绍了泄漏在OpenCV NSImage到IplImage转换器过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中有一个泄漏。当我使用cvCreateImage而不是cvCreateImageHeader它是304Kb和107b泄漏,但是当我改变它成为只有107位。
可以帮我找到泄漏。

I have a leak in the code which I put below. When I used cvCreateImage instead of cvCreateImageHeader it was 304Kb and 107b leak but when I changed it became to be 107 bites only. Could You help me to find leak.

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


推荐答案

这是对cvSetImageData的调用。当您调用cvCreateImage时,它分配头和图像数据。 cvCreateImageHeader只分配图像头。

It's your call to cvSetImageData. When you call cvCreateImage, it allocates both the header and the image data. cvCreateImageHeader only allocates the image header.

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

When you call off to cvSetImageData, it does NOT copy the data into the structure. Rather it simply sets the pointer to point at whatever data you've supplied. So if you call cvCreateImage and then cvSetImageData, the image data allocated by cvCreateImage is lost.

这样做的一个相当讨厌的副作用是,用户可能会调用cvReleaseImage,这实际上会尝试释放[rep bitmapData]中的数据。一个更好的方法是简单地调用cvCreateImage,然后将所有的数据从[rep bitmapData]复制到它。

A rather nasty side-effect of the way you're doing this, is that the user could potentially call off to cvReleaseImage, which would in fact try to free the data in [rep bitmapData]. A much better way would be to simply call cvCreateImage, and then copy all the data from [rep bitmapData] into it.

希望这有助于。

这篇关于泄漏在OpenCV NSImage到IplImage转换器过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆