Solved

C++ Function

Posted on 2007-11-26
11
1,129 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
Comment Utility
how do you store the data? do you have an array? some function to fill in?
0
 

Author Comment

by:ExpExchHelp
Comment Utility
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
Comment Utility
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Any pointers as to where I should go from here?

EEH
0
 

Author Comment

by:ExpExchHelp
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
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.

772 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

11 Experts available now in Live!

Get 1:1 Help Now