Training and model based approaches for optimal stack and boolean filtering with applications in image processing