Solved

C++ Function

Posted on 2007-11-26
11
1,134 Views
Last Modified: 2008-03-10
I'm trying to convert an "interpolation" function into C++ code.   Unfortunately, I'm not really sure how to do this.

Please check out the JPG posted at:
http://img523.imageshack.us/img523/4840/interpolatioinbd5.jpg

Could anyone pls provide me some pointers as to what's the best approach?

Thanks,
EEH
0
Comment
Question by:ExpExchHelp
  • 7
  • 4
11 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20355616
how do you store the data? do you have an array? some function to fill in?
0
 

Author Comment

by:ExpExchHelp
ID: 20355654
jaime_olivares:

so far, I only have the input/output of reading in the image.   I've posted the file at:
https://filedb.experts-exchange.com/incoming/ee-stuff/5834-Interpolation.zip 

In the body, I've marked where the function should go.   Any help would be greatly appreciated.

Thanks,
EEH
0
 

Author Comment

by:ExpExchHelp
ID: 20355689
Also, beyond the sample C++ posted above, the following JPG may provide more insights (I hope so).

http://img263.imageshack.us/img263/7990/interpolation2tp1.jpg
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20355738
Ok, I have post your code below.
I see where you create the image:
      // 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);

and where you ask for the factors:
      // User must enter interpolation factor Fm.
      cout << "\n\nEnter interpolation factor Fm.  The value must be an integer:  ";
      cin >> Fm;      
      // User must enter interpolation factor Fn.
      cout << "\n\nEnter interpolation factor Fn.  The value must be an integer:  ";
      cin >> Fn;      

So, you need to reserve memory for the new image:
      // Allocate this many number of bytes to the image.
      int newTotalPixels = (int)(numberOfRows*fm)*(int)(numberOfColumns*fn)*numberOfBands;
      unsigned char *newImage = (unsigned char *) malloc(newTotalPixels);

and  create a function like:
               void Interpolate(unsigned char *image, int width, int height, int bands, double fm, double fn, char *newImage);

So, inside this function, scan for all fixels in 'image' in the x and y axis with a nested for() loop.
for each pixel in the original image, save a pixel in the new image, as explained in the formula.
I don't see the utility of p and q, assume to be 0.

// Standard libraries
#include <stdio.h>                          // This header file defines the I/O functions
#include <iostream>                         // This header file defines the IO-stream functions
#include <stdlib.h>                         // This header file includes standard definitions like exit()
#include <math.h>                           // This header file includes math functions
#include <conio.h>                          // This header file include console input/output
 
 
// These #define statements allow to convert numerical values into string values.
#define FALSE 0
#define TRUE 1
#define DBL_SIZE sizeof(double)
#define CMPLX_SIZE 16
 
 
// The main function has always access to the programs.
// argc = counter; argv = array of strings.
int main(int argc, char **argv){
 
 
	// Initialize variables:
	// Existing variables (previous assignment)
 
	FILE *fpIn;
	FILE *fpOut;
	FILE *fresults;
 
	char *string;
	char c;
 
	int highVal;
	int doneReading = FALSE;
	int numberOfRows, numberOfColumns, numberOfBands;
	int totalPixels;
	int maximum=-1;
	int i, j, l;
 
 
	// New variables (midterm)
	int halfColumns;
	int bnd;
	int iOff, ipOff;
 
	double invMrowMcol;
	double iv;
	double *imaged;
	double *filter;
	double v, w;
	double *shiftedFilter;
 
	// Declares a pointer of this type.  The data will be stored in "*image".       
	unsigned char *image;
 
 
	// The read program is invoked by: readimage inputfile outputfile.
	// Program requires at least 3 arguments:
	// 1. "call program"
	// 2. "input pgm"
	// 3. "output pgm"; the modified image will be written to this outfile file
 
	if (argc < 3) {
		fprintf(stderr,"Usage: readimage input-file-name output-file-name\n");
		exit(0);
	}
	fpIn = fopen(argv[1],"rb");				// argv[1] contains the filename
	if (fpIn == NULL) {
		fprintf(stderr,"%s either cannot be read or does not exist\n", argv[1]);
		exit(-1);
	}
	string = (char *) calloc(256,1);		// initialized dynamic memory allocation
	while (!doneReading && (c = (char) fgetc(fpIn)) != '\0')
	{
		switch(c) {
case 'P':
	c = (char) getc(fpIn);
	switch(c) {
case '2':
	numberOfBands = 1;
	// ppmType = PPMASCII; 
	break;
case '3':
	numberOfBands = 3;
	// ppmType = PPMASCII; 
	break;
case '5':
	numberOfBands = 1;
	// ppmType = PPMGRAY; 
	break;
case '6':
	numberOfBands = 3;
	// ppmType = PPMRAW; 
	break;
	}  
 
	c = (char) getc(fpIn);
	if (c != 0x0A) {
		ungetc(c,fpIn);
	}
	else {
		ungetc(c,fpIn);
		fgets(string,256,fpIn);
	}
	break;
 
case '#':
	fgets(string,256,fpIn);
	break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
	ungetc(c,fpIn);
	fscanf(fpIn,"%d %d %d", &numberOfColumns, &numberOfRows, &highVal);
	doneReading = TRUE;
	fgetc(fpIn);
	break;
 
		}    // End Switch
	}      // End While
 
	// De-allocate memory
	free(string);
 
	// Passes the supplied output-filename (2nd argument) into the feedback (command window)
	char* arg2 = argv[2];
 
	// 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);
 
 
 
 
 
 
 
 
	// ******** Interpolation (begin) ********
 
	// Define variables
	using namespace std;
	double Fm;
	double Fn;
 
 
	// User must enter interpolation factor Fm.
	cout << "\n\nEnter interpolation factor Fm.  The value must be an integer:  ";
	cin >> Fm;      
 
 
	// User must enter interpolation factor Fn.
	cout << "\n\nEnter interpolation factor Fn.  The value must be an integer:  ";
	cin >> Fn;      
 
 
 
	// Interpolation function should go here...
 
 
 
 
	
	// ******** Interpolation (end) ********
 
 
 
 
 
 
 
 
	// ******** Image Output Section ********
 
	//Generate the midterm1.pgm (modified) image
	fpOut = fopen(argv[2],"wb");
 
	// Write image header
	fprintf(fpOut,"P%d\n%d %d\n255\n",numberOfBands>1?6:5,numberOfColumns,numberOfRows);   
    fwrite(image,1,totalPixels,fpOut); 
 
	// Command Window Feedback
	fprintf(stdout,"\n\n\n\n\nAn output image has been created!\n\nThe filename is: %s\n\n\n\n\n", argv[2]);
 
	// Exit routine
	exit(0);   
}    

Open in new window

0
 

Author Comment

by:ExpExchHelp
ID: 20357222
jaime_olivares:

thank you so much for your kind help.   I truly appreciate it.

Ok, previously, I've used the ForLoop shown below (contains old values such as zeta... pls disregard them).

My questions:
- can this approach be used?
- do I need to change all "double" to "int" for e.g. numberOfRows?
- beginning with the line "rho = sqrt((double(v*v+w*w)));", is this where the new function would go?
- do you have any additional pointers as to how I show write the terms for the function?

Thousand thanks in advance,
EEH

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

//Changed to (replaced double with int):
for (i = 0; i < numberOfRows; i++) {
      iOff = i*numberOfColumns;
      v = (i - numberOfRows/2)/(double)numberOfRows;
      for (j = 0; j < numberOfColumns; j++) {
            w = (j - numberOfColumns/2)/(double)numberOfColumns;

                  rho = sqrt((double(v*v+w*w)));
                  double rho_quotient = rho / rho_c;
                  double term1 = exp(-pow(rho_quotient, 2.0));
                  double term2 = zeta * exp(-pow(1.6 * rho_quotient, 2.0));
                  filter[iOff+j] = term1 - term2;
      }
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20358813
I have no idea about the equations you are using in your last post. Them are totally different from your original question.
0
 

Author Comment

by:ExpExchHelp
ID: 20358935
jaime,

yes... I understand... they were just an example.    I've given this some more thought and I believe that I need 4 ForLoops.

Does the following seem to be a reasonable approach?

for (p = 0; p < numberOfBands; bnd++) {
      for (q = 0; q < numberOfBands; bnd++) {
            for (i = 0; i < numberOfRows; i++) {                  
                  imaged = image -1 ;

                        for (j = 0; j < numberOfRows; j++) {                              
                              imaged = image -1 ;
                  }
            }
      }
}




0
 

Author Comment

by:ExpExchHelp
ID: 20359135
Jaime,

given above ForLoop, I'm not sure if I properly reflect the start/end value in the loop.

Therefore, should the ForLoop look like this?

for (p = 0; p < image -1; p++) {
      for (q = 0; q < image -1; q++) {
            for (i = 0; i < image -1; i++) {                  
                  imaged = image -1;

                        for (j = 0; j < image -1; j++) {                              
                              imaged = image -1;
                  }
            }
      }
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20359146
this looks inconsistent:
for (p = 0; p < numberOfBands; bnd++) {
      for (q = 0; q < numberOfBands; bnd++) {
you count p and q, but increment bnd, this will be infinite

about this:
for (i = 0; i < numberOfRows; i++) {                  
     imaged = image -1 ;
appears to be is not a realistic example
0
 

Author Comment

by:ExpExchHelp
ID: 20359256
Any pointers as to where I should go from here?

EEH
0
 

Author Comment

by:ExpExchHelp
ID: 20359557
jaimie,

I think I have new information.   I'll open up a new thread.   Thanks for the help earlier.

New question has been posted under:
http://www.experts-exchange.com/Programming/Languages/CPP/Q_22985448.html

Again, thanks!

EEH
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 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