microsoft / Lumia-imaging-sdk

Lumia Imaging SDK is a comprehensive set of GPU/CPU imaging tools and effects that run on both mobile and desktop, with high speed and memory efficiency. Samples and extras code are included.
MIT License
75 stars 37 forks source link

LensBlurEffect.LensBlurFocusAreaEdgeMirroring dont' work #6

Closed JuniperPhoton closed 8 years ago

JuniperPhoton commented 8 years ago

I want there is a smooth transition between the focus area and the unfocus area. However, whatever I try, the transition between those two area is arbitrary.

Here is my code:

//Draw a bitmap that contains a white circle in a black rect.
var bmp = new WriteableBitmap(canvasWidth, canvasHeight);
bmp.FillRectangle(0, 0, canvasWidth, canvasHeight, Colors.Black);
bmp.FillEllipseCentered((int)centerX, (int)centerY, (int)BlurCircle.ActualWidth/2, (int)BlurCircle.ActualHeight/2, Colors.White);

var src = new StorageFileImageSource(backgroundFile);
var masksrc = new BitmapImageSource((Bitmap)bmp.AsBitmap());
var blurEffect = new LensBlurEffect(src, masksrc);
blurEffect.FocusAreaEdgeMirroring = LensBlurFocusAreaEdgeMirroring.On;
blurEffect.FocusEdgeSoftening = new LensBlurFocusEdgeSoftening(30);
blurEffect.BlendKernelWidth = 255;
blurEffect.Kernels = new LensBlurPredefinedKernel[] { new LensBlurPredefinedKernel(LensBlurPredefinedKernelShape.Circle, 30) };

using(var renderer=new JpegRenderer(blurEffect)
{
      //...do render thing.
}
davidbozjak commented 8 years ago

Hi,

Have you given taken a look at the DepthOfField Extras? We realized that the smooth transition lens blur is an effect that is hard to achieve, so we created several samples to help developers. They can be found right here on GitHub, and will show you how to achieve a smooth transition between kernels and how to generate a mask with the SDK, without having to worry about the WriteableBitmap.

For your case in particular we have written the EllipticFocusDepthOfFieldEffect (https://github.com/Microsoft/Lumia-imaging-sdk/blob/master/Extras/Lumia.Imaging.Extras.ImageProviders/Lumia.Imaging.Extras.ImageProviders.Shared/DepthOfField/EllipticFocusDepthOfFieldEffect.cs). You can use that as inspiration, or include it as source code in your application, or just use pieces of it!

Please take a look at the entire concepts, including tests: https://github.com/Microsoft/Lumia-imaging-sdk/tree/master/Extras/Lumia.Imaging.Extras.ImageProviders/Lumia.Imaging.Extras.ImageProviders.Shared/DepthOfField

As for your sample code in particular, I see two obvious shortcomings:

Please note that the size of the kernels themselves should also depend on the size of your image.

JuniperPhoton commented 8 years ago

Thanks for replying. EllipticFocusDepthOfFieldEffect does help :-D !