欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

swift + core image

程序员文章站 2022-03-29 22:46:17
最近想用swift 做图像处理,比较来下core iamge 比较靠谱Core Image is Efficient and Easy to Use for Processing and Analyzing ImagesCore Image provides hundreds of built-in filters. You set up filters by supplying key-value pairs for a filter’s input parameters. The output...

最近想用swift 做图像处理,一开始觉得用core image 比较靠谱,但是自定义滤镜挺复杂,最后直接用cgimage操作像素解决了。

Core Image is Efficient and Easy to Use for Processing and Analyzing Images

Core Image provides hundreds of built-in filters. You set up filters by supplying key-value pairs for a filter’s input parameters. The output of one filter can be the input of another, making it possible to chain numerous filters together to create amazing effects. If you create a compound effect that you want to use again, you can subclass CIFilter to capture the effect “recipe.”

There are more than a dozen categories of filters. Some are designed to achieve artistic results, such as the stylize and halftone filter categories. Others are optimal for fixing image problems, such as color adjustment and sharpen filters.

Core Image can analyze the quality of an image and provide a set of filters with optimal settings for adjusting such things as hue, contrast, and tone color, and for correcting for flash artifacts such as red eye. It does all this with one method call on your part.

Core Image can detect human face features in still images and track them over time in video images. Knowing where faces are can help you determine where to place a vignette or apply other special filters.

Core image 能分析图像质量、调整hue,contrast,纠正红眼,人脸检测和跟踪,还有各种滤镜特效,具体有哪些滤镜,基于你的系统。

图像处理基本流程

import CoreImage

let context = CIContext()                                           // 1

let filter = CIFilter(name: "CISepiaTone")!                         // 2

filter.setValue(0.8, forKey: kCIInputIntensityKey)

let image = CIImage(contentsOfURL: myURL)                           // 3

filter.setValue(image, forKey: kCIInputImageKey)

let result = filter.outputImage!                                    // 4

let cgImage = context.createCGImage(result, from: result.extent)    // 5

图像是滤镜的输入和输出,上下文很占资源,生成了要抓紧用

想知道系统有哪些filter?

let filters = CIFilter.filterNames(inCategory: kCICategoryBuiltIn)

print(filters)

print(filters.count)

有219个内置滤镜

 可以对一个图像作用多种滤镜

加了滤镜后,图像被转了90度, 需要记录图像的方向

let orientation = self.img.imageOrientation
 let cimg = CIImage(image: self.img)
 let context = CIContext()
 ...
 let img2 = UIImage(cgImage: cgImage!, scale: 1.0, orientation: orientation)

因为要做二值化,需要自定义一个滤镜

core image 的图像处理函数真不如opencv来的丰富

Core image 做基于像素的滤镜比较适合,做直方图等统计性的功能有点困难

Write the Kernel Code

The code that performs per-pixel processing resides in a file with the .cikernel extension. You can include more than one kernel routine in this file. You can also include other routines if you want to make your code modular. You specify a kernel using a subset of OpenGL Shading Language (glslang) and the Core Image extensions to it. See Core Image Kernel Language Reference for information on allowable elements of the language.

一个滤镜就是一个算子,定义一个滤镜要用glslang和objc一起完成,尽量把代码放在objc中,看了觉得自定义滤镜挺复杂的

还是自己直接操作像素点好一点。

ios12以后,自定义滤镜推荐用metal,xcode 中貌似也只能找到创建metal文件选项,没有创建.cikernel的选项

metal 是用c++ 语言的,挺底层的。 gpuimage3是用swift封装的metal。不过很多功能貌似还没有实现?

meta支持苹果平台

还是用cgimage,cgcontext操作像素简单点。

extension UIImage {
    func pixelData() -> [UInt8]? {
        let size = self.size
        let dataSize = size.width * size.height * 4
        var pixelData = [UInt8](repeating: 0, count: Int(dataSize))
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let context = CGContext(data: &pixelData,
                                width: Int(size.width),
                                height: Int(size.height),
                                bitsPerComponent: 8,
                                bytesPerRow: 4 * Int(size.width),
                                space: colorSpace,
                                bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)
        guard let cgImage = self.cgImage else { return nil }
        context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))

        return pixelData
    }
 }

 

主要参考:https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_intro/ci_intro.html#//apple_ref/doc/uid/TP30001185

https://colin1994.github.io/2016/10/21/Core-Image-Custom-Filter/

https://www.raywenderlich.com/7475-metal-tutorial-getting-started

iOS滤镜系列-滤镜开发概览,先看下面这一篇,掌握总体情况。

https://www.cnblogs.com/kenshincui/p/12181735.html

本文地址:https://blog.csdn.net/txdb/article/details/107410813