LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C code for median filter on color images (https://www.linuxquestions.org/questions/programming-9/c-code-for-median-filter-on-color-images-434793/)

jayshri 02-28-2012 12:56 AM

image processing
 
Hello,
Thanx alot..:D.
I would like to say that since i am very new to this field nd also not that much expert about coding i would like to clear some doubts from u..
it wil b very helpful for me if you kindly clear my doubts.
1.last time when i got some problem with median filtering part then you have added the argument(pixel,pixel..)
then in case of laplacian what wil b the problem?? wil the argument(pixel,pixel...)wil work for median filter??
2.In declaring 2D array part:malloc function worked during median filter.bt during laplacian we are applying calloc???
so do we have to manupalted these things that i mentioned above during laplacian???
nd for median it won't need any change right???

Thanx will be very less for the help that u have done with my coding.:D.
actualy itz a project that i have to complete soon.
Regards..:)

firstfire 02-28-2012 02:12 AM

Hi.

Good questions.
Quote:

1.last time when i got some problem with median filtering part then you have added the argument(pixel,pixel..)
then in case of laplacian what wil b the problem?? wil the argument(pixel,pixel...)wil work for median filter??
Actually in both cases (median and laplacian filters) one should pass different arrays as input and output. That is because the transformation we apply to (i,j)-th input pixel depends also on the neighbourhood of that pixel and if we put the result into the same pixel (in the same array), then, when we proceed to (i+1,j)-th pixel, our result will be incorrect -- we already changed pixel (i,j) from neighbourhood if (i+1,j). That's why last time I used second array to store the result:
Code:

median: pixel -> result
laplacian: result -> pixel

Quote:

2.In declaring 2D array part:malloc function worked during median filter.bt during laplacian we are applying calloc???
malloc() and calloc() are the same, except that calloc() also sets allocated memory to zero. In fact, you may use malloc() and do not initialize arrays, because their elements are explicitly set when you load bitmap and perform transformations (so you never use uninitialized values). For more details read `man malloc'.

jayshri 02-28-2012 02:19 AM

image processing
 
now i gt it thanx alot... m done with preprocessing technique..
nw i wil start edge detection then..:)

jayshri 02-29-2012 02:04 AM

im
 
double gaussian (double x, double mu, double sigma) {
return exp( -(((x-mu)/(sigma))*((x-mu)/(sigma)))/2.0 );
}

vector< vector<double> > produce2dGaussianKernel (int kernelRadius) {
// get kernel matrix
vector< vector<double> > kernel2d ( 2*kernelRadius+1,
vector<double>(2*kernelRadius+1)
);

// determine sigma
double sigma = kernelRadius/2.;

// fill values
double sum = 0;
for (int row = 0; row < kernel2d.size(); row++)
for (int col = 0; col < kernel2d[row].size(); col++) {
kernel2d[row][col] = gaussian(row, kernelRadius, sigma) *
gaussian(col, kernelRadius, sigma);
sum += kernel2d[row][col];
}

// normalize
for (int row = 0; row < kernel2d.size(); row++)
for (int col = 0; col < kernel2d[row].size(); col++)
kernel2d[row][col] /= sum;

return kernel2d;
}

int main() {
vector< vector<double> > kernel2d = produce2dGaussianKernel (3);
for (int row = 0; row < kernel2d.size(); row++) {
for (int col = 0; col < kernel2d[row].size(); col++)
cout << setprecision(5) << fixed << kernel2d[row][col] << " ";
cout << endl;
}
}
itz a c++ code for aaplying gaussian blurr on a image..
i am unable to understand the 1 st part of the code nd hw to convert it into c..
what wil b the euivalent c code..
Regards

jayshri 02-29-2012 04:24 AM

i mean the middle and the main part wil b same i think.. bt 1st part i didn't get... what wil b the c code for that..
Regards..:)

jayshri 02-29-2012 05:23 AM

i
 
double sigma = 1;
int W = 5;
double kernel[W][W];
double mean = W/2;
for (int x = 0; x < W; ++x) for (int y = 0; y < W; ++y)
{
kernel[x][y] = exp( -0.5 * (pow((x-mean)/sigma, 2.0) + pow((y-mean)/sigma,2.0)) )
/ (2 * M_PI * sigma * sigma);
}



wil this code work with my code above???..
regards

firstfire 02-29-2012 05:35 AM

Hi.

Yes, in principle this will work after some tiny modifications to the laplacian filter (the size of convolution kernel in 5x5 instead of 3x3). BTW, take a look at http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm. I'd probably introduce some convolution(in, out, sizes, kernel, kern_size) function which can be used to perform different types of filtering.

jayshri 02-29-2012 05:39 AM

with the theory part i am all clear.. but again stuck with programming..:(((
regargds....

jayshri 03-01-2012 01:55 AM

im
 
hello firstfire,
I tried out this program.. i have changed the kernel into 5X5.

#include<stdio.h>
#include<stdlib.h>
//#include"medianfilter.h"
#include<memory.h>
#include<math.h>
#include<float.h>
#define MAX_BRIGHTNESS 80
#define OUTPUT_FILE "out.bmp"
typedef char element;


long getImageInfo(FILE*,long,int);
void _medianfilter(element* image,element* result,int nCols,int nRows)
{
for(int m=1;m<nRows-1;++m)
for(int n=1;n<nCols-1;++n)
{
int k=0;
element window[9];
for(int j=m-1;j<m+2;++j)
for(int i=n-1;i<n+2;++i)
window[k++]=image[j*nCols+i];
for(int j=0;j<5;++j)
{
int min=j;
for(int l=j+1;l<9;++l)
if(window[l]<window[min])
min=l;
const element temp=window[j];
window[j]=window[min];
window[min]=temp;
result[(m-1)*(nCols)+n-1]=window[4];
}
}
}
void laplacian_filtering(element *image1,element *image2,int x_size1,int y_size1)
/* spatial filtering of image*/
/*8-neighbor Laplacian filter*/
/*input:image1[y][x]-Outout:image2[y][x]*/

/*Definition of 8-neighbour Laplacian filter*/
{
int weight[5][5]={{2,4,5,4,2},
{4,9,12,9,4},
{5,12,15,12,5},
{4,9,12,9,4},
{2,4,5,4,2}};
double pixel_value;
double min,max;
int x,y,i,j;
/*Maximum values calculation after filtering*/
printf("Now,filtering of input image is performed\n\n");
min=DBL_MAX;
max=DBL_MIN;
for(y=1;y<y_size1-1;y++){
for(x=1;x<x_size1-1;x++){
pixel_value=0.0;
for(j=-1;j<2;j++){
for(i=-1;i<2;i++){
pixel_value+=weight[j+1][i+1]*image1[x_size1*(y+j)+x+i];
}
}
if(pixel_value<min)min=pixel_value;
if(pixel_value>max)max=pixel_value;
}
}
if((int)(max-min)==0){
printf("Nothing exist!!!\n\n");
exit(1);
}
//initialization of image2[y][x]
//x_size2=x_size1;
//y_size2=y_size1;
//for(y=0;y<y_size2;y++){
//for(x=0;x<x_size2;x++){
//image2[y][x]=0;
//}
//}
//Generation of image 2 after linear transformation
for(y=1;y<y_size1-1;y++){
for(x=1;x<x_size1-1;x++){
pixel_value=0.0;
for(j=-1;j<2;j++){
for(i=-1;i<2;i++){
pixel_value+=weight[j+1][i+1]*image1[x_size1*(y+j)+x+i];
}
}
pixel_value=MAX_BRIGHTNESS*(pixel_value-min)/(max-min);
image2[y*x_size1+x]=(unsigned char)pixel_value;
}
}
}
double sigma=1;
int W=5;
double kernel[W][W];
double mean=W/2;
for(int x=0;x<W;++x)
{
for(int y=0;y<W;++y)
{
kernel[x][y]=exp(-0.5*(pow((x-mean)/sigma,2.0)+pow((y-mean)/sigma,2.0)))/(2*M_PI*sigma*sigma);
}
}




int main(int argc,char *argv[])
{

FILE *bmpInput, *bmpOutput;
unsigned char *pixel,*result;
char signature[2];
long nRows,nCols,nbits;
long xpixpeRm,ypixpeRm;
long nColors;
long fileSize;
long vectorSize;
long nBits;
long rasterOffset;
int i, j,k,l,m;
unsigned char databuff[512][512][3];

if(argc<2)
{
printf("Usage: %s <median.bmp>\n",argv[0]);
exit(0);
}

//Open the specified input file for reading.
printf("Reading %s ...\n",argv[1]);
if((bmpInput = fopen(argv[1],"rb"))==NULL)
{
printf("Cannot read %s \n", argv[1]);
exit(0);
}

//open output file.
if((bmpOutput = fopen(argv[2],"w+"))==NULL)
{
if((bmpOutput = fopen(OUTPUT_FILE,"w+"))==NULL) //if user hasn't specified the output file use default output filename.
{
printf("Cannot read %s \n", argv[1]);
exit(0);
}
}

//position the pointer to the beginning of the file.
fseek(bmpInput, 0L, SEEK_SET);

//read First two characters of the input file.
for(i=0; i<2; i++)
{
signature[i] = (char)getImageInfo(bmpInput,i,1);
}

//verify First two character of a BMP image file are BM
if((signature[0]=='B') && (signature[1]=='M'))
{
printf("It is verified that the image is in Bitmap format\n");
}
else
{
printf("The image is not a BMP format,quitting....\n");
exit(0);
}

//specifies number of bits per pixel in the image.
nBits = getImageInfo(bmpInput, 28, 2);
printf("The Image is \t%ld-bits per pixel. \n", nBits);

//offset from the begining of the file where the pixel data starts.
rasterOffset = getImageInfo(bmpInput,10,4);
printf("The pixel Data is at \t%ld byte.\n",rasterOffset);

//size of the file in bytes.
fileSize=getImageInfo(bmpInput,2,4);
printf("File size is \t%ld byte\n",fileSize);

//number of columns in image.
nCols = getImageInfo(bmpInput,18,4);
printf("Width:\t\t%ld\n",nCols);

//number of rows in image.
nRows = getImageInfo(bmpInput,22,4);
printf("Height:\t%ld\n",nRows);


xpixpeRm = getImageInfo(bmpInput,38,4);
printf("Image has \t%ld pixels per m in x-dir.\n",xpixpeRm);

ypixpeRm = getImageInfo(bmpInput,42,4);
printf("Image has \t%ld pixel per m in y-dir.\n",ypixpeRm);

nColors = 1L<<nBits;
printf("There are \t%ld number of colors \n",nColors);

//it is the size of the array required to store the image pixel data.
vectorSize = nCols*nRows;
printf("vector Size is \t%ld\n",vectorSize);

//write the bmp header to the output file.
i = 0;
while(i < rasterOffset)
{
fputc((char)getImageInfo(bmpInput, i, 1), bmpOutput);
i++;
}


//now declare an 2D array to store & manipulate the image pixel data.
pixel = (char *) calloc(sizeof(char)*nRows*nCols,sizeof(char));
result= (char *) calloc(sizeof(char)*nRows*nCols,sizeof(char));

//Set all the array value to zero.
printf("\n\nResetting the pixel array: ");
i = 0;
while(i < vectorSize)
{
pixel[i] = 0x00;
i++;
// printf("%d ", i);
}

//Read the bitmap data into array:
printf("\n\nReading the pixel array: ");
i = 0;
while(i < vectorSize)
{
pixel[i] = (char)getImageInfo(bmpInput,rasterOffset+ i, 1);
i++;
// printf("%d ", i);
}

//Display or modify pixel values:
printf("\n\n Diplaying pixel values: \n\n");
i = 0;
j = 0;
while(i < nRows)
{
j = 0;
while(j < nCols)
{
printf("(%d,%d)-%02x\n ",i,j, pixel[i*nRows+j]); //Printing the pixel values.
j++;
}
i++;
}
if((nRows!=512)||(nCols!=512)||(nBits!=8)){
printf(" this works only for 512x512 8-color bitmaps\n");
return 0;
}

//int main(void)
//{
// enum {nCols=512,nRows=512};
// const float in[nCols*nRows];
// float out[nCols*nRows];
// median(out,in,nCols,nRows);
// printf("the filterd image is:\n");
// for (int i=0;i<nRows;i++)
//{
// for (int j=0;j<nCols;j++)
//{
// printf("%0.f",out[i*nCols+j]);
// printf("\n");
//}
//
//return 0;
_medianfilter(pixel,result,nCols,nRows);
//load_image_data();//input of image1
laplacian_filtering(result,pixel,nCols,nRows);//laplacian filtering is applied to image1
//save_image_data();//output of image2
//return 0;


//write the modified pixel array to the output file.
i = 0;
while(i < vectorSize)
{
fputc(pixel[i], bmpOutput);
i++;
}

//write the End-Of-File character the output file.
fputc(EOF, bmpOutput);

printf("\n");
fclose(bmpInput);
fclose(bmpOutput);
}

long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
{
unsigned char *ptrC;
long value = 0L, temp;
unsigned char dummy;
int i;

dummy = '0';
ptrC = &dummy;

//position the file pointer to the desired offset.
fseek(inputFile, offset, SEEK_SET);

//read the bytes into values (one byte at a time).
for(i=1; i<=numberOfChars; i++)
{
fread(ptrC,sizeof(char),1,inputFile);
temp = *ptrC;
value = (long) (value + (temp<<(8*(i-1))));

}

return(value);
}



here is the previous program with some change in kernel part..and also gaussian blurr part included..
is this the way u were telling me to do it????
some error are also coming after introducing gaussian blur part
errors are:
1. Variably modified 'kernel'at first scope
2.initializer element is not constant.
3. expecting identifier befor ++ token.
Didn't able to sort it out..
Regard..:(

firstfire 03-01-2012 12:52 PM

Hi.

Here is a new version. Try to figure out how it works and ask questions. load/save BMP should work with 8bpp images, not sure about other types.
Code:

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include <string.h>

#define MAX_BRIGHTNESS 255

/*
 * Loading part taken from
 * http://www.vbforums.com/showthread.php?t=261522
 * BMP info:
 * http://en.wikipedia.org/wiki/BMP_file_format
 *
 */
 
/* Note: the magic number has been removed from the bmpfile_header structure
  since it causes alignment problems
    struct bmpfile_magic should be written/read first
  followed by the
    struct bmpfile_header
  [this avoids compiler-specific alignment pragmas etc.]
*/
 
struct bmpfile_magic {
  unsigned char magic[2];
};
 
struct bmpfile_header {
  uint32_t filesz;
  uint16_t creator1;
  uint16_t creator2;
  uint32_t bmp_offset;
};

typedef struct {
  uint32_t header_sz;
  int32_t width;
  int32_t height;
  uint16_t nplanes;
  uint16_t bitspp;
  uint32_t compress_type;
  uint32_t bmp_bytesz;
  int32_t hres;
  int32_t vres;
  uint32_t ncolors;
  uint32_t nimpcolors;
} BITMAPINFOHEADER;

typedef struct {
        uint8_t r;
        uint8_t g;
        uint8_t b;
        uint8_t null;
} rgb_t;

typedef unsigned char pixel_t;

unsigned char *load_bmp(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
        FILE *filePtr; //our file pointer
        struct bmpfile_magic  mag;
        struct bmpfile_header bitmapFileHeader; //our bitmap file header
        unsigned char *bitmapImage;  //store image data

        filePtr = fopen(filename,"r");
        if (filePtr == NULL)
                return NULL;

        fread(&mag, sizeof(struct bmpfile_magic),1,filePtr);
        //verify that this is a bmp file by check bitmap id
        if ( *((uint16_t*) mag.magic) != 0x4D42 )
        {
                fprintf(stderr, "Not a BMP file: magic=%c%c\n", mag.magic[0], mag.magic[1]);
                fclose(filePtr);
                return NULL;
        }
        //read the bitmap file header
        fread(&bitmapFileHeader, sizeof(struct bmpfile_header), 1, filePtr);

        //read the bitmap info header
        fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);

        if( bitmapInfoHeader->compress_type != 0)
                fprintf(stderr, "Warning, compression is not supported.\n");

        //move file point to the beginning of bitmap data
        fseek(filePtr, bitmapFileHeader.bmp_offset, SEEK_SET);

        //allocate enough memory for the bitmap image data
        bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->bmp_bytesz);

        //verify memory allocation
        if (!bitmapImage)
        {
                free(bitmapImage);
                fclose(filePtr);
                return NULL;
        }

        //read in the bitmap image data
        fread(bitmapImage, 1, bitmapInfoHeader->bmp_bytesz, filePtr);

        //close file and return bitmap image data
        fclose(filePtr);
        return bitmapImage;
}

// Return: nonzero on error.
int save_bmp(char *filename, BITMAPINFOHEADER *bmp_ih, unsigned char *data)
{
        unsigned int offset =
                sizeof(struct bmpfile_magic)
                + sizeof(struct bmpfile_header)
                + sizeof(BITMAPINFOHEADER)
                + (1<<bmp_ih->bitspp)*4;

        struct bmpfile_header bmp_fh = {
                .filesz = offset + bmp_ih->bmp_bytesz,
                .creator1 = 0,
                .creator2 = 0,
                .bmp_offset = offset
        };

        FILE* fp = fopen(filename,"w");
        if (fp == NULL) return 1;
        struct bmpfile_magic mag = {{0x42, 0x4d}};

        fwrite(&mag, 1, sizeof(struct bmpfile_magic), fp);
        fwrite(&bmp_fh, 1, sizeof(struct bmpfile_header), fp);
        fwrite(bmp_ih, 1, sizeof(BITMAPINFOHEADER), fp);

        // Palette
        rgb_t color = {0, 0, 0, 0};
        int i;
        for(i=0; i < (1<<bmp_ih->bitspp); i++)
        {
                color.r = i;
                color.g = i;
                color.b = i;
                fwrite(&color, 1, sizeof(rgb_t), fp);
        }
        fwrite(data, 1, bmp_ih->bmp_bytesz, fp);

        fclose(fp);
        return 0;
}

void median_filter( pixel_t* image, pixel_t* result, int nCols, int nRows)
{
        int m, n, i, j, l, k, min;
        for(m=1; m<nRows-1; m++)
                for(n=1; n<nCols-1; n++)
                {
                        k = 0;
                        pixel_t window[9];
                        for(j=m-1; j<m+2; j++)
                                for(i=n-1; i<n+2; i++)
                                        window[k++] = image[j*nCols+i];
                        for(j=0; j<5; j++)
                        {
                                min = j;
                                for(l=j+1; l<9; l++)
                                        if(window[l]<window[min]) min=l;
                                pixel_t temp = window[j];
                                window[j] = window[min];
                                window[min] = temp;
                                result[m*(nCols)+n] = window[4];
                        }
                }
}

void convolution(pixel_t *in, pixel_t *out, double *kernel, int nx, int ny, int kn)
{
        int i, j, m, n, c;
        int khalf = floor(kn/2.);
        double pixel, min=DBL_MAX, max=DBL_MIN;

        for(m=khalf; m<nx-khalf; m++)
                for(n=khalf; n<ny-khalf; n++)
                {
                        pixel = 0;
                        for(c=0, i=-khalf; i<=khalf; i++)
                                for(j=-khalf; j<=khalf; j++)
                                        pixel += in[(n-j)*nx + m-i]*kernel[c++];
                        if(pixel < min) min = pixel;
                        if(pixel > max) max = pixel;
                }

        for(m=khalf; m<nx-khalf; m++)
                for(n=khalf; n<ny-khalf; n++)
                {
                        pixel = 0;
                        for(c=0, i=-khalf; i<=khalf; i++)
                                for(j=-khalf; j<=khalf; j++)
                                        pixel += in[(n-j)*nx + m-i]*kernel[c++];

                        pixel = MAX_BRIGHTNESS*(pixel-min)/(max-min);
                        out[n*nx + m] = (unsigned char) pixel;
                }
}

void laplacian_filter(pixel_t *in, pixel_t *out, int nx, int ny)
{
        double kernel[]={
                1,1,1,
                1,-8,1,
                1,1,1};
        convolution(in, out, kernel, nx, ny, 3);
}

void gaussian_filter(pixel_t *in, pixel_t *out, int nx, int ny)
{
        const int n = 7;
        int i, j, c=0;
        double        mean = floor(n/2.), sigma = 1;
        double kernel[n*n];
        for(i=0; i<n; i++)
                for(j=0; j<n; j++)
                        kernel[c++]=exp(-0.5*(
                                                pow((i-mean)/sigma,2.0) +pow((j-mean)/sigma,2.0))
                                        ) /(2*M_PI*sigma*sigma);
        convolution(in, out, kernel, nx, ny, n);
}

int main(int argc, char **argv)
{
        if(argc < 2){
                printf("Usage: %s image.bmp\n", argv[0]);
                exit(1);
        }
        BITMAPINFOHEADER ih;
        pixel_t *bitmap_data, *temp_image;

        bitmap_data = load_bmp(argv[1], &ih);
        temp_image = (unsigned char*) malloc(ih.bmp_bytesz);

        printf("Info: %d x %d x %d\n", ih.width, ih.height, ih.bitspp);

        median_filter(bitmap_data, temp_image, ih.width, ih.height);
        //laplacian_filter(bitmap_data, temp_image, ih.width, ih.height);
        //gaussian_filter(bitmap_data, temp_image, ih.width, ih.height);

        save_bmp("out.bmp", &ih, temp_image);
        return 0;
}


jayshri 03-01-2012 01:58 PM

image processing
 
hi.
the program code u have given is 4 colour image.. but i am working with gray image..
i hav to do the whole process in the same program... i hav already done the loading of bmp image now what i want to do is canny edge detection..
as u hav sugested i hav changed the kernel size... but hw it works i cant figure out.. what is going worng in that code???
nw i hav to manipulate the previous code only nd av to perform canny edge detection..
Regards

jayshri 03-02-2012 01:03 AM

image processing
 
hi firstfire,

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<memory.h>
#include<math.h>
#include<float.h>
#include<string.h>
#define MAX_BRIGHTNESS 80
#define OUTPUT_FILE "out.bmp"
typedef char element;


long getImageInfo(FILE*,long,int);
void _medianfilter(element* image,element* result,int nCols,int nRows)
{
for(int m=1;m<nRows-1;++m)
for(int n=1;n<nCols-1;++n)
{
int k=0;
element window[9];
for(int j=m-1;j<m+2;++j)
for(int i=n-1;i<n+2;++i)
window[k++]=image[j*nCols+i];
for(int j=0;j<5;++j)
{
int min=j;
for(int l=j+1;l<9;++l)
if(window[l]<window[min])
min=l;
const element temp=window[j];
window[j]=window[min];
window[min]=temp;
result[(m-1)*(nCols)+n-1]=window[4];
}
}
}
void convolution(element *in,element *out,double *kernel,int nx,int ny,int kn)
{
int i,j,m,n,c;
int khalf=floor(kn/2.);
double pixel,min=DBL_MAX,max=DBL_MIN;
for(m=khalf;m<nx-khalf;n++)
{
pixel=0;
for(c=0,i=-khalf;i<=khalf;i++)
for(j=-khalf;j<=khalf;j++)
pixel+=in[(n-j)*nx+m-i]*kernel[c++];
if(pixel<min)min=pixel;
if(pixel>max)max=pixel;
}
for(m=khalf;m<nx-khalf;m++)
for(n=khalf;n<ny-khalf;n++)
{
pixel=0;
for(c=0,i=-khalf;i<=khalf;i++)
for(j=-khalf;j<=khalf;j++)
pixel+=in[(n-j)*nx+m-i]*kernel[c++];
pixel=MAX_BRIGHTNESS*(pixel-min)/(max-min);
out[n*nx+m]=(unsigned char)pixel;
}
}
void laplacian_filtering(element *in,element *out,int nx,int ny)
/* spatial filtering of image*/
/*8-neighbor Laplacian filter*/
/*input:image1[y][x]-Outout:image2[y][x]*/

/*Definition of 8-neighbour Laplacian filter*/
{
double kernel[]={1,1,1,
1,-8,1,
1,1,1};
convolution(in,out,kernel,nx,ny,3);
}
//double pixel_value;
//double min,max;
//int x,y,i,j;
/*Maximum values calculation after filtering*/
//printf("Now,filtering of input image is performed\n\n");
//min=DBL_MAX;
//max=DBL_MIN;
void gaussian_filter(element *in,element *out,int nx,int ny)
{
const int n=7;
int i,j, c=0;
double mean=floor(n/2.);
double sigma=1;
double kernel[n*n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)

//for(y=1;y<y_size1-1;y++){
//for(x=1;x<x_size1-1;x++){
//pixel_value=0.0;
//for(j=-1;j<2;j++){
//for(i=-1;i<2;i++){
//pixel_value+=weight[j+1][i+1]*image1[x_size1*(y+j)+x+i];
//}
//}
//if(pixel_value<min)min=pixel_value;
//if(pixel_value>max)max=pixel_value;
//}
//}
//if((int)(max-min)==0){
//printf("Nothing exist!!!\n\n");
//exit(1);
//}
//initialization of image2[y][x]
//x_size2=x_size1;
//y_size2=y_size1;
//for(y=0;y<y_size2;y++){
//for(x=0;x<x_size2;x++){
//image2[y][x]=0;
//}
//}
//Generation of image 2 after linear transformation
//for(y=1;y<y_size1-1;y++){
//for(x=1;x<x_size1-1;x++){
//pixel_value=0.0;
//for(j=-1;j<2;j++){
//for(i=-1;i<2;i++){
//pixel_value+=weight[j+1][i+1]*image1[x_size1*(y+j)+x+i];
//}
//}
//pixel_value=MAX_BRIGHTNESS*(pixel_value-min)/(max-min);
//image2[y*x_size1+x]=(unsigned char)pixel_value;
//}
//}
//}
//double sigma=1;
//int W=5;
//double kernel[W][W];
//double mean=W/2;
//for(int x=0;x<W;++x)
//{
//for(int y=0;y<W;++y)
//{
kernel[c++]=exp(-0.5*(pow((i-mean)/sigma,2.0)+pow((j-mean)/sigma,2.0)))
/(2*M_PI*sigma*sigma); //error in this line
convolution(in,out,kernel,nx,ny,n);
}





int main(int argc,char *argv[])
{

FILE *bmpInput, *bmpOutput;
unsigned char *pixel,*result;
char signature[2];
long nRows,nCols,nbits;
long xpixpeRm,ypixpeRm;
long nColors;
long fileSize;
long vectorSize;
long nBits;
long rasterOffset;
int i, j,k,l,m;
unsigned char databuff[512][512][3];

if(argc<2)
{
printf("Usage: %s <median.bmp>\n",argv[0]);
exit(0);
}

//Open the specified input file for reading.
printf("Reading %s ...\n",argv[1]);
if((bmpInput = fopen(argv[1],"rb"))==NULL)
{
printf("Cannot read %s \n", argv[1]);
exit(0);
}

//open output file.
if((bmpOutput = fopen(argv[2],"w+"))==NULL)
{
if((bmpOutput = fopen(OUTPUT_FILE,"w+"))==NULL) //if user hasn't specified the output file use default output filename.
{
printf("Cannot read %s \n", argv[1]);
exit(0);
}
}

//position the pointer to the beginning of the file.
fseek(bmpInput, 0L, SEEK_SET);

//read First two characters of the input file.
for(i=0; i<2; i++)
{
signature[i] = (char)getImageInfo(bmpInput,i,1);
}

//verify First two character of a BMP image file are BM
if((signature[0]=='B') && (signature[1]=='M'))
{
printf("It is verified that the image is in Bitmap format\n");
}
else
{
printf("The image is not a BMP format,quitting....\n");
exit(0);
}

//specifies number of bits per pixel in the image.
nBits = getImageInfo(bmpInput, 28, 2);
printf("The Image is \t%ld-bits per pixel. \n", nBits);

//offset from the begining of the file where the pixel data starts.
rasterOffset = getImageInfo(bmpInput,10,4);
printf("The pixel Data is at \t%ld byte.\n",rasterOffset);

//size of the file in bytes.
fileSize=getImageInfo(bmpInput,2,4);
printf("File size is \t%ld byte\n",fileSize);

//number of columns in image.
nCols = getImageInfo(bmpInput,18,4);
printf("Width:\t\t%ld\n",nCols);

//number of rows in image.
nRows = getImageInfo(bmpInput,22,4);
printf("Height:\t%ld\n",nRows);


xpixpeRm = getImageInfo(bmpInput,38,4);
printf("Image has \t%ld pixels per m in x-dir.\n",xpixpeRm);

ypixpeRm = getImageInfo(bmpInput,42,4);
printf("Image has \t%ld pixel per m in y-dir.\n",ypixpeRm);

nColors = 1L<<nBits;
printf("There are \t%ld number of colors \n",nColors);

//it is the size of the array required to store the image pixel data.
vectorSize = nCols*nRows;
printf("vector Size is \t%ld\n",vectorSize);

//write the bmp header to the output file.
i = 0;
while(i < rasterOffset)
{
fputc((char)getImageInfo(bmpInput, i, 1), bmpOutput);
i++;
}


//now declare an 2D array to store & manipulate the image pixel data.
pixel = (char *) calloc(sizeof(char)*nRows*nCols,sizeof(char));
result= (char *) calloc(sizeof(char)*nRows*nCols,sizeof(char));

//Set all the array value to zero.
printf("\n\nResetting the pixel array: ");
i = 0;
while(i < vectorSize)
{
pixel[i] = 0x00;
i++;
// printf("%d ", i);
}

//Read the bitmap data into array:
printf("\n\nReading the pixel array: ");
i = 0;
while(i < vectorSize)
{
pixel[i] = (char)getImageInfo(bmpInput,rasterOffset+ i, 1);
i++;
// printf("%d ", i);
}

//Display or modify pixel values:
printf("\n\n Diplaying pixel values: \n\n");
i = 0;
j = 0;
while(i < nRows)
{
j = 0;
while(j < nCols)
{
printf("(%d,%d)-%02x\n ",i,j, pixel[i*nRows+j]); //Printing the pixel values.
j++;
}
i++;
}
if((nRows!=512)||(nCols!=512)||(nBits!=8)){
printf(" this works only for 512x512 8-color bitmaps\n");
return 0;
}

//int main(void)
//{
// enum {nCols=512,nRows=512};
// const float in[nCols*nRows];
// float out[nCols*nRows];
// median(out,in,nCols,nRows);
// printf("the filterd image is:\n");
// for (int i=0;i<nRows;i++)
//{
// for (int j=0;j<nCols;j++)
//{
// printf("%0.f",out[i*nCols+j]);
// printf("\n");
//}
//
//return 0;
_medianfilter(pixel,result,nCols,nRows);
//load_image_data();//input of image1
laplacian_filtering(result,pixel,nCols,nRows);//laplacian filtering is applied to image1
//save_image_data();//output of image2
//return 0;


//write the modified pixel array to the output file.
i = 0;
while(i < vectorSize)
{
fputc(pixel[i], bmpOutput);
i++;
}

//write the End-Of-File character the output file.
fputc(EOF, bmpOutput);

printf("\n");
fclose(bmpInput);
fclose(bmpOutput);
}

long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
{
unsigned char *ptrC;
long value = 0L, temp;
unsigned char dummy;
int i;

dummy = '0';
ptrC = &dummy;

//position the file pointer to the desired offset.
fseek(inputFile, offset, SEEK_SET);

//read the bytes into values (one byte at a time).
for(i=1; i<=numberOfChars; i++)
{
fread(ptrC,sizeof(char),1,inputFile);
temp = *ptrC;
value = (long) (value + (temp<<(8*(i-1))));

}

return(value);
}



here is the code.. i have changed this code a bit according to the previous code u have posted.
every thing worked wel.
bt in the last part itz showing an error..
i haved marked the line with bold letters.
the error is undeclared M_PI.
Regards..:(

firstfire 03-02-2012 02:55 AM

Hi.

Constant M_PI defined in math.h header.

jayshri 03-02-2012 03:03 AM

image processing
 
hey i have included that header...
still itz showing same error

firstfire 03-02-2012 03:17 AM

Then add
Code:

#define M_PI 3.14159265358979323846
after includes.


All times are GMT -5. The time now is 10:53 PM.