Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Gaussian Blur and Spatial convolution of Image by Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to achieve Gaussian blur and image spatial convolution in Java. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Gaussian blur

Gaussian blur (Gaussian Blur), also known as Gaussian smoothing, is widely used in image processing software such as Adobe Photoshop, GIMP and Paint.NET. It is usually used to reduce image noise and level of detail. The visual effect of the image generated by this blur technology is like observing the image through a translucent screen, which is obviously different from the out-of-focus imaging effect of the lens and the effect in the shadow of ordinary lighting. Gaussian smoothing is also used in the pre-processing stage of computer vision algorithms to enhance the image effect at different proportions. From a mathematical point of view, the Gaussian blur process of an image is the convolution of the image with the normal distribution. Because the normal distribution is also called Gaussian distribution, this technique is called Gaussian blur. Convolution of the image with a circular box blur will result in a more accurate out-of-focus imaging effect. Because the Fourier transform of Gaussian function is another Gaussian function, Gaussian blur is a low-pass filter for the image.

Gaussian blur uses the density function of Gaussian normal distribution to calculate the transformation of each pixel in the image.

According to the one-dimensional Gaussian function, the two-dimensional Gaussian function can be derived.

Where r is the fuzzy radius, r ^ 2 = x ^ 2 + y ^ 2, σ is the standard deviation of the normal distribution. In two-dimensional space, the contours of the surface generated by this formula are concentric circles with normal distribution from the center. The convolution matrix composed of pixels with non-zero distribution is transformed with the original image. The value of each pixel is a weighted average of the values of the surrounding neighboring pixels. The value of the original pixel has a Gaussian distribution, so it has a weight of *. As the adjacent pixels get farther and farther away from the original pixel, the weight becomes smaller and smaller. This fuzzy processing preserves the edge effect higher than other equalized fuzzy filters.

In fact, it is very easy to implement Gaussian blur on iOS. CoreImage's API has been available since iOS 5. 0, and a large number of filter implementations are provided in the CoreImage.framework library.

+ (UIImage *) coreBlurImage: (UIImage *) image withBlurNumber: (CGFloat) blur {CIContext * context = [CIContext contextWithOptions:nil]; CIImage * inputImage= [CIImage imageWithCGImage:image.CGImage]; / / set filterCIFilter * filter = [CIFilter filterWithName:@ "CIGaussianBlur"]; [filter setValue:inputImage forKey:kCIInputImageKey]; [filter setValue:@ (blur) forKey: @ "inputRadius"]; / / blur image CIImage * result= [filter valueForKey:kCIOutputImageKey]; CGImageRef outImage= [context createCGImage:result fromRect: [result extent]] UIImage * blurImage= [UIImage imageWithCGImage:outImage]; CGImageRelease (outImage); return blurImage;}

You can also use native API--RenderScript to implement Gaussian blur on Android, but the API that requires Android is above 17, which is version 4.2 of Android.

/ * * the algorithm for implementing Gaussian blur using RenderScript * @ param bitmap * @ return * / public Bitmap blur (Bitmap bitmap) {/ / Let's create an empty bitmap with the same size of the bitmap we want to blurBitmap outBitmap = Bitmap.createBitmap (bitmap.getWidth (), bitmap.getHeight (), Bitmap.Config.ARGB_8888); / / Instantiate a new RenderscriptRenderScript rs = RenderScript.create (getApplicationContext ()) / / Create an IntrinsicBlur Script using the RenderscriptScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create (rs, Element.U8_4 (rs)); / / Create the Allocations (in/out) with the Renderscript and the in/out bitmapsAllocation allIn = Allocation.createFromBitmap (rs, bitmap); Allocation allOut = Allocation.createFromBitmap (rs, outBitmap); / / Set the radius of the blur: 0 < radius

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report