I’ve the next picture:
I’m utilizing the beneath code to create a crimson masks from CIPersonSegmentation
after which change the background coloration to cyan
:
WARNING: CIPersonSegmentation
requires bodily machine. It makes use of Imaginative and prescient
API which I imagine shouldn’t be supported on simulator.
if let picture = UIImage(named: "demo9"), let editted = applyPersonFilter(to: picture) {
imageView.picture = editted
}
func applyPersonFilter(to picture: UIImage) -> UIImage? {
guard let ciImage = CIImage(picture: picture) else { return nil }
print("ciImage: (ciImage.extent)") // prints (0.0, 0.0, 2882.0, 1300.0)
let context = CIContext(choices: nil)
let personMaskFilter = CIFilter(title: "CIPersonSegmentation", parameters: [
kCIInputImageKey: ciImage,
"inputQualityLevel": 0
])
let maskImage = personMaskFilter?.outputImage?.cropped(to: ciImage.extent) as? CIImage
print("maskImage: (maskImage?.extent)") // prints (0.0, 0.0, 2016.0, 1300.0)
let solidColorFilter = CIFilter(title: "CIConstantColorGenerator")
solidColorFilter?.setValue(CIColor.cyan, forKey: kCIInputColorKey)
let blendFilter = CIFilter(title: "CIBlendWithRedMask", parameters: [kCIInputImageKey: ciImage, kCIInputBackgroundImageKey: solidColorFilter?.outputImage?.cropped(to: ciImage.extent) as Any, kCIInputMaskImageKey: maskImage as Any])
guard let outputImage = blendFilter?.outputImage else {
return nil
}
print("blendFilter: (outputImage.extent)") // prints (0.0, 0.0, 2882.0, 1300.0)
guard let cgImage = context.createCGImage(outputImage, from: outputImage.extent) else { return nil }
return UIImage(cgImage: cgImage)
}
The output it creates is that this:
As you may see, the masks place shouldn’t be right. It’s because the crimson masks created by CIPersonSegmentation
has a distinct extent
than the unique picture.
How one can appropriately apply this considering the completely different lengthen of the masks?