Page 1 of 212> Show 40 post(s) from this thread on one page

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Mean filter algorithm C++ (http://www.go4expert.com/forums/mean-filter-algorithm-cpp-t19891/)

 metamofia 26Oct2009 09:04

Mean filter algorithm C++

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 26Oct2009 13:56

Re: Mean filter algorithm C++

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 26Oct2009 14:50

Re: Mean filter algorithm C++

so can i declare as 'long' instead? im not too sure about malloc..

 xpi0t0s 26Oct2009 15:08

Re: Mean filter algorithm C++

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 27Oct2009 09:12

Re: Mean filter algorithm C++

ok thanks

 xpi0t0s 27Oct2009 14:42

Re: Mean filter algorithm C++

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 27Oct2009 16:27

Re: Mean filter algorithm C++

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
else{
//Not enough memory
}

 metamofia 28Oct2009 06:33

Re: Mean filter algorithm C++

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 28Oct2009 13:30

Re: Mean filter algorithm C++

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 30Oct2009 06:57

Re: Mean filter algorithm C++

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?

 All times are GMT +5.5. The time now is 10:06. Page 1 of 212> Show 40 post(s) from this thread on one page