Color mixing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Color mixing

Medardas
Hello,

I'm trying to understand and write a simple macro(macro is mainly based on maxmax example) to calculate green NDVI(Normalized Difference Vegetation Index). The thing is that all I have achieved now  is to use two colors to represent positive(green) and negative(blue) NDVI.
What I want to do is to include red color to represent lower positive NDVI. Code is here:

NDVI_Plant_Max = 1;      
NDVI_Not_A_Plant = -1;  

ScaledNDVI = 0;
  w = getWidth();            
  h = getHeight();            
  for (y=0; y<h; y++)                                              
     {
      for (x=0; x<w; x++)
        {  
           oldpixel = getPixel(x,y);                                  
           red =   (oldpixel & 0xff0000)>>16;                          
           green = (oldpixel & 0x00ff00)>>8;
           blue =  (oldpixel & 0x0000ff);

           NDVI = (red-green)/(red+green);

           if (NDVI < 0)                          
           {
              ScaledNDVI = NDVI/(-1*NDVI_Not_A_Plant);                
              if (ScaledNDVI < -1) ScaledNDVI = -1;                  
              blue= floor(ScaledNDVI*-255);                            
              red = 0;
              green = 0;
             

           }
           else                                                        
              {
              ScaledNDVI = NDVI/NDVI_Plant_Max;                        
              if (ScaledNDVI > 1) ScaledNDVI = 1;
                      green =floor(ScaledNDVI*255);                        
                      blue = 0;
                      red =  0;                      

              }

        red =  ((red & 0xff)<<16) ;
        green = ((green & 0xff)<<8) ;
        blue = (blue & 0xff);
           newpixel = red + green + blue ;
           putPixel(x, y, newpixel);
        }
     }

As I understand, green and blue color values are determined with decimal value(0-255) being manipulated and then parsed with masking and shifting to form a pixel.
I did try to use whole value(red : 65536-16711680, green: 256-65280, blue: 0-255) to multiply by ScaledNDVI  and when add result to the picture, but it turned out to be nonsence.

what I want to have is here

what I have instead is more like here

I guess what I need is to add red color value when determining positive and negative NDVI's in IF's conditional statement, but since color expression there is 0-255 for every color, I can't define other colors than green in positive or blue in negative. If I do, the result is either grey because all three colors have same decimal representation or yellow'ish and blue'ish because of other two colors being same value.

Can somebody give me a hint how should I restructure the macro to get desirable effect?
Reply | Threaded
Open this post in threaded view
|

Re: Color mixing

Michael Schmid
Hi Medaras,

probably it would be easier to create an 8-bit image with the NDVI (e.g. 0 corresponding to the most negative value, 100 to NDVI=0 and 255 to the most positive value).

Than use the LUT editor plugin (from the ImageJ web site) to create an appropriate lookup table. You need not specify each of the colors individually, you can select a range on the mouse and just define the colors of its end points, e.g. blue for 0 and black for 100.
For the positive values, create a LUT from black to red, then to green.

Beware that red and green cannot be correctly distinguished by about 8% of males (in Europe), so use Gabriel Landini's Dichromacy plugin or a similar plugin to try whether your LUT is also useful in case of Protanope or Deuteranope vision.

Michael
________________________________________________________________

On Jul 24, 2012, at 21:53, Medardas wrote:

> Hello,
>
> I'm trying to understand and write a simple macro(macro is mainly based on
> maxmax example) to calculate green NDVI(Normalized Difference Vegetation
> Index). The thing is that all I have achieved now  is to use two colors to
> represent positive(green) and negative(blue) NDVI.
> What I want to do is to include red color to represent lower positive NDVI.
> Code is here:
>
> NDVI_Plant_Max = 1;      
> NDVI_Not_A_Plant = -1;  
>
> ScaledNDVI = 0;
>  w = getWidth();            
>  h = getHeight();            
>  for (y=0; y<h; y++)                                              
>     {
>      for (x=0; x&lt;w; x++)
>        {  
>           oldpixel = getPixel(x,y);                                  
>           red =   (oldpixel &amp; 0xff0000)>>16;                          
>           green = (oldpixel & 0x00ff00)>>8;
>           blue =  (oldpixel & 0x0000ff);
>
>           NDVI = (red-green)/(red+green);
>
>           if (NDVI < 0)                          
>           {
>              ScaledNDVI = NDVI/(-1*NDVI_Not_A_Plant);                
>              if (ScaledNDVI < -1) ScaledNDVI = -1;                  
>              blue= floor(ScaledNDVI*-255);                            
>              red = 0;
>              green = 0;
>
>
>           }
>           else                                                        
>              {
>              ScaledNDVI = NDVI/NDVI_Plant_Max;                        
>              if (ScaledNDVI > 1) ScaledNDVI = 1;
>                      green =floor(ScaledNDVI*255);                        
>                      blue = 0;
>                      red =  0;                      
>
>              }
>
>        red =  ((red & 0xff)<<16) ;
>        green = ((green & 0xff)<<8) ;
>        blue = (blue & 0xff);
>           newpixel = red + green + blue ;
>           putPixel(x, y, newpixel);
>        }
>     }
>
> As I understand, green and blue color values are determined with decimal
> value(0-255) being manipulated and then parsed with masking and shifting to
> form a pixel.
> I did try to use whole value(red : 65536-16711680, green: 256-65280, blue:
> 0-255) to multiply by ScaledNDVI  and when add result to the picture, but it
> turned out to be nonsence.
>
> what I want to have is here
>
> what I have instead is more like here
>
> I guess what I need is to add red color value when determining positive and
> negative NDVI's in IF's conditional statement, but since color expression
> there is 0-255 for every color, I can't define other colors than green in
> positive or blue in negative. If I do, the result is either grey because all
> three colors have same decimal representation or yellow'ish and blue'ish
> because of other two colors being same value.
>
> Can somebody give me a hint how should I restructure the macro to get
> desirable effect?
>
>
>
> --
> View this message in context: http://imagej.1557.n6.nabble.com/Color-mixing-tp4999560.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Color mixing

Medardas
Michael Schmid-3 wrote
create an 8-bit image with the NDVI
Sorry, I dont understand this part. What do you mean to create 8-bit image with NDVI?