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 |
All times are GMT -5. The time now is 10:53 PM. |