Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set same (constant) hue value for every pixel's of an image by using ColorMatrix?

I am trying to set a constant hue value for entire image with using ColorMatrix. My goal is to make entire image look with same color without loosing brightness of any area. I found a way to shift the hue values of an image by using ColorMatrix but i couldn't find any way to set same hue value for all pixels. I can do it with iterating every pixel of image but this approach is too slow. I am not sure if it is possible to do it with ColorMatrix and i am open to possible solutions other than ColorMatrix approach.

Input Image

Hue shifting output Image*

Desired output Image**

*This can be done with color matrix

** I can do this with iterating pixels but not with ColorMatrix

PS: I am trying to do this on Android but i believe the question is not directly related to the android since ColorMatrix approach is common on other platforms like Flash, C# etc.

like image 336
Tony Avatar asked Mar 23 '11 14:03

Tony


4 Answers

not really familiar here, but i belive this link can help: http://www.graficaobscura.com/matrix/index.html it's c code, so you have to translate C -> ColorMatrix, but in the last paragraph there is the operation

Hue Rotation While Preserving Luminance

which seems what you are looking for.

like image 76
andijcr Avatar answered Oct 22 '22 14:10

andijcr


Check out QColorMatrix, which has a ColorMatrix routine called RotateHue. Source is in C++, but is portable to other languages (I've ported part of it to .NET in the past and it worked great).

like image 32
Todd Main Avatar answered Sep 20 '22 05:09

Todd Main


I don't think there is a way to do exactly what you're asking for with a ColorMatrix. It sounds like what you want to do is transform from RGB to HSL color space, set H constant across all pixels, and transform back from HSL to RGB. These color space transformations aren't linear, and can't be represented with matrices. Because of the different way these spaces parameterize color, I also suspect some degradation could occur doing RGB->HSL->RGB.

I think the closest you would be able to achieve with the ColorMatrix is by using one to convert to greyscale, and another to weight the RGB values (tint them). This kind of thing is often used to do fake sepia tone photos, but it is not what you are asking for.

like image 2
Adam Smith Avatar answered Oct 22 '22 14:10

Adam Smith


I have made a quick sample in Flash (this question is tagged as ActionScript), not sure if this is what you are looking for:

http://megaswf.com/serve/1047061

The code:

import com.greensock.*; 
import com.greensock.easing.*;
import flash.events.MouseEvent;

colorButton.addEventListener(MouseEvent.CLICK, onColor);
resetButton.addEventListener(MouseEvent.CLICK, onReset);

function onColor(e:MouseEvent):void {
    TweenMax.to(mc, 1, {colorMatrixFilter:{colorize:0x0099ff, amount:1}});
}

function onReset(e:MouseEvent):void {
    TweenMax.to(mc, 1, {colorMatrixFilter:{colorize:0x0099ff, amount:0}});
}
like image 2
Chris Avatar answered Oct 22 '22 13:10

Chris