38
BLUR FILTER HANPO

Blur Filter - Hanpo

Embed Size (px)

DESCRIPTION

Talk about Blur Filter on iOS (Swift)

Citation preview

Page 1: Blur Filter - Hanpo

BLUR FILTERHANPO

Page 2: Blur Filter - Hanpo

HANPOSBACE - Director of Visual & Interface DesignFreelancer - iOS UI/UX Designer ,iOS Developer ,Illustrator Portfoliohttp://hanpo.tw/ LinkedIntw.linkedin.com/in/hanpo/ Instagram http://instagram.com/ihanpo

Page 3: Blur Filter - Hanpo

Why?

Page 4: Blur Filter - Hanpo
Page 5: Blur Filter - Hanpo

i.Frame Exhibition

Page 6: Blur Filter - Hanpo

CamInstagram Path

Page 7: Blur Filter - Hanpo

Instagram

Page 8: Blur Filter - Hanpo

Path

Page 9: Blur Filter - Hanpo

Cam

Page 10: Blur Filter - Hanpo

What?

Page 11: Blur Filter - Hanpo
Page 12: Blur Filter - Hanpo

How?

Page 13: Blur Filter - Hanpo

Image Data

Page 14: Blur Filter - Hanpo

Pixel

     RGB 115,41,41 RGB 98,49,49 RGB 212,168,32

RGB 115,41,41 RGB 98,49,49 RGB 255,243,182

RGB 115,41,41 RGB 98,49,49 RGB 255,251,236

Image (2D array of pixels)

Page 15: Blur Filter - Hanpo

Naïve Box BlurBox blur - 3*3 Matrix of 1/9 * determinant matrix

11 1

11 1

11 1

Page 16: Blur Filter - Hanpo

Naïve Box Blur

RGB 115,41,41 RGB 98,49,49 RGB 212,168,32

RGB 115,41,41 RGB 98,49,49 RGB 255,243,182

RGB 115,41,41 RGB 98,49,49 RGB 255,251,236

RGB 151,104,80

Page 17: Blur Filter - Hanpo

Naïve Box BlurImage convolution - the convolution of a 2D image is O(m2*n2)

For it to be fast you must implement it as an accumulator, not a convolution loop.

for each row for each col set count to 0 for every neighboring pixel within the radius m in the x direction for every neighboring pixel within the radius m in the y direction add the color to the total count++ final_color = total/count setpixel(current x, current y, final_color)

Page 18: Blur Filter - Hanpo

Calculate all PixelsSample image 4 x 3 pixels, radius is 1

Page 19: Blur Filter - Hanpo

Box BlurR:0 R:1 R:3

R:6 R:10 R:20

Page 20: Blur Filter - Hanpo

Edge Handling

Page 21: Blur Filter - Hanpo

Better Box BlurFor it to be fast you must implement it as an accumulator, not a convolution loop.

Image convolution - the convolution of a 2D image is O(n^2 * 2m)

for each row for every neighboring pixel within the radius m in the x direction add the color to the total count++ final_color = total/count setpixel(current x, current y, final_color) !

repeat above for the columns

Page 22: Blur Filter - Hanpo

Calculate all PixelsSample image 6 x 6 pixels, radius is 2

in the x direction

in the y direction

if x <= r for var _i=i-x; _i<=i+r; _i++

if x > r && x < (w-r) for var _i=i-r; _i<=i+r; _i++

if x >= (w-r) for var _i=i-r; _i<=i+(w-x); _i++

if _y <= r for var _i=0; _i<=_y+r; _i++ let currentIndex = _i*w+_x

if r < _y && _y < (h-r) for var _i=_y-r; _i<=_y+r; _i++ let currentIndex = _i*w+_x

if _y >= (h-r) for var _i=_y-r; _i<=(h-1); _i++ let currentIndex = _i*w+_x

Page 23: Blur Filter - Hanpo

R:0 R:1 R:3

R:6 R:10 R:20

Better Box Blur

Page 24: Blur Filter - Hanpo

CompareBox Blur & Better Box Blur

Size: 512 x 512 Device: iPad mini 2

Box Blur Better Box Blur

Radius:1 112.4061s 7.1371s

Radius:2 300.8143s 7.1586s

Radius:3 586.2660s 7.2406s

Radius:6 1958.8380s 7.3746s

Radius:10 5017.5250s 7.6009s

Radius:20 8.1089s

Page 25: Blur Filter - Hanpo

Convolutionfor each image row in output image: for each pixel in image row: !

set accumulator to zero !

for each kernel row in kernel: for each element in kernel row: !

if element position corresponding* to pixel position then multiply element value corresponding* to pixel value add result to accumulator endif !

set output image pixel to accumulator

Page 26: Blur Filter - Hanpo

Accelerate FrameworkConsists of two frameworks: vImage and vecLib

Available in OS X v10.4 Use vector co-processor (CPU-based) Designed to run various mathematical operations with data Well optimized and uses SIMD

Page 27: Blur Filter - Hanpo

vImageAccelerate Framework

func vImageBoxConvolve_ARGB8888(_ src: UnsafePointer<vImage_Buffer>, _ dest: UnsafePointer<vImage_Buffer>, _ tempBuffer: UnsafeMutablePointer<Void>, _ srcOffsetToROI_X: vImagePixelCount, _ srcOffsetToROI_Y: vImagePixelCount, _ kernel_height: UInt32, _ kernel_width: UInt32, _ backgroundColor: UnsafeMutablePointer<UInt8>, _ flags: vImage_Flags) -> vImage_Error

Page 28: Blur Filter - Hanpo

vImage BlurR:0 R:1 R:3

R:6 R:10 R:20

Page 29: Blur Filter - Hanpo

Compare

Box Blur Better Box Blur vImage

Radius:1 112.4061s 7.1371s first: 0.0292s second: 0.0065s

Radius:2 300.8143s 7.1586s first:0.0260 second:0.0063

Radius:3 586.2660s 7.2406s first: 0.0410s second: 0.0066s

Radius:6 1958.8380s 7.3746s first: 0.0272s second: 0.0071s

Radius:10 5017.5250s 7.6009s first: 0.0294s second: 0.0063s

Radius:20 8.1089s first: 0.0281s second: 0.0073s

Box Blur & Better Box Blur & vImage Size: 512 x 512 Device: iPad mini 2

Page 30: Blur Filter - Hanpo

Gaussian Blur

0.00000067 0.00002292 0.00019117 0.00038771 0.00019117 0.00002292 0.00000067

0.00002292 0.00078633 0.00655965 0.01330373 0.00655965 0.00078633 0.00002292

0.00019117 0.00655965 0.05472157 0.11098164 0.05472157 0.00655965 0.00019117

0.00038771 0.01330373 0.11098164 0.22508352 0.11098164 0.01330373 0.00038771

0.00019117 0.00655965 0.05472157 0.11098164 0.05472157 0.00655965 0.00019117

0.00002292 0.00078633 0.00655965 0.01330373 0.00655965 0.00078633 0.00002292

0.00000067 0.00002292 0.00019117 0.00038771 0.00019117 0.00002292 0.00000067

r=3 ,σ = 0.84089642

Page 31: Blur Filter - Hanpo

Gaussian Distribution

in one dimension in two dimensions

Page 32: Blur Filter - Hanpo

Gaussian function

in one dimension

in two dimensions

Page 33: Blur Filter - Hanpo
Page 34: Blur Filter - Hanpo

Core Image - Gaussian Blurvar context: CIContext! let inputImage = CIImage(image: img) let gaussianBlurFilter = CIFilter(name:"CIGaussianBlur", withInputParameters: [kCIInputImageKey:inputImage,kCIInputRadiusKey:blurLevel]) let outputCIImage = gaussianBlurFilter.outputImage.imageByCroppingToRect(inputImage.extent()) context = CIContext(options:nil) let cgimg = context.createCGImage(outputCIImage, fromRect: outputCIImage.extent()) let outputImage = UIImage(CGImage: cgimg)

Page 35: Blur Filter - Hanpo

Gaussian Blur Speed

Level:1 0.1143s

Level:10 0.1192s

Level:100 0.1288s

Level:500 0.2334s

Level:680 0.1078s

Level:1000 0.1082s

Size: 512 x 512 Device: iPad mini 2Core Image - Gaussian Blur

Page 36: Blur Filter - Hanpo

Compare

Box Blur Gaussian Blur

Box Blur & Gaussian Blur

Page 37: Blur Filter - Hanpo

Compare

Box Blur Gaussian Blur

Box Blur & Gaussian Blur

Page 38: Blur Filter - Hanpo

Referencehttp://en.wikipedia.org/wiki/Box_blurhttp://en.wikipedia.org/wiki/Gaussian_blurACM SIGGRAPH@UIUC Fast Image Convolutions by: Wojciech Jaroszhttp://blog.amritamaz.me/post/42203030076/understanding-box-blurhttp://tech-algorithm.com/articles/boxfiltering/