Solved

C++ Function

Posted on 2007-11-26
11
1,135 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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…
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 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