像本主题一样,我正在使用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)
但是,它更像是将两个图像组合在一起但相减。
答案 0 :(得分:0)
我环顾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并停止应用程序,但是当我显示图像时,图像是好的。