Re: How to determine inverse color?
Posted by Gabriel Landini on Jan 07, 2006; 5:09pm
URL: http://imagej.273.s1.nabble.com/How-to-determine-inverse-color-tp3704104p3704108.html
> the problem is this: if you
> want to draw markers in an image, we want these markers to have
> optimal contrast with the background.
One can use the bitwise XOR of the intensity. One advantage is that applying
it twice restores the original intensity. One problem is that near the middle
of the intensity space the contrast vanishes.
What about shifting the colour space by half a cycle? Although the contrast
for a greyscale image is never the maximum (it is half of it), the contrast
jump is maintained constant for all intensity values.
If "i" is the intensity and there are 256 possible values, then the shifted
value should be ((i+128)%256) where "%" is the "remainder" operator.
This would be ideal for a circular colour space, like the Hue channel in HSB
(since the result is the furthest away). However for RGB it has the
disadvantage of jumping from one extreme to the other for 2 consecutive
values at the middle of the colour space.
Wayne once posted a macro called DrawXORLine.txt to show how to draw XOR
lines.
I have changed only 1 line to draw a contrasted line with the suggestion
above.
I hope it helps.
Gabriel
//--------------8<-----------------
if (nImages==0)
newImage("Test","8-bit Ramp",512,512,1);
r = minOf(getWidth, getHeight)/2;
x1=getWidth/2; y1=getHeight/2;
for (a=0; a<4*PI; a+=PI/100) {
x2 = r*sin(a) + x1;
y2 = r*cos(a) + y1;
drawXORLine(x1,y1,x2,y2);
wait(35);
drawXORLine(x1,y1,x2,y2);
}
function drawXORLine(x1, y1, x2, y2) {
if (bitDepth==8)
value = 255;
else if (bitDepth==16)
value = 65535;
else
value = -1;
autoUpdate(false);
dx = x2-x1;
dy = y2-y1;
if (abs(dy)>abs(dx))
n = abs(dy); else n = abs(dx);
n++;
xinc = dx/n;
yinc = dy/n;
do {
//putPixel(x1, y1, value^getPixel(x1, y1)); // XOR line
putPixel(x1, y1, ((getPixel(x1, y1)+128)%256)); // new line
x1 += xinc;
y1 += yinc;
n--;
} while (n>0);
updateDisplay();
}
//--------------8<-----------------