PDA

View Full Version : Recoloring an image



SullySwede
01-17-2012, 03:43 PM
I'm looking to recolor an image using OpenGL. So I'd like to be able to perform this kind of recoloring:

http://i.stack.imgur.com/yo9bg.png

I've tried searching on Google and on this forum for an answer - but to no avail. Ideally, the solution needs to be as efficient as possible.

awhig
01-17-2012, 04:23 PM
Can be achieved by modifying toon shading technique under shader programming.

carsten neumann
01-17-2012, 04:27 PM
OpenGL is not an image processing API, so without knowing more details it does not seem overly well suited for your problem.

You could upload your input image as a texture to the GPU, render a single quad with that texture using a shader that performs the color transformation and download back to CPU memory. I'm not sure that will be much faster (and quite possibly slower) than simply writing a loop over the image that performs the transformation (you only seem to be setting red and green channels to zero and preserve blue?).

SullySwede
01-17-2012, 04:44 PM
Yeah, I had considered simply iterating over every pixel and adjusting its colour in the texture before rendering but wondered if there was a better method.

carsten neumann
01-17-2012, 05:02 PM
Better by what metric? ;)
If performance: use SSE instructions, if the image exceeds a certain size, split into disjoint blocks and spawn threads to process the blocks.
For large images there may actually be some benefit to offloading the work to the GPU, because it frees the CPU to do other stuff. Of course that assumes the GPU is idle and you actually have other computations to keep the CPU busy.

If your metric is ease of maintenance and/or you only do this operation occasionally (or on small data sets), in other words this is not your application's bottleneck: write the loop and move on to more interesting problems ;) ;)

awhig
01-18-2012, 12:53 PM
Yeah, I had considered simply iterating over every pixel and adjusting its colour in the texture before rendering but wondered if there was a better method.

No need to write a loop. In fragment shader just check the value of pixel and recolor it on the basis of some threshold function.In short Toon shading.The shader will take care of processing each pixel.

You need to add a loop if you want to do some processing inside the shader.

Kopelrativ
01-18-2012, 11:28 PM
No need to write a loop. In fragment shader just check the value of pixel and recolor it on the basis of some threshold function.In short Toon shading.The shader will take care of processing each pixel.

You need to add a loop if you want to do some processing inside the shader.

That is, the shader will automatically loop over every pixel. This will give you a very high performance, maybe the best you can get. If, by performance, you mean the time it takes to recolor the image.

You didn't state if you want to present the result graphically, or if you just want to save it on a file.

The disadvantage of this method is that you only have access to one pixel at a time. If you need a more advanced algorithm, with access to other near pixels, then it is more complicated (but still possible).