Solved

C++ Function

Posted on 2007-11-29
20
285 Views
Last Modified: 2008-02-01
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
Comment
Question by:ExpExchHelp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 6
20 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20377157
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
 

Author Comment

by:ExpExchHelp
ID: 20377924
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20377982
there are missing lots of variables' explanation. But at least you have to understand the formula by yourself
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ExpExchHelp
ID: 20378278
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
 

Author Comment

by:ExpExchHelp
ID: 20388790
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
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 500 total points
ID: 20388812
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
 

Author Comment

by:ExpExchHelp
ID: 20388868
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20388875
I think will be easier to scale first and make the gamma task after.
0
 

Author Comment

by:ExpExchHelp
ID: 20388882
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
 

Author Comment

by:ExpExchHelp
ID: 20388898
Agree... any pointers on how I should transform my double-array scaling into single array scaling?
0
 

Author Comment

by:ExpExchHelp
ID: 20388913
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20388966
what do you mean with single-array and double-array?
0
 

Author Comment

by:ExpExchHelp
ID: 20388989
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20389020
you have an algorithm for scaling, unless you are an ExpExcHelp clone.
0
 

Author Comment

by:ExpExchHelp
ID: 20389033
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
 

Author Comment

by:ExpExchHelp
ID: 20389118
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
 

Author Comment

by:ExpExchHelp
ID: 20389143
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
 

Author Comment

by:ExpExchHelp
ID: 20389190
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
 

Author Comment

by:ExpExchHelp
ID: 20389612
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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question