- Home /
How to blur an image in memory?
Is there a way to blur an image in memory? What I'm doing is capturing the screen, and I'm trying to blur this image, in order to overlay it for a pause screen. The image is only going to be blurred once when pause is hit, not once per frame.
I've tried applying a gaussian blur algorithm but it's very slow. Is there a better way to do this? For example, any libraries or addons I can use? If not, does anyone know of the best way to do this?
You just need to optimize your algorithm; there's no reason why it should be slow. You can do fast, real-time image blurs in web Javascript, so in Unity it should be even faster.
That doesn't exactly help me, at all. I've tried multiple different iterations and it's still extremely slow with processing image information. If you know of any solutions or the best practices for doing so, I'd love to hear.
I posted that as a comment rather than an answer for a reason, but you can just google for "fast blur algorithm" or similar.
Your problem may be in how you're accessing the image information - Are you using GetPixel or GetPixels, and SetPixel or SetPixels? Ideally you want to pull the image information into a large array, apply your filter, and then call SetPixels once to apply the change. If you work with Color32 ins$$anonymous$$d of Color it should be even faster.
Ah, I didn't realize SetPixel and GetPixel had much overhead. I'll try this out and post my findings. Thanks! :)
Answer by tonycoculuzzi · Jun 13, 2012 at 10:14 PM
To answer my own question:
I got it working by using GetPixels to get all the pixels into a single array. I then turned that array into a 2D array based on the height and width of the input image.
I then processed this array, instead of the image. I found that using GetPixel every iteration had HUGE amounts of overhead, as it was grabbing the info from the GPU every single time. Grabbing the info once and storing it in a 2D GREATLY improved speed.
I then reversed the 2D array into a single dimension array, and used SetImage once, after the loop, to set the image contents. Works great, though going over 20 tends to crash the editor.
Great, glad you got it working! I'd definitely suggest trying to modify your blurring algorithm to work with a 1D array rather than copying the bits - ins$$anonymous$$d of using [x][y], you can just as easily use [x + y * width]. The reason for this is that $$anonymous$$ono / .NET is not always good about releasing large chunks of continuous memory when you're done with them - plus it's a potentially unnecessary overhead.
Your answer
Follow this Question
Related Questions
Stippled Shader? 2 Answers
Creating a Blur Effect on GUITexture 0 Answers
What is the best way to make clouds with png images? 1 Answer
Modifying an image effect component over time. 0 Answers
Add Motion Blur To Prefab - HELP 2 Answers