I really dont know what to say!! Thank you so so much. The links look very beneficial!! I had a look at them. But if possible, i need one small thing too. For the foreground extraction i need to use this criterion. B= arg min(∑Wk > T ) . We pick the first B distributions from the sorting according to this formula. The boundaries of the summation is k=1 to k=b. I dont know how to write this in MATLAB and I am really having hard time. Not much time left for me to finish the project but i should add this thing to my program. If you can tell me just have to write a MATLAB code for the above summation, you can never imagine how happy will I be!!! I need your help my colleagues. I am watining for a reply. Thank you all in advance!!!