gpt4 book ai didi

swift:如果我想标准化图像,我应该选择哪个过滤器?

转载 作者:行者123 更新时间:2023-11-28 07:37:16 24 4
gpt4 key购买 nike

和主题一样,我正在使用 swift 来预测模型,有人建议我使用 CoreImage 来减去输入图像的平均值,但似乎我不能只用一个过滤器来做到这一点。那么,如果我想对图像进行归一化,我应该选择哪个过滤器?

到目前为止,我已经尝试在没有 CoreImage 的情况下执行此操作

let oriimage = self.convert(cmage: ciimage)
let uimean:UIImage = UIImage(color: oriimage.averageColor(), size: CGSize(width: oriimage.size.width, height: oriimage.size.width))!
let cimean:CIImage = CIImage(image:uimean)!

ciimage.composited(over: cimean)
let image = self.convert(cmage: ciimage)

然而,它更像是将两个图像组合在一起但相减。

最佳答案

我查看了 CoreImage 框架过滤器,发现 CIColorMatrix 可以工作。

extension UIImage {
func colorized(with color: UIColor) -> UIImage? {
guard
let ciimage = CIImage(image: self),
let colorMatrix = CIFilter(name: "CIColorMatrix")
else { return nil }
var r: CGFloat = 1, g: CGFloat = 1, b: CGFloat = 1, a: CGFloat = 1
color.getRed(&r, green: &g, blue: &b, alpha: &a)
colorMatrix.setDefaults()
colorMatrix.setValue(ciimage, forKey: "inputImage")
colorMatrix.setValue(CIVector(x: 1, y: 0, z: 0, w: 0), forKey: "inputRVector")
colorMatrix.setValue(CIVector(x: 0, y: 1, z: 0, w: 0), forKey: "inputGVector")
colorMatrix.setValue(CIVector(x: 0, y: 0, z: 1, w: 0), forKey: "inputBVector")
colorMatrix.setValue(CIVector(x: 0, y: 0, z: 0, w: 1), forKey: "inputAVector")
colorMatrix.setValue(CIVector(x: -r, y: -g, z: -b, w: 0), forKey: "inputBiasVector")
if let ciimage = colorMatrix.outputImage {
return UIImage(ciImage: ciimage)
}
return nil
}
}

let image = oriimage.colorized(with: color) ?? oriimage
let screensize:CGSize = CGSize(width: 720, height: 1280)
var face = image.cropped(boundingBox: facerect)
var left = image.cropped(boundingBox: leftrect)
var right = image.cropped(boundingBox: rightrect)


let col = 64,row = 64,c=3
face = face.resizeImageWith(newSize: CGSize(width:col,height:row))

但是,另一个问题出现在最后一行。结果返回 nil 并停止应用程序,但是当我显示图像时,图像是好的。

关于swift:如果我想标准化图像,我应该选择哪个过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53057388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com