LQ Newbie
Registered: Apr 2004
Posts: 6
Rep:
|
What is pgm.h and how do I install?
Hallo everyone,
I am having some trouble compiling C code written for me. I working on cygwin (at work) at the moment but would like to compile the software on a machine running Mandriva 2006 (at home: no i-net access).
I get an error when I try and compile using the following command (cygwin work and at home).
gcc -O4 -o tempspatcorrel tempspatcorrel.c -lm -lpgm -lpbm
The error states that pgm.h: No such file or directory
When I add pgm.h to /usr/local/include I get an error asking for pbm.h
So I put pbm.h in /usr/local/include and get an error asking for pbmplus.h
After that I get new errors regarding conflicting types for srandom and random in stdlib.h and pbm.h.
I know that it would be useful to include the error messages but my efforts to write the error output to a file under cygwin have met with scant success and copying cygwin command line to windows notepad just doesnt work. Apologies.
The source code is below. As I said I did not write the code and I have little or no experience writing and compiling c code.
I would really appreciate some help on this as I need to get the code compiled to run some analysis.
Many thanks in advance for your help and input
Wesley
***************************************************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <pgm.h>
// Some basic parameters that won't change often
#define MAXFILES 600
#define MAXSTR 100
#define KERNEL_SIZE 2
// Function declaration for basic Pearson Correlation calc
float correl(float *series1, float *series2, int n);
int main(int argc, char **argv)
{
char tempfilename[MAXSTR], filename[MAXSTR], command[500];
FILE *infiles[MAXFILES], *filelist, *outfile;
int fileno, xsize, ysize, xpos, ypos, xi, yi, zi, img;
gray **sourceimages[MAXFILES], **outimage, maxval;
float cntr_series[MAXFILES], series[MAXFILES], r;
// Check command line parameters
if (argc != 3)
{
printf("Usage: %s filelist outfile\n", argv[0]);
exit(1);
}
// Open the file list file
if (!(filelist = fopen(argv[1], "r")))
{
printf("Error opening filelist: %s\n", argv[1]);
exit(1);
}
else
printf("Opened filelist: %s\n", argv[1]);
outfile = fopen(argv[2], "w");
// Open all the input image files and read in the image data
fileno = 0;
while (!feof(filelist))
{
// Get the file name
fscanf(filelist, "%s\n", filename);
printf("file %d: %s\n", fileno, filename);
// Convert to pgm format
sprintf(tempfilename, "temp%d.pgm", fileno);
sprintf(command, "/usr/X11R6/bin/convert %s %s >& /dev/null", filename, tempfilename);
system(command);
// Open the file and read in the data
if (!(infiles[fileno] = fopen(tempfilename, "r"))) printf("Oops!\n");
sourceimages[fileno] = pgm_readpgm(infiles[fileno], &xsize, &ysize,&maxval);
fileno++;
}
// Allocate space for the new image
outimage = pgm_allocarray(xsize-(KERNEL_SIZE*2), ysize-(KERNEL_SIZE*2));
// Now scan the source images and calc correlations
for (ypos = KERNEL_SIZE; ypos < ysize - KERNEL_SIZE; ypos++)
{
printf("Processing row %d \r", ypos);
for (xpos = KERNEL_SIZE; xpos < xsize - KERNEL_SIZE; xpos++)
{
r = 0;
// Pull out the kernel central time series
for (img = 0; img < fileno; img++)
cntr_series[img] = (float)sourceimages[img][ypos][xpos];
// For each pixel in the kernel
for (xi = xpos-KERNEL_SIZE; xi <= xpos+KERNEL_SIZE; xi++)
for (yi = ypos-KERNEL_SIZE; yi <= ypos+KERNEL_SIZE; yi++)
{
// .. except the central pixel!
if (xi == xpos && yi == ypos) continue;
// Pull out the time series
for (img = 0; img < fileno; img++)
series[img] = (float)sourceimages[img][yi][xi];
// Accumulate the correlation coefficients
r += correl(cntr_series, series, fileno);
}
// Now calculate the average coefficient and write to the new image
r = r/((KERNEL_SIZE*2 + 1)*((KERNEL_SIZE*2)+1) - 1);
outimage[ypos-KERNEL_SIZE][xpos-KERNEL_SIZE] = (int)(r*255);
}
}
// Write out the image file
pgm_writepgm(outfile, outimage, xsize-(KERNEL_SIZE*2), ysize-(KERNEL_SIZE*2), 255, 1);
return 0;
}
// Calculate Pearson correlatoin coefficient
float correl(float *series1, float *series2, int n)
{
float xm=0, ym=0, xx=0, yy=0, xy=0;
int i;
float r;
// Means
for (i = 0; i < n; i++)
{
xm += series1[i];
ym += series2[i];
}
xm = xm/n;
ym = ym/n;
// Sum of normalised squares
for (i = 0; i < n; i++)
{
xx += pow((series1[i]-xm),2);
yy += pow((series2[i]-ym),2);
xy += (series1[i] - xm)*(series2[i] - ym);
}
// We don't want div by zero
if (xx*yy == 0.0)
r = 0;
// The final formula...
else
r = pow(xy,2)/(xx*yy);
// And back we go
return r;
}
|