Link to home
Start Free TrialLog in
Avatar of ExpExchHelp
ExpExchHelpFlag for United States of America

asked on

C++ Function

I've posted an algorithm for a Gamma Transform function at the following site:
http://img118.imageshack.us/img118/8346/gammatransformsd5.jpg

I have a CPP program that works in 3 parts:
1. read an image (grayscale) into the the program
2. "do some process"... (in this case, I'm trying to implement the Gamma transform function)
3. output the new image (based on modification in step #2).


I've tried two different approaches (see below).  Approach #1 results in a uniformly black image.  Approach #2 results in a uniformly dark-gray image.

Does anyone know if my interpretation of the equation is correct?

Thousand thanks in advance,
EEH
Approach #1:
newImage[i]=(pow(totalPixels*(image[i]/totalPixels),1/2.5));
 
Approach #2
newImage[i]=(pow(totalPixels-1*(image[i]/totalPixels-1),1/2.5));

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ExpExchHelp

ASKER

jkr,

wow... it amazingly works.   I didn't know that breaking it into pieces could make such a difference.   BTW, what does the variable 'lmo" stand for?

Finally, I compared my two images with the those listed on the original JPG post.   I'm comparing the left and right images with the new ones.

The new one looks a bit different the image on the right.   Essentially, the right image (original post has much less contrast) compared to the new image.    

Please compare:
http://img118.imageshack.us/img118/8346/gammatransformsd5.jpg

with
http://img132.imageshack.us/img132/8950/randpolytm8.jpg
'lmo' is  'L - 1' ;o)

Well, that depends on the 'lambda' used in the function, you used 2.5, they probably used some other value. I'd play with that a little.
L minus one... got it... feel quite dumb.   8))

Yes, I think you're right... I now used 0.9 for "gamma".    It is now much darker.   It almost looks like the one in the middle.

But overall, the code appears to work fine.   I'll leave this thread open for a bit longer.   However, I'm already very confident that I found the solution... thanks to your help.

EEH
Well, you had the 'L - 1' as a factor inside the 'pow()', that was the problem.
jkr,

again, thanks so much.   This solution works for me.

Take care,
EEH
jkr,

one final question:

Instead of only processing PGM (grayscale images), I also would like to process PPM (color) images:

I've included a 2nd For Statement as shown below:    The processed color PPM file (new image) is only gray though.

Am I missing something obvious?

EEH

************************************************

int numberOfRows, numberOfColumns, numberOfBands, totalPixels;
      
      
// Allocate this many number of bytes to the image.
totalPixels = numberOfRows*numberOfColumns*numberOfBands;    
   
   
// Generate the transfer function T[i]
for (i=0; i<totalPixels; i++){
      for (int b=0; b<numberOfBands; b++)
            GT_image[i] = LmO*(pow((image[i]/LmO),oneDividedByGamma));
}
You are applying the same transformation to the same pixel multiple times?
In another problem, I used the following approach.   I thought I could utilize the same approach for the Gamma Transform function.

Is this NOT the case?

******* Approach in different problem ********
// Also, the nested ForLoop(s) must include "Bands" in order to process PPM images.
for (i=0; i<numberOfColumns; i++){
      for (j=0; j<numberOfRows; j++) {
            for (p=0; p<Fm; p++){
                  for (q=0; q<Fn; q++) {
                        for (int b=0; b<numberOfBands; b++)  
                              imaged[((i*Fm+p)*(numberOfColumns)+(j*Fn+q))*numberOfBands+b] = image[(i*numberOfColumns+j)*numberOfBands+b];
                  }
            }
      )
}
Actually, that's hard to tell from that snipped without knowing the big picture. And I am not really sound in image processing ;o)
Thanks, jkr.   I appreciate your previous help.