Mean filter algorithm C++

metamofia's Avatar
Go4Expert Member
hi, i read up some articles about mean filter and its use-age. however im connfused about it when it comes to coding it. can someone explain to me in mathematical term on how its done? this is what i implemented so far, help from here one will be great:

Code:
void
CLEO_MedivisionView::OnNoiseremovalMedianfilter() {

unsigned int meanarray[262144];  
int i;  

for (i=0; i<262144; i++) {
meanarray[i]=0;
}
 
for (i=2; i<262144-2; i++) {
meanarray[i - 2]= (image[i - 2] +image[i - 1] +image[i] +image[i + 1] +image[i + 2]) / 5;
}
thing to note is when i debug at the breakpoint, i get an error saying that the stack is overflow or something like that.
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
How much stack space have you got?
Code:
unsigned int meanarray[262144];
... could account for the problem; the stack does not usually have unlimited storage and you could be better off creating meanarray on the heap instead (with malloc, not forgetting to free it when you've done).

int is 4 bytes so this allocates 1MB of stack space.
metamofia's Avatar
Go4Expert Member
so can i declare as 'long' instead? im not too sure about malloc..
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
No, because long is also 4 bytes. If you don't want to use malloc (why not?) then you need to read your compiler documentation to find out how to increase the stack size.

malloc'd arrays are just as easy to use as stack arrays. Here's a stack example:
Code:
int arr[32];
arr[1]=27;
and here's a malloc example:
Code:
int *arr=(int*)malloc(32*sizeof(int));
arr[1]=27;
free(arr);
so apart from malloc and free, the usage is exactly the same.
metamofia's Avatar
Go4Expert Member
ok thanks

what is this for in your reply? : arr[1]=27;
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
It's an example of how to use it. It sets the 2nd element of arr to 27. As you can see, there is no difference between the stack version and the heap version.
micsom_micsom's Avatar
Go4Expert Member
i would like to add one more point, though its not really necessary , it will still work without this Check, but its adviced to have these checks.

int *p=(int*)malloc(32*sizeof(int));
if(NULL != p)
//Check to see proper allocation of memory
....Do your Stuff....
else{
//Not enough memory
}
metamofia's Avatar
Go4Expert Member
okay it settled the stack overflow problem. so coming back to the MAIN issue.
looking at this codeline:

Code:
 
for (i=2; i<262144-2; i++) {
meanarray[i - 2]= (image[i - 2] +image[i - 1] +image[i] +image[i + 1] +image[i + 2]) / 5;
} //  ----> toggled breakpoint
what i want to do is to find the average values of image[0] to image[4] WHEN i=2. so what i should get in meanarray[] is the average of the 5 image[] values until the condition ends at i<262144-2.. at the moment when i debug at the breakpoint shown and return the values for meanarray[], i dont get any values.. what i get was showing meanarray is not even an array! it just shows me ZERO. i really need help on this. thanks
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
The previous point about checking the return value of malloc is absolutely correct. Much of programming is about handling error conditions properly.

What are the values in image[0]..image[4], and exactly what result in meanarray[0] are you expecting to see?
Have you tried checking the algorithm in a simple testbed, e.g.
Code:
main()
{
  int *image={2,4,8,1,9,16,3,47,6,13};
  int meanarray[8];
  for (int i=2; i<8; i++)
  {
    meanarray[i-2]=(image[i-2]+...[image[i+2])/5;
  }
}
(there may be errors in that; I just typed it straight in without testing).
I would expect meanarray[0] to contain 4, in this case (2+4+8+1+9=24; 24/5=4).
metamofia's Avatar
Go4Expert Member
Hi thanks for replying. I managed to have the mean values now. AQctually i solved the stack overflow issue by doing 2 things: first i changed my variable to meanarray1[] instead and i declare this into the global definition way up in this whole programme.

I now want to do median filter. I am unsure how to sort the arrays out from increasing order. Can u give me some hints on how to go about?