Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

THE LBRARY code: #include img_pro.h unsigned char **alloc_img(int ncol,int nrow) { int i; unsigned char **img; /* allocate pointers to rows */ img = (unsigned

image text in transcribed
THE LBRARY code:
#include "img_pro.h"
unsigned char **alloc_img(int ncol,int nrow)
{
int i;
unsigned char **img;
/* allocate pointers to rows */
img = (unsigned char **) malloc((size_t)((nrow+1)*sizeof(unsigned char*)));
if (!img)
{
printf(" Cannot allocate memory (1) ");
exit(0);
}
/* allocate rows and set pointers to them */
img[0] = (unsigned char *) malloc((size_t)((nrow*ncol+1)*sizeof(unsigned char)));
if (!img[0])
{
printf(" Cannot allocate memory (2) ");
exit(0);
}
for(i=1;i
img[i]=img[i-1] + ncol;
/* return pointer to array of pointers to rows */
return (img);
}
unsigned long **alloc_img_long(int ncol,int nrow)
{
int i;
unsigned long **img;
/* allocate pointers to rows */
img = (unsigned long **) malloc((size_t)((nrow+1)*sizeof(unsigned long*)));
if (!img)
{
printf(" Cannot allocate memory (1) ");
exit(0);
}
/* allocate rows and set pointers to them */
img[0] = (unsigned long *) malloc((size_t)((nrow*ncol+1)*sizeof(unsigned long)));
if (!img[0])
{
printf(" Cannot allocate memory (2) ");
exit(0);
}
for(i=1;i
img[i]=img[i-1] + ncol;
/* return pointer to array of pointers to rows */
return (img);
}
void free_img(unsigned char** img)
{
free( img[0] );
free( img );
}
void show_pgm_file(char *file_name)
{
FILE *xfp;
char command[256]="";
char app[256]="";
strcat(command,view);
sprintf(app," %s",file_name);
/*
xfp=popen((const char *)strcat(command,app),"w");
pclose(xfp);
*/
system((const char *)strcat(command,app));
}
void img_to_pgm_file(unsigned char **img, char *file_name,int NC, int NR)
{
int i,j;
FILE *fp;
fp=fopen(file_name,"w");
#ifdef WIN
_setmode( _fileno(fp), _O_BINARY); /* be sure to write files in binary mode */
#endif
fprintf(fp,"P5 %d %d 255 ",NC,NR);
for(i=0;i
for(j=0;j
fputc(img[i][j],fp);
fclose(fp);
}
unsigned char **pgm_file_to_img(char *file_name,int *pNC,int *pNR)
#define MAXC 80
{
FILE *fp;
unsigned char **img;
char cline[MAXC];
int i,j,NR,NC;
int maxgray;
if((fp=fopen(file_name,"r")) == NULL) {
/* open image file */
printf(" *** pgm_file_to_img -- can't open file: %s ****",file_name);
exit(0); }
#ifdef WIN
_setmode( _fileno(fp), _O_BINARY); /* be sure to read files in binary mode */
#endif
comment_loop0: /* skip any comments */
fgets(cline,MAXC,fp); /* get next line from file */
if(cline[0]=='#') goto comment_loop0;
if(cline[0]!='P'||cline[1]!='5')
{
printf(" **** pgm_file_to_img -- not a pgm file: %s ****",file_name);
exit(0);
}
comment_loop1: /* skip any comments */
fgets(cline,MAXC,fp); /* get next line from file */
if(cline[0]=='#') goto comment_loop1;
sscanf(cline,"%d %d ",&NC,&NR);
comment_loop2: /* skip any comments */
fgets(cline,MAXC,fp); /* get next line from file */
if(cline[0]=='#') goto comment_loop2;
sscanf(cline,"%d ",&maxgray); /* get maxgray */
/* return size of image to caller */
*pNC=NC;
*pNR=NR;
img=alloc_img(NC,NR); /* allocate image array */
for(i=0;i
for(j=0;j
{
img[i][j]=fgetc(fp); /* read image data from file */
}
fclose(fp);
return(img);
}
Develop your own C program using the previously supplied library, that performs Low Pass Filtering in frequency domain. The program should work for 512512 gray valued images. It should perform 2D DFT to create two 512512 (type double) arrays for real and imaginary parts of complex numbers. Low frequency components should be placed at the center of the arrays so that angular frequencies are represented in range of [, in both directions. Your program should apply an ideal low pass filter which should cancel out the terms outside the specified region as illustrated below. The cut off frequency is specified using command line arguments. The cut off frequency for the example shown below is /2. The coefficient of (which is 0.5 in this case) should be entered in the command line. I.e: . /hwk4.exe filtest1.pgm 0.5 Your program should generate an output image logDFTmag.pgm from the magnitude of the complex values. This image should be generated by taking log of each magnitude value and scaling each value so that minimum is represented by 0 and max is represented by 255 . After DFT is applied and logDFTmag.pgm is displayed, filtering should be done in frequency domain, and 2D inverse DFT should be computed. Finally the filtered result should be displayed. The following 512512pgm test images will be supplied: You should submit a C file named hwk4.c including comments on how to compile \& run the program. This file should be submitted via Teams at or before due date

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions