chaylock
02/12/2006, 12:33
Merhabalar..
Çok iyi bir C programlayıcısı değilim, bir image-processing projem için karşılaştığım bu hatanın nedenini bulamıyorum..
Başka bir fonksiyon çağırıyorum ve bu hatayı veriyor.. Diğer fonksiyonun içine printf koydum hemen başa, onu print ettikten sonra patladı bu sefer.. Hemen altına bir başka printf koyunca onu göstermeden patladı.. Çok saçma geliyor kulağa.. Nerede hata yaptığımı gösterirseniz ileri düzey C programcıları, çok sevinirim.. Google' dan da araştırmaya devam ediyorum bir yandan..
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
/*
*
* Tolga Arican
* Project 02
* 10376004.c
*
*/
// prints SNR report
void SNR(int* op,int* np,int tp,char* name) {
printf(">> SNR started..\n");printf("a");
// SNR stuff:
// sum of old value and sum of (new value - old value)
int sum1=0;
int sum2=0;
// values
int j = 0;
int k = 0;
// loop cursor
int cur = 0;
// this loop does the summation
for(cur ; cur < tp ; cur = cur + 1)
{
// old file gray value
j = op[cur];
// new file gray value
k = np[cur];
// SNR calculations:
sum1 = sum1 + k;
sum2 = sum2 + (k - j);
}
// result value of SNR function
double result;
result = sum1 / sum2;
// analyzing the result
if (result < 0)
printf (">>> SNR REPORT (%s): Too much corrupted (SNR = %f)\n",name,result);
else
printf (">>> SNR REPORT (%s): NOT Too much corrupted (SNR = %f)\n",name,result);
}
// calculates histogram of the picture and returns an array
int* calculateHistogram(int* picture,char* histname,int col,int row) {
// creating a histogram file
FILE *hfile;
hfile = fopen(histname, "w");
char array[10] = {0};
// total pixels
int tp = row * col;
// temp. array
int result[256];
// a cursor
int c1 = 0;
// default array with 0' s in it
for (c1 ; c1<256 ; c1=c1+1)
result[c1] = 0;
// temp. integer - takes the value of picture
int i;
// for cursor
int cur = 0;
// cursor moves in the file and calculates histogram
for(cur ; cur < tp ; cur = cur + 1)
{
// takes the value
i = picture[cur];
// increases the histogram
result[i] = result[i] + 1;
}
// for cursor
cur = 0;
// creating histogram file
for(cur ; cur < 256 ; cur = cur + 1)
{
// gets histogram value and puts to char array
sprintf(array, "%d\n", result[cur]);
// puts the value to histogram file
fputs(array, hfile);
}
// closing file
fclose(hfile);
// returns temp. array
return result;
}
void saltAndPepper() {
printf(">> Salt & Pepper Function started..\n");
// opens cameraman picture
FILE *f;
f=fopen("cameramangray.pgm","r");
// creating a new picture
FILE *pfile;
pfile = fopen("cameraman_saltpepper.pgm", "w");
// cursor in the old file
char *z;
// col & row of the image
int col,row;
int tmp;
// a cursor that moves in file
z=malloc(100);
// error handling
if(!z)
printf("error in memory allocation for the string");
// pgm default things
fgets(z, 100, f);
fputs(z, pfile);
fgets(z, 100, f);
fputs(z, pfile);
fgets(z, 100, f);
fputs(z, pfile);
// info message
printf(">> new image header created..\n");
// taking row and col sizes of picture
sscanf(z,"%d %d",&row,&col);
// total pixels of image
int tp = col*row;
// total pixels that will changed
int total_pix_numb = (int) (tp * 0.05);
// info message
printf(">> image info:\n\tcol = %d\n\trow = %d\n\ttotal pixels = %d\n\ttotal changing pixels = %d\n",col,row,tp,total_pix_numb);
// file arrays
int old_pic[tp];
int new_pic[tp];
// pgm default things
fgets(z, 100, f);
fputs(z, pfile);
// a cursor
int i = 0;
// filling picture pixels to array
while(fscanf(f,"%d",&tmp)!=EOF)
{
old_pic[i]=tmp;
i = i+1;
}
// a cursor that moves in array
int cur = 0;
// a temp. character array
char array[10] = {0};
// new picture array is before adding noise
for (cur; cur < tp; cur = cur + 1) {
new_pic[cur] = old_pic[cur];
}
cur = 0;
// random number
int rn;
// histogram of old file
int* histogram = calculateHistogram(old_pic,"H_Cameraman.txt",col,row);
// info message
printf(">> calculating noises..\n");
// SALT (250) LOOP
for (cur; cur < total_pix_numb; cur = cur + 1) {
// generates random number
rn = rand() % tp;
new_pic[rn]=250;
}
cur = 0;
// PEPPER (20) LOOP
for (cur; cur < total_pix_numb; cur = cur + 1) {
// generates random number
rn = rand() % tp;
new_pic[rn]=20;
}
// info message
printf(">> writing new image file..\n");
cur = 0;
// temp. integer takes new picture values
int k = 0;
// this loop creates the new file
for(cur; cur < tp; cur = cur + 1)
{
k = new_pic[cur];
// puts calculated value to temp. array
sprintf(array, "%d", k);
// puts the array to new file
fputs(array, pfile);
// puts a space
fputs(" ", pfile);
// putting newline
if (cur%8==0)
fputs("\n", pfile);
}
int* new_h = calculateHistogram(new_pic,"H_Salt_Pepper.txt",col,row);
SNR(old_pic,new_pic,tp,"Salt & Pepper");
// info message
printf(">> Salt & Pepper Function completed!\n\n");
// closing files
fclose(f);
fclose(pfile);
}
void uniform(int lower, int upper) {
printf(">> Uniform Function started..\n");
// opens cameraman picture
FILE *f;
f=fopen("cameramangray.pgm","r");
// creating a new picture
FILE *pfile;
pfile = fopen("cameraman_uniform.pgm", "w");
// cursor in the old file
char *z;
// col & row of the image
int col,row;
int tmp;
// a cursor that moves in file
z=malloc(100);
// error handling
if(!z)
printf("error in memory allocation for the string");
// pgm default things
fgets(z, 100, f);
fputs(z, pfile);
fgets(z, 100, f);
fputs(z, pfile);
fgets(z, 100, f);
fputs(z, pfile);
// info message
printf(">> new image header created..\n");
// taking row and col sizes of picture
sscanf(z,"%d %d",&row,&col);
// total pixels of image
int tp = col*row;
// total pixels that will changed
int total_pix_numb = (int) (tp * (1 / upper - lower));
// info message
printf(">> image info:\n\tcol = %d\n\trow = %d\n\ttotal pixels = %d\n\ttotal changing pixels = %d\n\tlower bound = %d\n\tupper bound = %d\n\n",col,row,tp,total_pix_numb,lower,upper);
// file arrays
int old_pic[tp];
int new_pic[tp];
// pgm default things
fgets(z, 100, f);
fputs(z, pfile);
// a cursor
int i = 0;
// filling picture pixels to array
while(fscanf(f,"%d",&tmp)!=EOF)
{
old_pic[i]=tmp;
i = i+1;
}
// a cursor that moves in array
int cur = 0;
// a temp. character array
char array[10] = {0};
// new picture array is before adding noise
for (cur; cur < tp; cur = cur + 1) {
new_pic[cur] = old_pic[cur];
}
cur = 0;
// histogram of old file
int* histogram = calculateHistogram(old_pic,"H_Cameraman.txt",col,row);
// info message
printf(">> calculating noises..\n");
// inner loop cursor
int cur2 = 0;
// temp value
int val=0;
// random number
int rn;
// POSITIVE LOOP
for (cur; cur <= upper; cur = cur + 1) {
for(cur2; cur2 < total_pix_numb; cur2 = cur2 + 1) {
// generates random number
rn = rand() % tp;
// taking the value
val = new_pic[rn];
// checking the new value
if (val + cur < 255)
new_pic[rn] = val + cur;
else
new_pic[rn] = 255;
}
}
cur = 0;
cur2 = 0;
// NEGATIVE LOOP
for (cur; cur >= lower; cur = cur - 1) {
for(cur2; cur2 < total_pix_numb; cur2 = cur2 + 1) {
// generates random number
rn = rand() % tp;
// taking the value
val = new_pic[rn];
// checking the new value
if (val + cur > 0)
new_pic[rn] = val + cur;
else
new_pic[rn] = 0;
}
}
// info message
printf(">> writing new image file..\n");
cur = 0;
// temp. integer takes new picture values
int k = 0;
// this loop creates the new file
for(cur; cur < tp; cur = cur + 1)
{
k = new_pic[cur];
// puts calculated value to temp. array
sprintf(array, "%d", k);
// puts the array to new file
fputs(array, pfile);
// puts a space
fputs(" ", pfile);
// putting newline
if (cur%8==0)
fputs("\n", pfile);
}
int* new_h = calculateHistogram(new_pic,"H_Uniform.txt",col,row);
SNR(old_pic,new_pic,tp,"Uniform");
// info message
printf(">> Uniform Function completed!\n\n");
// closing files
fclose(f);
fclose(pfile);
}
int main ()
{
// info message
printf("\n> program started!\n\n");
// saltAndPepper();
uniform(-47,47);
return 1;
}
uniform(-47,47);
satırını çağırdıktan sonra o fonksiyon çalışıyor ama son satırdı SNR' ı çağırdığında olan oluyor.. SNR' ı çağırdığı satırı silince düzeliyor ?!?!?
teşekkürler şimdiden..
Çok iyi bir C programlayıcısı değilim, bir image-processing projem için karşılaştığım bu hatanın nedenini bulamıyorum..
Başka bir fonksiyon çağırıyorum ve bu hatayı veriyor.. Diğer fonksiyonun içine printf koydum hemen başa, onu print ettikten sonra patladı bu sefer.. Hemen altına bir başka printf koyunca onu göstermeden patladı.. Çok saçma geliyor kulağa.. Nerede hata yaptığımı gösterirseniz ileri düzey C programcıları, çok sevinirim.. Google' dan da araştırmaya devam ediyorum bir yandan..
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
/*
*
* Tolga Arican
* Project 02
* 10376004.c
*
*/
// prints SNR report
void SNR(int* op,int* np,int tp,char* name) {
printf(">> SNR started..\n");printf("a");
// SNR stuff:
// sum of old value and sum of (new value - old value)
int sum1=0;
int sum2=0;
// values
int j = 0;
int k = 0;
// loop cursor
int cur = 0;
// this loop does the summation
for(cur ; cur < tp ; cur = cur + 1)
{
// old file gray value
j = op[cur];
// new file gray value
k = np[cur];
// SNR calculations:
sum1 = sum1 + k;
sum2 = sum2 + (k - j);
}
// result value of SNR function
double result;
result = sum1 / sum2;
// analyzing the result
if (result < 0)
printf (">>> SNR REPORT (%s): Too much corrupted (SNR = %f)\n",name,result);
else
printf (">>> SNR REPORT (%s): NOT Too much corrupted (SNR = %f)\n",name,result);
}
// calculates histogram of the picture and returns an array
int* calculateHistogram(int* picture,char* histname,int col,int row) {
// creating a histogram file
FILE *hfile;
hfile = fopen(histname, "w");
char array[10] = {0};
// total pixels
int tp = row * col;
// temp. array
int result[256];
// a cursor
int c1 = 0;
// default array with 0' s in it
for (c1 ; c1<256 ; c1=c1+1)
result[c1] = 0;
// temp. integer - takes the value of picture
int i;
// for cursor
int cur = 0;
// cursor moves in the file and calculates histogram
for(cur ; cur < tp ; cur = cur + 1)
{
// takes the value
i = picture[cur];
// increases the histogram
result[i] = result[i] + 1;
}
// for cursor
cur = 0;
// creating histogram file
for(cur ; cur < 256 ; cur = cur + 1)
{
// gets histogram value and puts to char array
sprintf(array, "%d\n", result[cur]);
// puts the value to histogram file
fputs(array, hfile);
}
// closing file
fclose(hfile);
// returns temp. array
return result;
}
void saltAndPepper() {
printf(">> Salt & Pepper Function started..\n");
// opens cameraman picture
FILE *f;
f=fopen("cameramangray.pgm","r");
// creating a new picture
FILE *pfile;
pfile = fopen("cameraman_saltpepper.pgm", "w");
// cursor in the old file
char *z;
// col & row of the image
int col,row;
int tmp;
// a cursor that moves in file
z=malloc(100);
// error handling
if(!z)
printf("error in memory allocation for the string");
// pgm default things
fgets(z, 100, f);
fputs(z, pfile);
fgets(z, 100, f);
fputs(z, pfile);
fgets(z, 100, f);
fputs(z, pfile);
// info message
printf(">> new image header created..\n");
// taking row and col sizes of picture
sscanf(z,"%d %d",&row,&col);
// total pixels of image
int tp = col*row;
// total pixels that will changed
int total_pix_numb = (int) (tp * 0.05);
// info message
printf(">> image info:\n\tcol = %d\n\trow = %d\n\ttotal pixels = %d\n\ttotal changing pixels = %d\n",col,row,tp,total_pix_numb);
// file arrays
int old_pic[tp];
int new_pic[tp];
// pgm default things
fgets(z, 100, f);
fputs(z, pfile);
// a cursor
int i = 0;
// filling picture pixels to array
while(fscanf(f,"%d",&tmp)!=EOF)
{
old_pic[i]=tmp;
i = i+1;
}
// a cursor that moves in array
int cur = 0;
// a temp. character array
char array[10] = {0};
// new picture array is before adding noise
for (cur; cur < tp; cur = cur + 1) {
new_pic[cur] = old_pic[cur];
}
cur = 0;
// random number
int rn;
// histogram of old file
int* histogram = calculateHistogram(old_pic,"H_Cameraman.txt",col,row);
// info message
printf(">> calculating noises..\n");
// SALT (250) LOOP
for (cur; cur < total_pix_numb; cur = cur + 1) {
// generates random number
rn = rand() % tp;
new_pic[rn]=250;
}
cur = 0;
// PEPPER (20) LOOP
for (cur; cur < total_pix_numb; cur = cur + 1) {
// generates random number
rn = rand() % tp;
new_pic[rn]=20;
}
// info message
printf(">> writing new image file..\n");
cur = 0;
// temp. integer takes new picture values
int k = 0;
// this loop creates the new file
for(cur; cur < tp; cur = cur + 1)
{
k = new_pic[cur];
// puts calculated value to temp. array
sprintf(array, "%d", k);
// puts the array to new file
fputs(array, pfile);
// puts a space
fputs(" ", pfile);
// putting newline
if (cur%8==0)
fputs("\n", pfile);
}
int* new_h = calculateHistogram(new_pic,"H_Salt_Pepper.txt",col,row);
SNR(old_pic,new_pic,tp,"Salt & Pepper");
// info message
printf(">> Salt & Pepper Function completed!\n\n");
// closing files
fclose(f);
fclose(pfile);
}
void uniform(int lower, int upper) {
printf(">> Uniform Function started..\n");
// opens cameraman picture
FILE *f;
f=fopen("cameramangray.pgm","r");
// creating a new picture
FILE *pfile;
pfile = fopen("cameraman_uniform.pgm", "w");
// cursor in the old file
char *z;
// col & row of the image
int col,row;
int tmp;
// a cursor that moves in file
z=malloc(100);
// error handling
if(!z)
printf("error in memory allocation for the string");
// pgm default things
fgets(z, 100, f);
fputs(z, pfile);
fgets(z, 100, f);
fputs(z, pfile);
fgets(z, 100, f);
fputs(z, pfile);
// info message
printf(">> new image header created..\n");
// taking row and col sizes of picture
sscanf(z,"%d %d",&row,&col);
// total pixels of image
int tp = col*row;
// total pixels that will changed
int total_pix_numb = (int) (tp * (1 / upper - lower));
// info message
printf(">> image info:\n\tcol = %d\n\trow = %d\n\ttotal pixels = %d\n\ttotal changing pixels = %d\n\tlower bound = %d\n\tupper bound = %d\n\n",col,row,tp,total_pix_numb,lower,upper);
// file arrays
int old_pic[tp];
int new_pic[tp];
// pgm default things
fgets(z, 100, f);
fputs(z, pfile);
// a cursor
int i = 0;
// filling picture pixels to array
while(fscanf(f,"%d",&tmp)!=EOF)
{
old_pic[i]=tmp;
i = i+1;
}
// a cursor that moves in array
int cur = 0;
// a temp. character array
char array[10] = {0};
// new picture array is before adding noise
for (cur; cur < tp; cur = cur + 1) {
new_pic[cur] = old_pic[cur];
}
cur = 0;
// histogram of old file
int* histogram = calculateHistogram(old_pic,"H_Cameraman.txt",col,row);
// info message
printf(">> calculating noises..\n");
// inner loop cursor
int cur2 = 0;
// temp value
int val=0;
// random number
int rn;
// POSITIVE LOOP
for (cur; cur <= upper; cur = cur + 1) {
for(cur2; cur2 < total_pix_numb; cur2 = cur2 + 1) {
// generates random number
rn = rand() % tp;
// taking the value
val = new_pic[rn];
// checking the new value
if (val + cur < 255)
new_pic[rn] = val + cur;
else
new_pic[rn] = 255;
}
}
cur = 0;
cur2 = 0;
// NEGATIVE LOOP
for (cur; cur >= lower; cur = cur - 1) {
for(cur2; cur2 < total_pix_numb; cur2 = cur2 + 1) {
// generates random number
rn = rand() % tp;
// taking the value
val = new_pic[rn];
// checking the new value
if (val + cur > 0)
new_pic[rn] = val + cur;
else
new_pic[rn] = 0;
}
}
// info message
printf(">> writing new image file..\n");
cur = 0;
// temp. integer takes new picture values
int k = 0;
// this loop creates the new file
for(cur; cur < tp; cur = cur + 1)
{
k = new_pic[cur];
// puts calculated value to temp. array
sprintf(array, "%d", k);
// puts the array to new file
fputs(array, pfile);
// puts a space
fputs(" ", pfile);
// putting newline
if (cur%8==0)
fputs("\n", pfile);
}
int* new_h = calculateHistogram(new_pic,"H_Uniform.txt",col,row);
SNR(old_pic,new_pic,tp,"Uniform");
// info message
printf(">> Uniform Function completed!\n\n");
// closing files
fclose(f);
fclose(pfile);
}
int main ()
{
// info message
printf("\n> program started!\n\n");
// saltAndPepper();
uniform(-47,47);
return 1;
}
uniform(-47,47);
satırını çağırdıktan sonra o fonksiyon çalışıyor ama son satırdı SNR' ı çağırdığında olan oluyor.. SNR' ı çağırdığı satırı silince düzeliyor ?!?!?
teşekkürler şimdiden..