Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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 or color) 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).

Does anyone know how to convert the Gamma function (as shown in the JPG) into C++ code?   Or, alternatively, are you aware of a specific website where I can find some existing code that already contains some Gamma transform C++ code?

Thousand thanks in advance,
EEH
0
ExpExchHelp
Asked:
ExpExchHelp
  • 14
  • 6
2 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
the formula is not complete, you should focus on understanding the formula itself, then traverse all the image with a nested for() loop and apply the formula.
0
 
ExpExchHelpAuthor Commented:
jaimie,

what is missing from the formula.   I don't really understand it.    Since I don't know what it means, I'm not sure what to look for.

EEH
0
 
Jaime OlivaresSoftware ArchitectCommented:
there are missing lots of variables' explanation. But at least you have to understand the formula by yourself
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ExpExchHelpAuthor Commented:
jaimie,

thanks -- I'll keep on studying the formula first.   I'll get back w/ you soon.   I'll leave this post open for a few days, ok.

EEH
0
 
ExpExchHelpAuthor Commented:
jaimie,

before I continue with the Gamma Transform function, I'd like to get more clarification on the CPP functionality below.

Normally, I have an "existing image"... then do some processing w/ it.... and finally create the "new image".

Based on the recommended code (by teacher), I should use the following variables/pointers:
1. image (existing image)
2. GT_image[i]
3. T_image[i]

Also, so far, we have "image" and "imaged" (image delta).   How come that I now have arrays or file pointers?   Should I streamline the code or this the below code correct?

EEH
// Initialize variables
FILE *fpIn;
FILE *fpGT_image;
FILE *fpT_image;
 
// Declares a pointer of this type.  The data will be stored in "*image".
unsigned char *image;
unsigned char *GT_image;
unsigned char *T;
 
 
 
 
// Allocate this many number of bytes to the image.
totalPixels = numberOfRows*numberOfColumns*numberOfBands;
 
// Reads from the specified pointer; 1 byte, # of pixels, and file pointer
image = (unsigned char *) malloc(totalPixels);
l = fread(image,1,totalPixels,fpIn);
fprintf(stderr,"fread returned %d bytes\n", l);
 
 
GT_image = (unsigned char *) malloc(totalPixels);
T = (unsigned char *) malloc(totalPixels);
 
 
 
 
 
// Generate the transfer function T[i] for Gamma here.
for (i=0; i<totalPixels; i++)
{
	GT_image[i]=T[image[i]];
}
 
 
 
 
 
 
//  Statements open up two files specified by argv[2].
fpGT_image = fopen(argv[2],"wb");
 
// Prints and writes the images GT image..
fprintf(fpGT_image,"P5\n%d %d\n255\n", numberOfColumns, numberOfRows);
fwrite(GT_image,totalPixels,1,fpGT_image);

Open in new window

0
 
Jaime OlivaresSoftware ArchitectCommented:
Besides the processing issue, the malloc() and for() loop to copy are well.
But you have to decide if doing the gamma transformation before of after the scale transformation.
How are you plannig to do it?
0
 
ExpExchHelpAuthor Commented:
Generally, in all previous programs we did the "process" AFTER the scaling.    This raises the next question.    I've done scaling before with a double array.   'Don't know how to do it in this case.   As a matter of fact, I wasn't even aware that scaling is required for this GammaTransform.   But, if you recommend it, then I'm sure it's the right way to do it.

So, instead of dealing with the Gamma Transform:
(L-1) * (POW(gee/(L-1), 1/gamma))

I think I now need to deal with scaling first, right?

EEH
0
 
Jaime OlivaresSoftware ArchitectCommented:
I think will be easier to scale first and make the gamma task after.
0
 
ExpExchHelpAuthor Commented:
Following up on my previous post, this is how I handled "scaling' when we using double arrays.

I'm not sure how to transform it into the current code using single array.

EEH
	// Determine maximum g value
	for (i=0;i<256;i++) {
		if(cnt1[i]>maximum) {
			maximum=cnt1[i];
		}
	}
 
	// Resizes the graphic to fit to an optimal scale
	scalefactor=256.0/maximum;
 
	for(i=0;i<256;i++) {
		cnt2[i]=scalefactor*cnt1[i];
	}

Open in new window

0
 
ExpExchHelpAuthor Commented:
Agree... any pointers on how I should transform my double-array scaling into single array scaling?
0
 
ExpExchHelpAuthor Commented:
Does the below look right to you?
	int maximum=-1;
	float scalefactor;
	int x;
 
	// Determine maximum g value
	for (i=0;i<256;i++) {
		if(numberOfColumns>maximum) {
			maximum=numberOfColumns;
		}
	}
 
	// Resizes the graphic to fit to an optimal scale
	scalefactor=256.0/maximum;
 
	for(i=0;i<256;i++) {
		x=scalefactor*numberOfColumns;
	}

Open in new window

0
 
Jaime OlivaresSoftware ArchitectCommented:
what do you mean with single-array and double-array?
0
 
ExpExchHelpAuthor Commented:
Based on your recommendation, I need to do some scaling.   Based on the code below, how do I scale the image?

EEH
// Generate the transfer function T[i] for Gamma here.
for (i=0; i<totalPixels; i++)
{
	GT_image[i]=T[image[i]];
}

Open in new window

0
 
Jaime OlivaresSoftware ArchitectCommented:
you have an algorithm for scaling, unless you are an ExpExcHelp clone.
0
 
ExpExchHelpAuthor Commented:
LOL... no clone here... still the "original" ExpExchHelp... 8)

As listed previously, can could confirm that below scaling algorith is correct?   If not, what you're referring to.    As far as I know, no scaling was used in "Interpolation" (the problem you helped me with previously).
	int maximum=-1;
	float scalefactor;
	int x;
 
	// Determine maximum g value
	for (i=0;i<256;i++) {
		if(numberOfColumns>maximum) {
			maximum=numberOfColumns;
		}
	}
 
	// Resizes the graphic to fit to an optimal scale
	scalefactor=256.0/maximum;
 
	for(i=0;i<256;i++) {
		x=scalefactor*numberOfColumns;
	}

Open in new window

0
 
ExpExchHelpAuthor Commented:
Ok, per instructor, NO SCALING is required.

I'm moving on to the Gamma Transformation function then.

I hope I could continue to "bug" you w/ questions.

EEH
0
 
ExpExchHelpAuthor Commented:
jaime,

during further reading, I've come across this comment.

"Gamma produces a 0-255 output for a 0-255 input.  It does not need to be scaled."

EEH
0
 
ExpExchHelpAuthor Commented:
jaime,

this is my first cut at the Gamma Transformation equation.   Any feedback?

imaged = totalPixels*(image[i]/totalPixels) ^ (1/gamma)

Again, equation is shown in JPG:
http://img118.imageshack.us/img118/8346/gammatransformsd5.jpg
0
 
ExpExchHelpAuthor Commented:
jaimie,

this thread is growing very long.  

I'll close this one... will award you the points for the previous help.   I'll open up a new question... I'd certainly welcome your helping me out on this one again.

EEH

0
 
ExpExchHelpAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 14
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now