C code for median filter on color images
Somebody has maybe somewhere color median filter code written in C to filter out a color picture with it. I have read image to image_pointer but i dont know the algorithm to do median filtering on it. I made it for gray pictures which is easy, but i am stuck for color pictures. If somebody maybe has this filter already written or seen it somewhere i would appreciate it. BTW. using some opencv or some other image processing plugin or whatever doesnt come into account :) I need to have the C code for it.
Thx. |
|
Yah, i know this url, it doesnt help me. It needs libs from that software, i have to use pure C (have to reinvent the whole thing, dont ask why its a damn school project ...).
For exmpl. this is median for gray pictures: Code:
#include <stdio.h> |
Its looks like you need to add checking for 16bit and 24 bit color images. You are aware of little endianess, right? Your swap array is only effective on 4bit images(16 color grayscale) because it only works with two bytes.
Code:
void swap(int *array, int i, int j) |
The thing i have for grayscal images is ok, because i use pgm files. I need algorithm for median for color images (RGB) format ppm.
|
I have already stored image in memory as image_ptr. I only need the algorithm what to do with pixels. I know how to write the image back aswell.
|
Quote:
|
gray pixture is like this: valuse from 1-255 '23 34 45 234 12' each representing one pixel. RGB picture on the other hand is like this: 23 34 56 78 34 120 first three values reperesent one pixel with first number being R second G third B. Now you have to do meadian somehow based on that, so no i cant use gray median for this not even close :) I am stuck :(
|
pgm - see http://netpbm.sourceforge.net/doc//pgm.html
ppm - see http://netpbm.sourceforge.net/doc//ppm.html for some reference :) |
they look nearly the same, except the last two values are swapped. But are they really both 32bit in length? That seems wasteful for a grayscale.
But ahh I see Quote:
|
I cant make same procedure to get median value on color picture. Because you cant make median on three pixels. you must make median on a window of 3x3 for example on R and then on G and on B then u write back all those components to get next pixel with RGB, or else you would get just diffrent colors if not all black.
I just need the algorithm for this procedure, i can manage other myself, just dont know the logic part of the algorithm. :( |
Request for ip.h file
Quote:
|
image processing
hello people m also doing a project in image processing... i have already devloped the code that reads the image pixel by pixel. now i want to do median filtering.... i have to make a structured program.. i.e i hav to incude the median filter code in the code which is reading pixel value... can nebody help me with the coding of median filtering..
i need it in c. thank u. regards |
Quote:
Welcome to LQ! Take a look at this Wikipedia article. You will find the pseudo code for median filtering there as well as a link to rosettacode.org with implementations in different languages (including C). Hope this helps. |
image processing
thank u...:)
|
Just finished reading an article (from aforementioned wikipedia page). It turned out that time complexity of median filtering could be reduced to O(1) (that is made independent of kernel radius). The idea seems to be quite straightforward. You may take a look at it also.
|
image processing
thanx alot... m reading it nw..:)
|
image processing
i have got very little knowledge about C. also image processing is a new field 4 me... so itz gettng abit difficult 4 me . m tryng to solve it... thanx 4 d fast replies... i wil get back to u if i have any doubts further..
Regards. |
im
median_filter(the_image, out_image,
rows, cols, size) int size; F.7. CODE LISTINGS FOR CHAPTER 7 short long **the_image, **out_image; rows, cols; { int short a, b, count, i, j, k, length, sd2, sd2p1, ss, width; *elements; sd2 = size/2; sd2p1 = sd2 + 1; /********************************************** * * Allocate the elements array large enough * to hold size*size shorts. * **********************************************/ ss = size*size; elements = (short *) malloc(ss * sizeof(short)); /*************************** * * Loop over image array * ****************************/ printf("\n"); for(i=sd2; i<rows-sd2; i++){ if( (i%10) == 0) printf("%d ", i); for(j=sd2; j<cols-sd2; j++){ count = 0; for(a=-sd2; a<sd2p1; a++){ for(b=-sd2; b<sd2p1; b++){ elements[count] = the_image[i+a][j+b]; count++; } } out_image[i][j] = median_of(elements, &ss); } /* ends loop over j */ } /* ends loop over i */ free(elements); fix_edges(out_image, sd2, rows-1, cols-1); } m using dis code for median filtering.. bt its showing 2 errors: 1.undefined refrence to median_of; 2.undefined refence to fix_edges. as i am a newbie in C.. so m nt able to rectify it. any help wil b appreciated. Regards. |
Hi.
The following code I copied from this pdf file. Header files and empty main() function are also added. Compiles without errors. Code:
#include <stdio.h> By the way here is a self-contained sample program from rosettacode.org, which compiles and run on PPM files well: Code:
#include <stdio.h> |
image processing
#include<stdio.h>
#include<stdlib.h> //#include"medianfilter.h" #include<memory.h> #include<math.h> #define OUTPUT_FILE "out.bmp" long getImageInfo(FILE*,long,int); int main(int argc,char *argv[]) { FILE *bmpInput, *bmpOutput; unsigned char *pixel; 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 <lena512.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 *) malloc(sizeof(char)*nRows*nCols); //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, 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; // Median filtering 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*N+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-2)+n-1]=window[4]; } } } //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); } i am using this code.. it prints the pixel value of image of gray scale lena.. bt i am tryning to bulid a structured program.. i am nt able to do the median filtering. if we comrnt out median filtering part it wil display pixel value wid image.. can ne 1 plz suggest me what is going wrong in median filtering part... regards..:( |
2 Attachment(s)
Hi.
Here is a reviewed code, which seems to do median filtering with 3x3 window. Previously erroneous places are marked with bold font. Code:
#include<stdio.h> Code:
$ gcc -std=c99 median.c && ./a.out lena512.bmp Attachment 9124 Attachment 9125 PS: To improve readability of your posts please put program sources into [code] ... [/code] tags. Full list of available tags is here. |
image processing
Thanx alot.. u helpd a lot... m checking d code
|
im
It worked.. thank u friend..:)
|
You're welcome!
|
image processing
helo firstfire,
m trying to do laplacian spatial filtering of my image for the sharpening purpose. I am easily gettng the codes in matlab.but for C i am not getting any specific codes.. can u help me with that??? Regards.:) |
image processing
/* laplacian.c */
#include <stdio.h> #include <stdlib.h> #include <float.h> #include "mypgm.h" void laplacian_filtering( ) /* Spatial filtering of image data */ /* 8-neighbor Laplacian filter */ /* Input: image1[y][x] ---- Outout: image2[y][x] */ { /* Definition of 8-neighbor Laplacian filter */ int weight[3][3] = {{ 1, 1, 1 }, { 1, -8, 1 }, { 1, 1, 1 }}; double pixel_value; double min, max; int x, y, i, j; /* Loop variable */ /* Maximum values calculation after filtering*/ printf("Now, filtering of input image is performed\n\n"); min = DBL_MAX; max = -DBL_MAX; 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[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 exists!!!\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 image2 after linear transformtion */ 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[y + j][x + i]; } } pixel_value = MAX_BRIGHTNESS * (pixel_value - min) / (max - min); image2[y][x] = (unsigned char)pixel_value; } } } main( ) { load_image_data( ); /* Input of image1 */ laplacian_filtering( ); /* Laplacian filtering is applied to image1 */ save_image_data( ); /* Output of image2 */ return 0; }. I have this code with me.. but while putting it with the previous program with median filtering itz showing some error.. i am unable to figure it out... hw to apply this program with the previous 1 for spatial filtering?? Regards |
Hi.
Can you please post the exact code you are compiling with all necessary headers (like mypgm.h) and show exact error message. Also consider using [code] ... [/code] tags around your code. |
image processing
#include<stdio.h>
#include<stdlib.h> //#include"medianfilter.h" #include<memory.h> #include<math.h> #include"mypgm.h" #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() /* spatial filtering of image*/ /*8-neighbor Laplacian filter*/ /*input:image1[y][x]-Outout:image2[y][x]*/ { /*Definition of 8-neighbour Laplacian filter*/ int weight[3][3]={{1,1,1}, {1,-8,1}, {1,1,1}}; 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_MAX; 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[y+j][x+i]; } } if(pixe;_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]*image[y+j][x+i]; } } pixel_value=MAX_BRIGHTNESS*(pixel_value-min)/(max-min); image2[y][x]=(unsigned char)pixel_value; } } } int main(int argc,char *argv[]) { FILE *bmpInput, *bmpOutput; unsigned char *pixel; 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 <lena512.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 *) malloc(sizeof(char)*nRows*nCols); //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,pixel,nCols,nRows); load_image_data();//input of image1 laplacian_filtering();//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 mypgm.h header is not accpted due to which itz showing errors like undeclared DBL_MAX,x_size1,y_size1,MAX_BRIGHTNESS.. any alternative option is there 4 doing the laplacian filtering??? Regards |
Hi.
Seems to work: Code:
#include <stdio.h> |
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..:) |
Hi.
Good questions. Quote:
Code:
median: pixel -> result Quote:
|
image processing
now i gt it thanx alot... m done with preprocessing technique..
nw i wil start edge detection then..:) |
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 |
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..:) |
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 |
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. |
with the theory part i am all clear.. but again stuck with programming..:(((
regargds.... |
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..:( |
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> |
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 |
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..:( |
Hi.
Constant M_PI defined in math.h header. |
image processing
hey i have included that header...
still itz showing same error |
Then add
Code:
#define M_PI 3.14159265358979323846 |
im
hi,
is there anything wrong in my code,,,because it is showing segmentation fault.......:( |
im
hi,,,it worked.thanks a lot....:)....will the output of laplacian and gaussian blur b the same??..we r getting the same output.....
reguards |
I don't get the question.
|
im
hi,
the code worked. we got the output by modifyng the program with the codes that u hav posted.. In the codes that u have posted above the gaussian part was so easily represented that it didn't take me much time to understand.. is the code for other parts of canny edge detection avaiable any where..? actualy i need to do canny edge detection.. 1st part i.e gaussian blur is done. now i have left with gradient finding,non maximal suppresion and edge tracking hyteresis. since i am very new in this field itz a bit difficult for me and the project submission day is also coming nearby.. Thanx 1nce again.. u hav helped me alot with ma project.. Regards.:) |
image processing
hi,
can any 1 suggest me the c code to find the gradient of an image???? i am nt getting any specific code to understand that... Regards.. |
All times are GMT -5. The time now is 05:09 AM. |