Link to home
Start Free TrialLog in
Avatar of Gupi
Gupi

asked on

Coding for Median Filter

i am coding median filtering on a TIFF image.
not sure, if it is correct. can any one please provide me its detailed algorithm on how does it work ?
Thanks.
Avatar of think-cell
think-cell

For each pixel, take the pixel values in a neighborhood around each pixel, sort them by intensity and pick the middle value. To do this, you can use std::nth_element. If you have an RGB image, do that for each channel individually.

Note that you have to copy the image in the process, or at least keep enough source pixels in a buffer to cover the neighborhood.
Avatar of Infinity08
Here's a description plus algorithm :

        http://en.wikipedia.org/wiki/Median_filter
(plus a C implementation btw)
Avatar of Gupi

ASKER

i am writing my code for Median filtering to be applied on data of a tiff file. i am not satisfied with results.
please check.
Ques 1. Check code.
Ques 2. will it clean noise of 1 pixel width, i mean if there are 2-3 pixels noise continuous, should that be removed using median filter.

Thaks.

unsigned char *I ;
 
ReadTiffFile() ;
	//it gets data of tiff file in memory pointed by I.
	//Also finds Image length, and width and stores same in Il, Iw respectively.
 
	// now start applying Median Filter on Image Data
 
void MedianFilter()
{
	unsigned long il,iw,i,j;
	il=Il;
	iw=Iw;
 
 	// form an Array named array to store resulting pixels seperately. array must be of same size as I.
 
	array= (unsigned char **)malloc(il*sizeof(unsigned char *));
	if(array==NULL)
	{
		printf("Malloc Failed !") ;
		exit(0);
	}
 
	for(i=0;i<il;i++)
	{	
		array[i]=(unsigned char *)malloc(iw*sizeof(unsigned char));
	}
	
	// now start Median Filter
	unsigned  long r,c ;
	
	for(r=0;r<il;r++)
	{
		for(c=0;c<iw;c++)
		{
			//MedianFilter each pixel (r, c)
			// form an array A[] with its neibours, excluding pixel itself.
			
			int i,j,k=0,n  ;
			unsigned char median, A[9];
			 
			if(r==0 && c==0)   //1. First Cell
			{
				//form an array of 3 neighbours
				n=3;
				A[0]= *(I+0*iw+1); // *(I[0][1]) ;
				A[1]= *(I+1*iw+0); // *I[1][0];
				A[2]= *(I+1*iw+1); // *I[1][1];
			}
			else if (r==0 && c>0 && c<iw) //2. first row
			{
				// form an array of 5 neighbours
				n=5;
				A[k++]= *(I+0*iw+c-1); //*I[0][c-1] ;
				A[k++]=  *(I+0*iw+c+1); //*I[0][c+1];
				for(int t=-1;t<2;t++)
					A[k++]= *(I+1*iw+c+t); //*I[1][c+t];
			}
			else if(r>0 && r<il && c==0) //3.first col
			{
				//form an array of 5 neighbours
				n=5;
				A[k++]=  *(I+(r-1)*iw+0); //*I[r-1][0] ;
				A[k++]= *(I+(r+1)*iw+0); //*I[r+1][0];
				for(t=-1;t<2;t++)
					A[k++]=*(I+(r+t)*iw+1); //*I[r+t][1];
			}
 
			else if(c==iw-1 && r==0) //4. Top Right Cell
			{
				n=3;
				A[k++]= *(I+0*iw+c-1);
				A[k++]= *(I+1*iw+c-1);
				A[k++]= *(I+1*iw*c);
			}
			else if(r==il-1 && c==0) // 5.Bottom Left Cell
			{
				n=3 ;
				A[k++]=*(I+(r-1)*iw+0);
				A[k++]=*(I+(r-1)*iw+1);
				A[k++]=*(I+r*iw+1);
 
			}
			else if(r==il-1 && c==iw-1) //6.Last Cell
			{
				n=3;
				A[k++]=*(I+r*iw+c-1);
				A[k++]=*(I+(r-1)*iw+c-1);
				A[k++]=*(I+(r-1)*iw+c);
			}
			else if(r==il-1 && c>0 && c<iw-1) //7. last row
			{
				n=5;
				A[k++]=*(I+r*iw+c-1) ;
				A[k++]=*(I+r*iw+c+1);
				for(t=-1;t<2;t++)
					A[k++]=*(I+(r-1)*iw+c+t);
			}
			else if(c==iw-1 && r>0 && r<il-1) //8.last col
			{
				n=5;
				A[k++]=*(I+(r-1)*iw+c);
				A[k++]=*(I+(r+1)*iw+c);
				for(t=-1;t<2;t++)
					A[k++]=*(I+(r+t)*iw+c-1);
			}
			else if(c>0 && c<iw-1 && r>0 && r<il-1) //9 anywhere in between
			{
				n=8;
 
				// all 8 elements are added here.
				A[0]=*(I+(r-1)*iw+(c-1) );
				A[1]=*(I+(r-1)*iw+c);
				A[2]=*(I+(r-1)*iw+(c+1));
 
				A[3]=*(I+(r)*iw+(c-1));
				A[4]=*(I+(r)*iw+(c+1));
 
				A[5]=*(I+(r+1)*iw+(c-1));
				A[6]=*(I+(r+1)*iw+(c));
				A[7]=*(I+(r+1)*iw+(c+1));
 
			}
			else
			{
				//ERROR
				printf(" Error "); 
				exit(0); 
			}
 
 
			//find Median of n elemets of A[]
 
			median=FindMedian(A,n);
			//store this median in array [][]
			array[r][c]=median;
		}
	}
}//end of MedianFilter()
 
 
 
unsigned char FindMedian(unsigned char A[], int n)
{
	// Find Median of n elements.
	int tmp,i,j;
	//sort using Bubble Sort
	for(i=0;i<n;i++)
	{
		for(j=0;j<n-i;j++)
		{
			if(A[j] > A[j+1])
			{
			  tmp=A[j];
			  A[j]=A[j+1];
			  A[j+1]=tmp;
			}
		}
	}
	/*
	printf("\n For %d elements, sorted values are  ",n);
	for(i=0;i<n;i++)
		printf(" %d  ", A[i]) ;
	printf(" Returning  %d", A[n/2]);
	*/
	return A[n/2];
}//end of Median()

Open in new window

Avatar of Gupi

ASKER

infinity 08, please look into this.
>> i am not satisfied with results.

Can you elaborate on that ? How are you not satisfied ?

After a quick look over the code, here are a few comments :

1) your bubble sort is wrong. It has to be :

        for (i = 0; i < n; ++i) {
            for (j = n - 1; j > i; --j) {
                if (A[j - 1] > A[j]) {
                    tmp = A[j - 1];
                    A[j - 1] = A[j];
                    A[j] = tmp;
                }
            }
        }

2) for calculating the median, you didn't take into account the pixel itself. You need to add the pixel itself to the A array too.


3) why all the globals ? Why not pass data as parameters, and use return values ?


4) for handling the edges, you have a lot of extra code. You can reduce that by just comparing the indexes with the min and max rather than having 9 separate cases. Alternatively, just leave out the edge handling entirely - it has less of an impact anyway.
Or even better, just use an already existing implementation, like the C code from the wiki.
Avatar of Gupi

ASKER

ok Infinity08,

1. bubble sort, i think my code is not wrong, it is sorting, but no problem, i will replace it with the code u provided,

2. ok, i will add pixel itself also.

3. all variables are global, bychance i have started it like this, but it should not matter much here.

4.by edge handling, do u mean,  case 1 First cell, case 2 First row, case 3 first col,  case 4 top right cell, case 5 bottom left cell, case 6 last cell, case 7 last row, case 8 last col,
if yes, i will elliminate these cases and consider only last case.

>> or even better, just use an already existing implementation like at wike.

but then i will have to change all my Image Data i.e I like their algo,
2ndly in my (small) project, i will have to justify why have i picked code from there.


so please reply, regarding 4 above so that after making  changes i can show u new code.

Please reply.
Thanks.
Avatar of Gupi

ASKER

tink-cell, Infinity08, SunnyCoder and other experts in this field,  please reply.
thanks.
Avatar of Gupi

ASKER

waited  for ur reply.

now i have written this code, is this ok, logically, i mean is this how median filter works.
My original data is in a buffer pointed by unsigned char *I,
i am filtring it and storing at new location unsigned char **arr, both I, arr are global.


void MedFiltr(const unsigned long il, const unsigned long iw)
                                     //passed arguments
{
      
    arr = (unsigned char **)malloc(il*sizeof(unsigned char *));
      if(arr==NULL)
            DisplayError("Malloc Failed !") ;
      for(c=0;c0 && c0 && r i; --j)
          {
                if (A[j - 1] > A[j])
            {
                    tmp = A[j - 1];
                    A[j - 1] = A[j];
                    A[j] = tmp;
                }
            }
        }
      return A[n/2];
}//end of Median()
Avatar of Gupi

ASKER

waiting ...
>> 1. bubble sort, i think my code is not wrong, it is sorting

I assure you it's not ;) You are comparing with out-of-bounds elements for example.


>> 3. all variables are global, bychance i have started it like this, but it should not matter much here.

No, I was just wondering.


>> 4.by edge handling, do u mean

That's what I mean, yes.


>> if yes, i will elliminate these cases and consider only last case.

You don't have to. You can keep the edge handling. I would however re-order the code so that it's a bit shorter, easier to read, and is less repetitive.


>> i will have to justify why have i picked code from there.

Justifying that is usually not complicated. Tried and tested code is usually preferable over newly written code, because it allows you to develop faster, have less testing to do, less bugs to resolve, etc.


>> now i have written this code, is this ok, logically, i mean is this how median filter works.

That looks good yes, apart from a missing ) here :

                        A[4]=*(I+(r*iw+c); //ifself

;)

If you don't want to ignore the edges, then you can still handle them ...



Btw, can you explain this a bit more :

>> >> i am not satisfied with results.
>> 
>> Can you elaborate on that ? How are you not satisfied ?
Avatar of Gupi

ASKER

ok, i can send u a tiff file with some noise in it. and u please run median filter over it. it does not give satisfying results.
actually i ran it over a tiff file in which 'B' was written,
beore it, i printed original data produced by my ReadTiffFile() function.
and a full 'B' was visible.
but when i ran this function on it, resulting text file in which i printed contents of arr[][] were on upper half of B.
so something is wrong.
now Please, tell how should i send u ReadTiffFile() function, PrintArr() and main() along with Tiff File, may i send it at ur email address, so that code does not get publicise, (please, u see, it is part of my project, so u can understand it).

please reply soon.
Thanks.
Avatar of Gupi

ASKER

>> but when i ran this function on it, resulting text file in which i printed contents of arr[][] were on upper half of B.

sorry, typo.
 
but when i ran this function on it, resulting text file in which i printed contents of arr[][] (after median filtering) were only upper half of B.

>> ok, i can send u a tiff file with some noise in it. and u please run median filter over it. it does not give satisfying results.

Can you attack the original and resulting image here ? You can upload them as attachment, so we can see the problem you are referring to.


>> in which i printed contents of arr[][] (after median filtering) were only upper half of B.

Are you sure that the il and iw values are correct ? Did you verify that the entire image was read into memory (into I) ?
What's the size of the image ? What are your values for il and iw ? What's the (filled) size of the I buffer ?
Avatar of Gupi

ASKER

yes i have checked. original tiff file when read has Image Length and width of 200 and 200 resp.
when data pointed by I is printed into a text file (int values) so that space is represented by 255 (unsigned char) that loop also runs for the same values and when Median filter is run there also values passe are same, and loops also runs for 200, 200.

i am sending u 3 files, zipped in a rar file.
b1.tif, Original B.doc, Filtrd B.doc.
Original B.doc has the text values produced by ReadTiffFile funciton printed in a text file (Copied from .txt to .doc) at font size 1 and after replacing 255 with "   " (3 spaces) for easy viewing.
similarly Filtrd B.doc also has values that were produced after running Median Filter function, stored in a text file, copied in .doc file, font size 1, 255 replaced with "   ".
Please check.


THANKS A LOT.

OOPS, .rar files are not supported here, i will have to upload these 3 file one by one.
No Problem.

Oh No !
TIFF files also not supported !
i have converted tiff file to bmp file using PaintBrush.

ORIGNIAL-B.doc
filtrd-B.doc
b1.bmp
Avatar of Gupi

ASKER

waiting ...
can't you post the data in txt files rather than doc files - I would be able to read them then ;)
the original tiff file would also be nice.
You can place all in a zip or rar file, and then rename the file to a supported extension for uploading.

Btw, was the tiff file encoded with 1 byte per pixel ? Or more than one byte ?
>> waiting ...

Btw, please don't be impatient. I am not forgetting you, but I do have other things to do in the mean time (like my job), so I cannot always respond immediately. I will respond as fast as possible though.
Avatar of Gupi

ASKER

Ok, i am sending u 3 files, Original B.txt, Filtrd B.txt and b.tif all zipped in a rar file and .rar file rename to .zip.
( i dont have free winzip software, i was using its evaluation version, that expired).
hope it works.

thanks for all ur efforts.

and i m really sorry, for getting imptaient, but i am very much worried about it, i dont have many days left to finish it. sorry agian.


B.zip
Avatar of Gupi

ASKER

i am not sure, it will work, i tried to down load it, and it gave an error.
so, please, resending the files one by one.
as u suggested, i renamed b1.tif to b1.bmp, hope it preserves its TIFF format. u please again rename it b1.tiff.

and yes, it is one byte per pixel.

Thanks.

ORIGNIAL.TXT
FILTRD.TXT
b1.bmp
I could download the rar file without a problem.

I see what you mean now. In the filtered file, it's all white below line 90.

The code you posted for MedFiltr correctly fills the arr array (completely), assuming that the il and iw values are correct (ie. both are equal to 200). Do verify that that's the case by outputting their values inside the MedFiltr function.

So, it might be an issue with the code that writes the array to the file. Can you show that code ? I noticed that after line 90, there is a column missing too (only 199 columns were in the file). So that confirms that it's probably a problem with the way you write the data to the file.
Avatar of Gupi

ASKER

This is part of main that calls filtering funtion MedFil() and then calls another function to write data in file


  printf("\n\nFiltering for rows, cols = %lu, %lu", Il,Iw);
  MedFil(Il,Iw);
  printf("\nFilterd. Storing data in filtrd.txt . . . ");
 
  //Print Filtered Data
  PrintFilteredArray(arr,Il,Iw);
  printf("\nStored.")   ;
  // rest of the code.



and body of function to write data in file is :


void PrintFilteredArray(unsigned char**arr, unsigned long nr, unsigned long nc)
{
      unsigned long i, j;
      ofstream f("filtrd.txt");
      printf("\n Starting with rows, cols =%lu,%lu", nr,nc);
      for(i=0;i
That looks good. Are you sure that this is the code that generated the file ? All lines should have the same number of values and that's not the case in the file you uploaded.

Make sure that when you make a modification to the code and re-compile, that it re-compiles the file(s) where the modification was made. You can do a make clean first to be sure.


>> so, as u can see, i have written many printfs to check that it is doing as expected.

And at no point were there wrong values ?
Avatar of Gupi

ASKER

this is the latest, clean code, that produced the files that i have sent u.

>>And at no point were there wrong values ?
yes, i have checked.

so, i guess, our MedFil function to apply medain filter (on Original B.txt file to produce Filtrd B.txt) is not doing well.
Please, what do u say.
Avatar of Gupi

ASKER

i changed MedFil() function as :
void MedFil(const unsigned long il, const unsigned long iw) 
{
	printf("\nMed Fill running for il,iw=%lu, %lu", il,iw) ;
	unsigned  long r,c ;
	arr = (unsigned char **)malloc(il*sizeof(unsigned char *));
	if(arr==NULL)
		DisplayError("Malloc Failed !") ;
	for(c=0;c<il;c++)
	{	
               arr[c]=(unsigned char *)malloc(iw*sizeof(unsigned char));
	}
 
	unsigned char tmp;
 
	for(r=0;r<il;r++)
	{
		for(c=0;c<iw;c++)
		{
			tmp=*(I+r*iw+c);
			arr[r][c]=tmp;
		}
	}
}

Open in new window

Avatar of Gupi

ASKER

so, it simply copies every pixel from I to arr.
BUT it still produced the same results, VERY SURPRISING.
so, it means part that does median filter may not be wrong.
how the values are filled in arr, that seems to be wrong.
Please check,
how i am allocating space to arr (line 5 to 11)
and how am i refering each pixel in I (line 19)
also please not, now  i have changed argumets il, iw as const unsigned long.

what can u make out of it.

I will try to print arr values in a file in the same functino where arr is being assigned values that is inside above posted function MedFil().

Avatar of Gupi

ASKER

tried following, got same result.

void MedFil(const unsigned long il, const unsigned long iw)
{
	printf("\nMed Fill running for il,iw=%lu, %lu", il,iw) ;
	unsigned  long r,c ;
	arr = (unsigned char **)malloc(il*sizeof(unsigned char *));
	if(arr==NULL)
		DisplayError("Malloc Failed !") ;
	for(c=0;c<il;c++)
	{	
               arr[c]=(unsigned char *)malloc(iw*sizeof(unsigned char));
	}
	unsigned char tmp;
 
	for(r=0;r<il;r++)
	{
		for(c=0;c<iw;c++)
		{
			tmp=*(I+r*iw+c);
			arr[r][c]=tmp;
		}
	}
 
	//print in tmp file.
 
	ofstream f("tmp.txt");
	for(r=0;r<il;r++)
	{
		printf("\n%lu", r);
		for(c=0;c<iw;c++)
		{
			f<<int(arr[r][c])<< " ";
		}
		f<<endl;
	}
}

Open in new window

Avatar of Gupi

ASKER

INTERESTINGLY !!
if i run my program on a a1.tiff in which character 'A' is written, everything works fine.

?
?

but on b1.Tif and D1.tif with 'D' in it above problem occurs.


>> Please check,
>> how i am allocating space to arr (line 5 to 11)
>> and how am i refering each pixel in I (line 19)

No, that's fine ... I already checked that in the beginning ;)



Nothing you showed so far shows a defect that would cause this behavior, so the problem has to be somewhere else. Can you post all of the code (ie. including how you read the tiff file into I, then write I to file, then call the filter, etc.)
Avatar of Gupi

ASKER

yes, but may i send it at ur email address.
You can (you'll find my e-mail address in my profile) if you don't want to upload it here on EE.
I had a look over your code, and below, you'll find what I changed to make it work. I've also sent you the modified file by mail (all modifications are commented with "// <--infinity--").


1) you included some deprecated headers. These are the includes you need at the top of the file :

        #include <cstring>            // C standard headers are prefixed with a 'c' and the '.h' is not added (1)
        #include <cstdio>             // same as (1)
        #include <iostream>        // <iostream.h> is deprecated and should not be used (2)
        #include <conio.h>
        #include <cstdlib>            // same as (1)
        #include <process.h>
        #include <cmath>             // same as (1)
        #include <fstream>          // same as (2)


2) main must return int, so :

        int main(void)

    instead of :

        void main(void)


3) several unused variables were commented, and non-standard functions were either commented (clrscr for example) or re-implemented (getw for example).


4) in this line :

        (unsigned char *)ib = (unsigned char *)ib + StripSize;

   the cast of the left-hand side is invalid, so :

        ib = (unsigned char *)ib + StripSize;

    (ib is an unsigned char* anyway)


5) modified the PrintFilteredArray function to print all values 3 characters wide, prepended with 0's if needed. It makes it easier to read the output file.


After these changes, everything works fine for me. I tested all the sample tiff files you sent.
You'll see that none of the modifications I did were about the algorithm. I just fixed some problems with the code.
Avatar of Gupi

ASKER

thanks a lot for ur reply. and please ..
>>1) you included some deprecated headers.
i tried to run the program with the headers u have specified, but my compiler was not available to find them. i checked INCLUDE sub folder in my TCC folder there were no files without .h extension.
so i have to use stdio.h instead of cstdio, and cstdio was not there.
 i am using boroland inc (turbo c++) version 3.0.
which version u tried it on., should i run it on c++ for windows version 4.5.?

>> 2) main must return int,
u have codes main() as int main()  but if i code my main as void main() and do not return anything at end, it should not matter.

>>3) several unused variables were commented.
Ok, thats fine. Good Observation !
but even if they r not commented, it should not matter much, i mean due to this only, we cant say program starts giving wrong/bad outupt.

>> 4) in this line :
thats fine. Ok.

>> 5) modified the PrintFilteredArray function
I think this was the main thing to be done, and only because of this program started behaving correctly. thanks a lot for this.
regarding unsigned short getw(FILE *fp)
i have int getw(FILE *) in stdio.h  so when i compile ur program it gives mismatch prototype error, so i will have to comment it out also.
hope program will work without it.

i will check and reply soon.
>> i am using boroland inc (turbo c++) version 3.0.

That's a pretty old compiler (1991). You might be better served with a more recent compiler (like gcc).
But first try making the other modifications (use the file I sent you, and just undo the include changes I made), and see if that makes it work. If that doesn't work, then consider switching to a more recent compiler.


>> >> 2) main must return int,
>> u have codes main() as int main()  but if i code my main as void main() and do not return anything at end, it should not matter.

No, void main() is not a valid way of defining main. It HAS to return an int, ie. int main().


>> Ok, thats fine. Good Observation !
>> but even if they r not commented, it should not matter much, i mean due to this only, we cant say program starts giving wrong/bad outupt.

Of course. It's just annoying to have to wade through all those warnings ;)



>> regarding unsigned short getw(FILE *fp)
>> i have int getw(FILE *) in stdio.h  so when i compile ur program it gives mismatch prototype error, so i will have to comment it out also.

That's fine. I only added it so I could compile your code and make it work. If it works with the version you have in stdio.h, then by all means use that.
Avatar of Gupi

ASKER

OOPs, sorry, i forgot to mention an important thing,
std::
is not working, error message is :
type qualifier std must be a struct or class.

without this ur code for PrintFilteredData becomes same as that of mine and gives same (wrong) results as i was expecting earlier.

What to do ?
Avatar of Gupi

ASKER

>> You might be better served with a more recent compiler (like gcc).
will it work on windows XP ?
>> std::
>> is not working, error message is :
>> type qualifier std must be a struct or class.

That's because your compiler is too old ;) It doesn't know about the std namespace yet.


>> without this ur code for PrintFilteredData becomes same as that of mine and gives same (wrong) results as i was expecting earlier.

Note that that wasn't the only thing I modified ... I also added the setw and setfill modifiers.


>> What to do ?

If it still doesn't work for you, then I would really suggest using a more recent compiler.

I assure you the code works for me. And I kind of expected that, since I couldn't see anything in your code that would explain the behavior.

If you are on Windows, you can try the free Dev-C++ IDE :

        http://www.bloodshed.net/devcpp.html
Avatar of Gupi

ASKER

i tried to download from link provided by u. downloaded and installed,  but could not set its library paths.
so Now i am using bloodshed Dev-C++ version 4.0 from bloodshed.net, with Mingw compiler.


i am getting these errors :

c:\dev-cpp\bin\p1.cpp: In function `short unsigned int getw(FILE *)':
c:\dev-cpp\bin\p1.cpp:360: new declaration `short unsigned int getw(FILE *)'
C:\DEV-C_~1\Include\stdio.h:367: ambiguates old declaration `int getw(FILE *)'

so there are two versions of getw() which make it ambigous.


if i commentout #include<cstdio>
then i get 31 errors like :
c:\dev-cpp\bin\p1.cpp: In function `int main()':
c:\dev-cpp\bin\p1.cpp:60: implicit declaration of function `int printf(...)'
c:\dev-cpp\bin\p1.cpp:91: implicit declaration of function `int getchar(...)'
c:\dev-cpp\bin\p1.cpp: At top level:
c:\dev-cpp\bin\p1.cpp:359: `FILE' was not declared in this scope
c:\dev-cpp\bin\p1.cpp:359: `fp' was not declared in this scope
:
:


if i comment the getw function , so that all calls to getw() are to sent cstdio then
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccGcdaaa.o(.text+0xf4a):p1.cpp: undefined reference to `ostream & operator<<<int>(ostream &, smanip<int> const &)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccGcdaaa.o(.text+0xf55):p1.cpp: undefined reference to `ostream & operator<<<int>(ostream &, smanip<int> const &)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccGcdaaa.o(.text+0xf60):p1.cpp: undefined reference to `ostream::operator<<(int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccGcdaaa.o(.text+0xf6b):p1.cpp: undefined reference to `ostream::operator<<(char const *)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccGcdaaa.o(.text+0xf84):p1.cpp: undefined reference to `endl(ostream &)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccGcdaaa.o(.text+0xfad):p1.cpp: undefined reference to `fstreambase::close(void)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccGcdaaa.o(.text$__8ofstreamiPCcii+0xab):p1.cpp: undefined reference to `fstreambase::fstreambase(int, char const *, int, int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccGcdaaa.o(.text$setfill__Fi+0x11):p1.cpp: undefined reference to `__iomanip_setfill(ios &, int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccGcdaaa.o(.text$setw__Fi+0x11):p1.cpp: undefined reference to `__iomanip_setw(ios &, int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccGcdaaa.o(.text$_$_11fstreambase+0x60):p1.cpp: undefined reference to `filebuf::~filebuf(void)'


if i rename function getw() to Getw()  so that getw in cstdio and getw written by u are not ambigous, also the calls made to getw() to Getw() so that all calls are to local Getw()    then i get errors :

C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccWkbaaa.o(.text+0xf4a):p1.cpp: undefined reference to `ostream & operator<<<int>(ostream &, smanip<int> const &)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccWkbaaa.o(.text+0xf55):p1.cpp: undefined reference to `ostream & operator<<<int>(ostream &, smanip<int> const &)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccWkbaaa.o(.text+0xf60):p1.cpp: undefined reference to `ostream::operator<<(int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccWkbaaa.o(.text+0xf6b):p1.cpp: undefined reference to `ostream::operator<<(char const *)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccWkbaaa.o(.text+0xf84):p1.cpp: undefined reference to `endl(ostream &)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccWkbaaa.o(.text+0xfad):p1.cpp: undefined reference to `fstreambase::close(void)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccWkbaaa.o(.text$__8ofstreamiPCcii+0xab):p1.cpp: undefined reference to `fstreambase::fstreambase(int, char const *, int, int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccWkbaaa.o(.text$setfill__Fi+0x11):p1.cpp: undefined reference to `__iomanip_setfill(ios &, int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccWkbaaa.o(.text$setw__Fi+0x11):p1.cpp: undefined reference to `__iomanip_setw(ios &, int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccWkbaaa.o(.text$_$_11fstreambase+0x60):p1.cpp: undefined reference to `filebuf::~filebuf(void)'



if i comment out whole PrintFilteredArray Function, then program runs. But we cant leave this function.


so, what should i do?
Are you compiling this as a C or C++ project ? It should be a C++ project. Did you use the exact file I sent you via e-mail ?
>> if i rename function getw() to Getw()  so that getw in cstdio and getw written by u are not ambigous, also the calls made to getw() to Getw() so that all calls are to local Getw()

Oh, and that's a good idea to do anyway ;)
Avatar of Gupi

ASKER

yes, i am running it as it is, File is saved in C:\Dev-C++\Bin and is a CPP file so it means it is a C++ Project.

when i compile it from  Execute -> Compile it gives
2 Errors
0 Bytes Output file.
Errors are :
359 c:\dev-c_~1\bin\p1.cpp    new declaration `short unsigned int getw(FILE *)'
367 c:\dev-c_~1\include\stdio.h    ambiguates old declaration `int getw(FILE *)'


Then i changed ur defintion of getw to Getw without changing any calls made to it, so that now calls made to getw are mapped to inbuilt funciton.
when i compiled it agian, it shows:
Total Errors 0,
Size of OutPut File 0 byte.
 and Compiler and Linker Output window shows :
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccCmcaaa.o(.text+0xf4a):p1.cpp: undefined reference to `ostream & operator<<<int>(ostream &, smanip<int> const &)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccCmcaaa.o(.text+0xf55):p1.cpp: undefined reference to `ostream & operator<<<int>(ostream &, smanip<int> const &)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccCmcaaa.o(.text+0xf60):p1.cpp: undefined reference to `ostream::operator<<(int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccCmcaaa.o(.text+0xf6b):p1.cpp: undefined reference to `ostream::operator<<(char const *)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccCmcaaa.o(.text+0xf84):p1.cpp: undefined reference to `endl(ostream &)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccCmcaaa.o(.text+0xfad):p1.cpp: undefined reference to `fstreambase::close(void)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccCmcaaa.o(.text$__8ofstreamiPCcii+0xab):p1.cpp: undefined reference to `fstreambase::fstreambase(int, char const *, int, int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccCmcaaa.o(.text$setfill__Fi+0x11):p1.cpp: undefined reference to `__iomanip_setfill(ios &, int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccCmcaaa.o(.text$setw__Fi+0x11):p1.cpp: undefined reference to `__iomanip_setw(ios &, int)'
C:\DOCUME~1\SANDEE~1\LOCALS~1\Temp\ccCmcaaa.o(.text$_$_11fstreambase+0x60):p1.cpp: undefined reference to `filebuf::~filebuf(void)'


What is this all ?
i have not worked earlier in Dev C++, thats why i am troubling again and again. sorry.

Please reply.
Thanks.
>> File is saved in C:\Dev-C++\Bin

You shouldn't save it in the bin folder. Place it somewhere in your workspace (wherever that is).

Then open it in the Dev-C++ IDE, and rebuild it.


Are you sure that the installation of Dev-C++ finished successfully (why did you take version 4 btw ? There is a version 5 available).


>> Then i changed ur defintion of getw to Getw without changing any calls made to it, so that now calls made to getw are mapped to inbuilt funciton.
>> when i compiled it agian, it shows:
>> Total Errors 0,
>> Size of OutPut File 0 byte.
>>  and Compiler and Linker Output window shows :

Can you send me the exact file you got these errors with.
Avatar of Gupi

ASKER

yes sure. here it comes, saved it .txt becuase here for uploading .cpp is not allowed.
Thanks.
P1.txt
You apparently made some other modifications to the code ;)

In the PrintFilteredArray function, you have :

>>  ofstream g;

This needs the std:: namespace quelification :

        std::ofstream g;


It seems you also removed several bits of code I added. All of those modifications were added for a reason ...


I will also attach the file I sent you earlier by e-mail, with modifications to make it compile in Dev-C++. I'd prefer you test that one.
P1-mod.cpp.txt
Avatar of Gupi

ASKER

i m sorry, while trying to debug, i made some changes.

YES !!  it is working in Dev-C++.

but it is not working in Turbo C++. this is very strange, same program gives differnt output, when run using different compilers of C++ (Question 1 Why)
when i tried to run above program p1mod.cpp in turbo c++, first it gave errors for using std. when i removed all std:: and run it on b1.tif, it gave wrong output.

but you have helped me a lot.
Hats Off to You !
i have no words to express my thanks.

anyway, code is correct now. but still logic/algorithm of MedFil function for Median Filtering is wrong.
you can verify it from the filterd.doc file, i am sending.
so, i think, this function needs more work. as u can see, b2 original has much noise, b2 filtered.doc has comparitvly less noise but still i has some. and it will affect next part of my program where by i create bouding box. i am seding u b2 bouded.doc also which i get after running CreateBoundingBox function on the data produced after filtering. and u can see, wrong filtering affects size of boudnig box also.
what do u say ? (Question 2)

THANKS A LOT.

P.S : Here for Uploadeing why .tiff files are not supported (ques 3).
Results.zip
>> so, i think, this function needs more work. as u can see, b2 original.doc has much noise, b2 filtered.doc has comparitvly less noise but still it has some.

there were typing mistakes.
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, i will post a new question on efficiency of Median Algorithm.
now i close this question.
you were very co-operative during this whle discussion.
you have listened all my questions very patiently and replied very intelligently.
i am fully satisfied.
Thanks a lot.
oh, there is not Accept Answer button. how do i give points to u.
Hi, it seems you are logged in as a different user. The question was asked by Gupi.
oh yes.
yes, we are working in a team.
What I meant is that the person that asked the question also has to close it ;)