【iPhone】GPUImageで円ぼかしを実装する方法

概要

GPUImageを使用して、画像に指定したサイズの「円ぼかし」をかける方法。
iPhoneで画像処理をする際の定番になりつつあるGPUImageです。
導入方法については他のブログを参照してください。
■GPUImage
https://github.com/BradLarson/GPUImage

*実際にかかる円ぼかしは下記のようになります。境界線も自然に処理がかかっています。
f:id:unibo:20131112220522p:plain:w200:h200

方法

実装はとても簡単で、下記のように書くだけで実装できます。

UIImage *inputImage = [UIImage imageNamed:@"Lenna.bmp"];
GPUImagePicture *imagePicture = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageGaussianSelectiveBlurFilter *blurFilter = [[GPUImageGaussianSelectiveBlurFilter alloc] init];
[(GPUImageGaussianSelectiveBlurFilter *)blurFilter setExcludeCircleRadius:0.4];
[(GPUImageGaussianSelectiveBlurFilter *)blurFilter setExcludeCirclePoint:CGPointMake(0.5, 0.5)];
[imagePicture addTarget:blurFilter];
[imagePicture processImage];
UIImage *outputImage = [blurFilter imageFromCurrentlyProcessedOutputWithOrientation:inputImage.imageOrientation];
UIImageView *imageView = [[UIImageView alloc] initWithImage:outputImage];

下記で円の半径、円の中心を指定していますが、
どちらも「0.0〜1.0」の間で記述します。

[(GPUImageGaussianSelectiveBlurFilter *)blurFilter setExcludeCircleRadius:0.4];
[(GPUImageGaussianSelectiveBlurFilter *)blurFilter setExcludeCirclePoint:CGPointMake(0.5, 0.5)];

その他のフィルターについて

GPUImageでは2種類の画像をBlendすることが可能で、まだ試しておりませんがPhotoShopにあるような、
オーバーレイ、ソフトライト、焼き込み、乗算なども可能なようです。

■Blending modes(一部)
GPUImageMultiplyBlendFilter: Applies a multiply blend of two images
GPUImageOverlayBlendFilter: Applies an overlay blend of two images
GPUImageColorBurnBlendFilter: Applies a color burn blend of two images
GPUImageScreenBlendFilter: Applies a screen blend of two images
GPUImageExclusionBlendFilter: Applies an exclusion blend of two images
GPUImageHardLightBlendFilter: Applies a hard light blend of two images
GPUImageSoftLightBlendFilter: Applies a soft light blend of two images
GPUImageAlphaBlendFilter: Blends the second image over the first, based on the second's alpha channel
mix: The degree with which the second image overrides the first (0.0 - 1.0, with 1.0 as the default)
GPUImageNormalBlendFilter: Applies a normal blend of two images

おすすめの書籍

Effective Objective-C 2.0

Effective Objective-C 2.0

定番のEffectiveシリーズ!