Home > Software design >  Bokeh-like Blur with Mask as Intensity of Blur Radius
Bokeh-like Blur with Mask as Intensity of Blur Radius

Time:09-12

I know how to Gaussian blur with Pillow, but can't track how to mask it by intensity of the radius value with a mask.

I am using MiDaS package to produce depth maps form 2D images. What I want to do is be able to blur the original image by the depth mask as a pseudo depth of field.

Here is a visual demonstration of the result I'm after with CV2 or Pillow (I don't understand which can do what I'm after.)

Fake DOF Example

Note: I'm sorry if this is considered junk, I've sat on this question for a month. I tried scouring the net for something like this, and all I found was Poor Man's Portrait Mode which I could not get to work, and also would be reproducing depth maps when I already have them from my script and used for the 3D image creation.

Edit:

I did come up with this, using composite Not sure why I didn't take note of it before. Though I have to say, the results aren't too great. I think I really do need to emulate some sort of shape blur like bokeh.

sharpen = 3
boxBlur = 5

oimg = Image.open('2.png').convert('RGB')
width, height = oimg.size
mimg = Image.open('2_depth.png').resize((width, height)).convert('L')

bimg = oimg.filter(ImageFilter.BoxBlur(int(boxBlur)))
bimg = bimg.filter(ImageFilter.BLUR)
for i in range(sharpen):
    bimg = bimg.filter(ImageFilter.SHARPEN)

rimg = Image.composite(oimg, bimg, mimg)
  • Basically get your image, and mask, ensure the mask matches the image (I had a issue where images didn't match, but were the same size, just saved different from 2 saved the same way)

  • Blur your image to a new variable, however you like, Gaussian, etc. Gaussian was too soft for me. Add whatever extra filtering you want

  • Composite the results together, using depth map as a mask for composite.

Note: If someone knows how to achieve a different sort of blur that mimics bokeh, I'd like to know, and have adjusted the question title. I read about a discBlur but couldn't find anything for PIL/CV2.

CodePudding user response:

I’ve got only a brute-force solution with iteration over pixels: Variable blur intensity. My code is working but not as efficiently as I want.

You can try. Open your image as input and put your depth map in the variable blur_map.

CodePudding user response:

check out my repo it might help you, It contains an implementation many image processing techniques including filters like gaussian

  • Related