Hi everyone! I'm new to this forum so any advise will be very much appreciated.
I'm currently creating a C program that would read a text file full of integer values that represents pixel spectrum values of an image(satellite image) and do some computation on it so I can classify them into dry grass, road, trees, water, building,etc.
The text files contains 4 values for each pixel representing 4 bands(band 1,band 2 band 3, band 4) for each pixel so if i have a 4x5 image i would have a total of 16 rows and 5 columns. the first 4 rows would be for band 1 representation of the image,the next 4 rows would be for band 2 representation of the image...
Im using 2D arrays in storing these pixel values from the file,it works for that 4x5 image but when i use a file representing a 400x400 image it crashes. So i think it has a problem in allocating memory for my arrays.
here's a copy of the code i created.
Thanks in advance!
I'm currently creating a C program that would read a text file full of integer values that represents pixel spectrum values of an image(satellite image) and do some computation on it so I can classify them into dry grass, road, trees, water, building,etc.
The text files contains 4 values for each pixel representing 4 bands(band 1,band 2 band 3, band 4) for each pixel so if i have a 4x5 image i would have a total of 16 rows and 5 columns. the first 4 rows would be for band 1 representation of the image,the next 4 rows would be for band 2 representation of the image...
Im using 2D arrays in storing these pixel values from the file,it works for that 4x5 image but when i use a file representing a 400x400 image it crashes. So i think it has a problem in allocating memory for my arrays.
here's a copy of the code i created.
Code:
#include <stdio.h> #include <stdlib.h> #include <math.h> void stripNewline(char *string,int size); int main() { char fileName[30]; int rows, cols, bands; //user input printf("================ TEST IMAGE ===============\n"); printf("Enter the SBQ text file: "); fgets(fileName,30,stdin); stripNewline(fileName,30); printf("Enter the number of rows in the test image: "); scanf("%d",&rows); printf("Enter the number of columns in the test image: "); scanf("%d",&cols); printf("Enter the number of bands used in the test image: "); scanf("%d",&bands); printf("\n================ REFERENCE SPECTRA ===============\n"); //prepare matrix for reference int referenceArray[bands],b,i; i = 1; for(b=0;b<bands;b++){ printf("Spectrum value for Band %d: ",i); scanf("%d",&referenceArray[b]); i++; } //get data from file FILE *file; static int imageArray[20][4]; int p; printf("\nOPENING FILE...\n"); file = fopen(fileName,"r"); if(file == 0){ printf("%s could not be opened\n",fileName); //file not found or does not exist }else{ printf("%s OPENED SUCCESSFULLY\n",fileName); //opened file printf("\nPREPARING NEEDED MATRICES FOR CLASSIFICATION...\n"); //prepare matrices of each test pixel in image for(b=(bands-1);b>=0;b--){ for(p=0;p<(rows*cols);p++){ fscanf(file,"%d",&imageArray[p][b]); } } printf("\nCLASSIFYING USING SAM...\n"); //COMPUTE FOR COSINE OF ANGLE //compute for numerator int summationOfTR[(rows*cols)]; for(p=0;p<(rows*cols);p++){ summationOfTR[p] = '\0'; for(b=0;b<bands;b++){ summationOfTR[p] = summationOfTR[p] + (imageArray[p][b] * referenceArray[b]); } //printf("%d\n", summationOfTR[p]); } //compute for denominator int summationOfT2[(rows*cols)]; double sqrtSumOfT2[(rows*cols)]; int summationOfR2 = 0; double sqrtSumOfR2 = 0; double denominator[(rows*cols)]; //summationOfT2 for(p=0;p<(rows*cols);p++){ summationOfT2[p] = '\0'; for(b=0;b<bands;b++){ summationOfT2[p] = summationOfT2[p] + pow(imageArray[p][b],2); } //printf("%d\n", summationOfT2[p]); } //sqrtSumOft2 for(p=0;p<(rows*cols);p++){ sqrtSumOfT2[p] = '\0'; sqrtSumOfT2[p] = sqrt(summationOfT2[p]); } //summationofR2 for(b=0;b<bands;b++){ summationOfR2 = summationOfR2 + pow(referenceArray[b],2); } //sqrtSumOfR2 sqrtSumOfR2 = sqrt(summationOfR2); //denominator for(p=0;p<(rows*cols);p++){ denominator[p] = '\0'; denominator[p] = sqrtSumOfT2[p] * sqrtSumOfR2; } //cosine of angle double cosineOfAngles[(rows*cols)]; for(p=0;p<(rows*cols);p++){ cosineOfAngles[p] = '\0'; cosineOfAngles[p] = summationOfTR[p] / denominator[p]; if((p%(cols)) == 0){ printf("\n"); } printf("%.5lf ",cosineOfAngles[p]); } fclose(file); } getchar(); return 0; } //strip newline character '\n' after getting string input from user void stripNewline(char *string,int size){ int i; for(i=0;i<size;i++){ if(string[i] == '\n'){ string[i] = '\0'; break; } } }
Comment