Normalize Image?


#1

Im in trouble!

I have an image and I need the SUM of the RGB values of all pixels to be 255.

NO: 249/113/7

YES: 255/0/0
YES: 100/100/55

Is there a photoshop plugin, filter or anything that can do this?

Give me help and we´ll :beer:


#2

Someone may know a way, but my friend has PS7 and I’ve just looked for some way of doing what you want to do to no avail.

However, I’m assuming that because this is in the programming section of the site, you’re not afraid to get into the bits and hack together some code to do this, so your best bet is to write a tool to do it (or embed it in your game code or whatever)

You want the sum of all elements to add up to 255, if you think back to how you normalize a vector you should remember something like

length = (xx)+(yy)+(z*z)
x = x / sqrt(length)
y = y / sqrt(length)
z = z / sqrt(length)

which is basically what you want (except all values are *255)

my (incredibly lazy) quick hack way (in C/C++) of doing it would be:

// I assume pImage is declared as a char* to the image data,
// which I also assume is in bgra format

float r, g, b, l;
int idx;
for ( idx=0; idx < numPixelsInImage; idx++ )
{
	// 4 bytes per pixel
	unsigned int offset = idx*4;

	// get the pixel colors, rescale each element to [0,1]
	r = ((float)pImage[offset+4)+2])/255.0f;
	g = ((float)pImage[offset+1])/255.0f;
	b = ((float)pImage[offset])/255.0f;

	// find the length of the color vector
	l = (float)sqrt((r*r)+(g*g)+(b*b));

	// rescale the color vector to between [0,1]
	r /= l;
	g /= l;
	b /= l;

	// put the pixel elements back, remembering to rescale back
	pImage[offset] = (int)(b*255.0f);
	pImage[offset+1] = (int)(g*255.0f);
	pImage[offset+2] = (int)(r*255.0f);
}

now, to any programmers out there currently choking and having a cow over the dodgyness of that code snip, yes I realise it’s not great ;). Optimization is left as an exercise to the reader, I thought it better to present a solution with as much clarity as possible. It also strikes me as possible to do this all in integer operations, with the only real sticking point being the square root, but I reckon this could be done with a lut and some newton raphson shenanigans. Unfortunately I’m too lazy to do that right now :wink:

HTH.


#3

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.