tags:

views:

65

answers:

3

i have found this formula and its description on a site

l(x,y)=(l(x,y)-min)(no of intensity levels/(max-min)) + initial intensity level

where, I( x,y ) represents the images, on the left side it represents the output image, while on the right side it represents the xth pixel in the yth column in the input image. In this equation the "min" and "max" are the minimum intensity value and the minimum intensity value in the current image. Here "no. of intensity levels" shows the total number of intensity values that can be assigned to a pixel. For example, normally in the gray-level images, the lowest possible intensity is 0, and the highest intensity value is 255. Thus "no. of intensity levels" is equal to 255.

i have converted it into code, have i correctly converted it?

for(int y = 0; y < bmp.bmHeight; y++)   
  {      
      for(int x = 0; x < bmp.bmWidth; x++)     
      {         
          COLORREF rgb = dc.GetPixel(x, y);     
          BYTE r = GetRValue(rgb);       
          BYTE g = GetGValue(rgb);     
          BYTE b = GetBValue(rgb);     
          BYTE p,p1;
          if(r<g)
          {
              p=r;
              p1=g;
          }
          else
          {
              p=g;
              p1=r;
          }
          if(b<p)
              p=b;
          if(b>p1)
              p1=b;
          BYTE bw = (RGB(r, g, b)-p);
          bw=bw*(255/(p1-p));
          bw+=p1;
          dc.SetPixel(x, y, RGB(bw, bw, bw));      
      }   
A: 

You need to make two passes through the image. First pass: find the max and min level of the image. Second pass: use the max and min values to do the contrast stretching.

Paul R
p is min and p1 is max. why r u saying that number of intensity levels is (max - min + 1)?
Sweety Khan
Re-read what I wrote: you need to make **TWO** passes, one to calculate min and max **for the whole image** and then a second pass to apply the contrast stretching. BTW, for RGB you will probably to convert RGB to HSV, apply contrast stretching to the **intensity**, then convert back to RGB.
Paul R
the number of intensity levels is 255 if you want to stretch to the full spectrum though, see: http://homepages.inf.ed.ac.uk/rbf/HIPR2/stretch.htm for the proper formula. It should be clear straight away that (max-min+1)/(max-min) will mostly yield a number just above 1, which is not very useful...
ufotds
yeh finally it gives a grey scale contrast strectched image. thanx to u
Sweety Khan
can we done this for getting a color stretched image?
Sweety Khan
@ufotds: sorry, I didn't look closely enough at the formula - I've now removed the erroneous comment about number of intensity levels
Paul R
@Sweety Khan: yes, you can either cheat and to it the easy way, which is to do the stretching separately for each R, G, B plane (i.e. keep a separate min, max for each plane), or you can do it properly and convert your RGB image to HSV, do the stretching on the intensity, and then convert back to RGB.
Paul R
i dont know how to convert RGB image into HSV :(grey contrast stretching is done but still color contrast stretching is not done.
Sweety Khan
+1  A: 

You need a separate loop to compute p and p1 (horrible variable names by the way) on a whole image basis before you have the loop for the main contrast stretching algorithm.

Also, in the example you give there are 256 intensity levels, not 255. 0 is an intensity level too.

Lastly, you should probably either convert the image to HSV before running the algorithm and only run it on the V values, or run it separately for R, G and B. Picking the minimum and maximum intensity the way you are is not likely to yield useful results for a color picture.

Omnifarious
suggest a good variable name. and plz tell me what is this error. i m fed up of it. it usually comes and sometimes remove automatically.LINK : fatal error LNK1168: cannot open C:\Documents and Settings\FAIZI \My Documents\Visual Studio 2008\Projects\contrast\Debug\contrast.exe for writing
Sweety Khan
variable names: "min", "max" on the error, make sure the program isn't running anymore when you compile
ufotds
ok but still something goes wrong output is not right. this is my formula part check this BYTE bw = (RGB(r,g,b)-min);bw=bw*(255/(max-min));bw+=max;dc.SetPixel(x, y, RGB(bw, bw, bw));one thing more this is the formula for grey scale contrast strectching
Sweety Khan
that's why you convert to an intensity level first, apply formula and then change your rgb signal accordingly... You cannot apply this formula directly to rgb
ufotds
is there any direct method? i mean can i got a color contrast stretched image without converting it into grey scale?
Sweety Khan
yeh finally it gives a grey scale contrast strectched image. thanx to u
Sweety Khan
can this contrast or stretching increases? if it can, how?
Sweety Khan
@Sweety, int the code posted in my answer it stretches to the full 255 levels of intensity. You will already lose some information of you image. In theory you could stretch it further (by putting OUT_MIN and OUT_MAX further apart) and lose more shades in both ends of the spectrum. Otherwise, look on the wikipedia page on HSV for alternatives of calculating luminosity, which might give a better result. The intensity calculation I used is the most basic one.
ufotds
+2  A: 

This is my attempt to code the formula. Based on the formula from Fisher et al. Note that you will still run into artificial looking images unless you use the cutoff fraction described in Fisher's article, but that is your own homework ;-)

I didn't test this with real images, debugging it is also your homework.

Reference: For calculation of the intensity, see HSL and HSV on wikipedia.

typedef unsigned char byte;

const byte OUT_MIN  = 0;   // The desired min output luminosity 0   to stretch to entire spectrum
const byte OUT_MAX  = 255; // The desired max output luminosity 255 to stretch to entire spectrum

byte min = 255, max = 0;

// get the minimum and maximum values.
for( int y = 0; y < bmp.bmHeight; y++ )
{      
   for( int x = 0; x < bmp.bmWidth; x++ )     
   {  
       COLORREF rgb       = dc.GetPixel(x, y);   
       byte     intensity = ( rValue(rgb) + gValue(rgb) + bValue(rgb) ) / 3;

       min = min < intensity ? min : intensity;
       max = max > intensity ? max : intensity;
   }
}     

// set the new pixel values
for(int y = 0; y < bmp.bmHeight; y++)   
{      
   for(int x = 0; x < bmp.bmWidth; x++)     
   {  
      COLORREF rgb = dc.GetPixel(x, y);

     // Creates color image
     // Calculate new color using the formula. Has to be int, not byte, because you might go out of range.
     // We correct the range manually on the next two lines. 

     int r = ( rValue( rgb ) - min ) * ( OUT_MAX / (max - min) ) + OUT_MIN;  
     r = r < 0   ? 0   : r;  
     r = r > 255 ? 255 : r;

     int g = ( gValue( rgb ) - min ) * ( OUT_MAX / (max - min) ) + OUT_MIN;  
     g = g < 0   ? 0   : g;  
     g = g > 255 ? 255 : g;

     int b = ( bValue( rgb ) - min ) * ( OUT_MAX / (max - min) ) + OUT_MIN;  
     b = b < 0   ? 0   : b;  
     b = b > 255 ? 255 : b;

     dc.SetPixel( x, y, RGB( r, g, b ) );    
   }   
}
ufotds
this is for getting a color contrast stretched image???
Sweety Khan
well, i propose you work that out since i spent more than an hour writing this code for you, greets
ufotds
how sweet of u! thank u so much. i m trying it.
Sweety Khan