Solved

Need to "clean up" CPP file

Posted on 2007-11-30
6
187 Views
Last Modified: 2016-08-29
Hello Experts,

this may be one of "those" strange questions again.     I've been taking a (beginner) C++ class for the for the past 4 months.  

My first homework assignment (Histogram Equalization) ended up to be a decent grade.   However, the program wasn't very efficient and "clean".   Too much "passing one value to another".

I now have the need to re-use the structure of my 1st program for my final assignment.   When looking at the file, there's a bunch of "stuff" in there that could be streamlined.   There's some stuff in there that I can't make sense of any longer (I know, I know.... if I can't make sense of you, how could you.... -- still an experienced programmer may understand what's going on).

I've posted the "framework" CPP at the following location:
https://filedb.experts-exchange.com/incoming/ee-stuff/5913-NeedCleanCPP.zip

All UNNESSARY STUFF should be removed.   The only thing that I need to do is processing an image file and created a 2nd image with "histogram equalizaiton".    I've removed all non-pertaining stuff from the file already (incl. declarations).   So, it pretty much leaves only the functions.  

Once that's done I insert the "read file" and "output file" later on.   I just wanted it out of the way for right now.

Again, this may be one of those "weird questions" again.    Any help would be greatly appreciated so that I can begin working on my final project, using the streamlined CPP as my foundation.

Thanks,
EEH

0
Comment
Question by:ExpExchHelp
  • 3
6 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 20385105
Hi ExpExchHelp,

I tried to follow your link, but it asked me to sign in with my EE account and password.  Odd, since I'm already signed in to EE.

Then I did an nslookup on www.ee-stuff.com and www.experts-exchange.com

They aren't even in the same class A network.  206.171.169.10  &  64.156.132.140  



I'd love to help, but until someone I know verifies that this is an EE related site, I'm not willing to enter my ID and password there.

Kent
0
 
LVL 45

Expert Comment

by:Kdo
ID: 20385263
Hi jkr,

Thanks.  :)


Just because I'm paranoid doesn't mean that they're not out to get me.  
Kent
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 20385325
Hi ExpExchHelp,

The first thing that comes to mind is that even though this is a c++ forum, and the source code may well compile as a C++ program, it's a C program.  I expect everything here to compile with a modern C compiler.

To make this more C++ like, I'd start by defining a class to encapsulate the histogram functions.

THistogram
{
};

Instantiate it in the main function.

  THistogram *histogram;

  histogram = new THistogram;


Note that this histogram uses the 256x256 dimensions in your code.  You might want to "parameterize" this, but that's your call.

The big question is whether you want this histogram to be tailored specifically for the items in your image, or whether the image should be it's own class and/or variables.  My preference would be that it be another class that encapsulates the imaging functions.

TMyImage
{
};

  TMyImage *Image;

  Image = new TMyImage (FILE *f);     //  Or the file name.  All of the I/O should be moved to the class.


Then call the appropriate method(s) for the Image.  When the analysis is done, pass the results to the histogram class.  main() becomes very, very simple.



Good Luck,
Kent
0
 

Author Comment

by:ExpExchHelp
ID: 20386560
Kent,

thank you for your help.   While I truly appreciate your feedback, I need some concrete help with the code listed below.

The code (again, I removed all declarions... otherwise it would have been too lengthy) covers two functions:
1. Histogram equalization
2. Autolevels

My goal is the following:
- I want to remove as much code as possible so that only one image enhancement method works (it'll serve as a baseline for another project then)

- right now, I have way to much "going-back-and-forth" code (e.g. "x=y; y=z; z=a") code in there.   It would be very hard for me to implement the new program into it

- so, if e.g. "cnt[4], cnt[5] (or whatever they may be) could be removed/merged, that would be extremely helpful.

Thank you,
EEH
// *** GENERAL CODE

// ===================================================
 

	// 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);
 
 
 
 
 
 

// *** BEGIN OF HISTOGRAM EQUALIZATION AND AUTO LEVELS

// ===================================================
 
 
 

    // NEED THIS CODE

	// Generate histogram from input file to produce histogram

	for (i = 0; i < 256; i++) {

		cnt1[i] = 0;

	}
 

	for (i=0; i < totalPixels; i++) {

		l = image[i];

		(cnt1[l])++;

	}
 
 
 
 

    // NEED THIS CODE

	// Determine maximum g value

	for (i=0;i<256;i++) {

		if(cnt1[i]>maximum) {

			maximum=cnt1[i];

		}

	}
 
 

	// NEED THIS CODE

	// Resizes the graphic to fit to an optimal scale

	scalefactor=256.0/maximum;

	for(i=0;i<256;i++) {

		cnt2[i]=scalefactor*cnt1[i];

	}
 
 
 
 
 
 
 

    // ?????

	// Get the denominator for obtaining probabilies

	int denominator=0;

	for(int cntr=0; cntr < 256; cntr++)

	{

		denominator += cnt2[cntr];

	}
 
 
 

	// ?????

	// Store the values from cnt2 into cnt3.  These are adjusted by dividing by the denominator.

	// Declare variables.

	float cnt3[256];

	float numval = 0;

	float numval2 = 0;
 
 

	// ?????

	// numval2 is used to convert the int value to a float value

	// numval is used to store the values of numval2 divided by the denominator (probability)

	// Probabilities array is: cnt3
 

	for(int cntr=0; cntr <256; cntr++)

	{

		numval2 = cnt2[cntr];

		numval = (numval2 / denominator);

		cnt3[cntr] = numval;

	}
 

                 
 

    // ?????

	// Histogram equalization (HE) section:

	// ====================================

	// We need to obtain an array for the CDF values.  The total sum must be equal to 1.

	// cnt3 contains probability values.

	// cnt4 will be created.  It equals the sum of the cnt3 values.

	// tempStorage was created in previous assigment (#1).
 

	float cnt4[256];

	float tempStorage2 = cnt3[0];

	cnt4[0] = tempStorage2;

	for(int i =1; i<256;i++)

		{

			tempStorage2 = cnt3[i];

			tempStorage2 += cnt4[i-1];

			cnt4[i] = tempStorage2;
 

		}
 
 

    // ?????

	// Each element in the CDF array must be multiplied by 255.

	// In then is converted into an integer (no rouding function).

	// Achieved by storing the cnt4 float value into the cnt5 array.
 

	int cnt5[256];
 

	for(int i = 0; i<256;i++)

	{

		tempStorage2 = cnt4[i];

		tempStorage2 *= 255;

		cnt5[i] = tempStorage2;

	}
 
 
 
 
 
 
 
 
 
 

	// NEED THIS CODE

	// Sets HE_image to take on the data and size of equalized image.

	HE_image = (unsigned char *)malloc(totalPixels);
 

	// HE conversion routine to modify the image by cnt5 and output it into HE_image.

	for (i=0; i<totalPixels; i++)

	{

		HE_image[i]=cnt5[image[i]];

	}
 
 
 
 

// *** OUTPUT SECTION CODE

// =======================
 
 

	// User feedback

	fprintf(stdout,"\n\n\n\nThe modified image (Gamma Transform) has been generated.\n\n\n\n");
 

	//  Statements open up two files specified by argv[2].

	fpHE_image = fopen(argv[2],"wb");
 

	// Prints and writes the images HE image..

	fprintf(fpHE_image,"P5\n%d %d\n255\n", numberOfColumns, numberOfRows);

	fwrite(HE_image,totalPixels,1,fpHE_image);

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now