Recoloring an image

I’m looking to recolor an image using OpenGL. So I’d like to be able to perform this kind of recoloring:

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.

Can be achieved by modifying toon shading technique under shader programming.

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?).

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.

Better by what metric? :wink:
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 :wink: :wink:

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).