Domanda

I am using this function here in my ios app:

cv::cvtColor(image, image, cv::COLOR_BGR2RGB);

But when I call this in my - (void)processImage:(cv::Mat&)image delegate method, images get lost in memory. So after a few seconds my app crashes with memory problems.
Terminated due to Memory Pressure

Don't I just copy the converted image over the previous image?
And what can I do to prevent this behavior?

- (void)processImage:(cv::Mat&)image
{
    cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
}

Some output of how data looks like in inspector (these vm_allocate lines appear a lot):

0 0x20961000 VM: CoreAnimation 00:22.762.010 • 7,91 MB QuartzCore CA::Render::aligned_malloc(unsigned long, void**)
1 0x20178000 VM: CoreAnimation 00:22.415.490 • 7,91 MB QuartzCore CA::Render::aligned_malloc(unsigned long, void**)
2 0x2114a000 CGSImageData 00:22.762.165 • 5,95 MB CoreGraphics CGSImageDataHandleCreate
3 0x1f3a0000 VM: Foundation 00:22.752.743 • 5,93 MB libsystem_kernel.dylib vm_allocate
4 0x1fb89000 VM: Foundation 00:22.408.091 • 5,93 MB libsystem_kernel.dylib vm_allocate

È stato utile?

Soluzione

I usually convert over the original image with no problem. You can create another destination mat image if you wish to preserve the original one. So it's based on case to case basis.

Would rather comment than reply, but my answer would be too long.

Try this methods: 1) High chance is due to you not declaring the channel. For instance CV_8U3, etc..

2) If step one doesn't work, the other high possibility: Try using CV_BGR2RGB instead of cv::COLOR_BGR2RGB (Version compatibility problem)

3) Have you tried removing the pointer? &

If the three methods still doesn't work, please do comment on the exact error message you are receiving on this answer. I will try help you out. Cheers.

EDIT(To answer your comments):

When I was talking about channels, I meant CV_8U3, CV_8U, etc. You don't have to try it anymore though, cause the error is due to the IOS's aggressive kernel thread which sets all to kill all the processes on low memory. This means when background process are running, they are more likely to be "killed" to allocate memory for the current/running/foreground process.

More information about that kernel:http://newosxbook.com/articles/MemoryPressure.html

I am not an expertise when it comes to IOS, but things I think you can try:

1)Use global Variable, for instance, make Mat Image global rather than local

2)A slightly bad programming convention to some: skip the function and just dump the code from the function to the main/or program which was trying to call the function. This ensures that the IOS doesn't need to switch process, hence killing either of them.

3) Define app profile(UIBackgroundMode) in kernel, thereby taming Jetsam, the aggressive kernel killer a little.

4) release images from RAM (remove the reference to them) when there is no more need to the images

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top