Search code examples
iosmaskciimage

How to change the size of a CIImage mask as an input to blendKernel.apply function in Swift


I try to create live hair color mask IOS app. I used UIGraphicsGetImageFromCurrentImageContext() function to merge the mask and the background from video. It works but the image quality is not good enough. I wanted to change my code to use blendKernel.apply(foreground: maskCI!, background: backgroundCI!) function. In that case the size and position of the mask and the background image do not match. Mask image is shown on the right bottom corner of the background image. My background image size is 1080x1080 and mask image is 224x224. Could you please advice me how to overlap the mask and the background? Please see my code below:

class IntegrateViewAndMask {
    var blendKernel: CIBlendKernel { return .softLight }

    func mergeMaskAndBackground(mask: UIImage, background: CVPixelBuffer, size: Int) -> UIImage? {
        // Merge two images
//        let sizeImage = CGSize(width: size, height: size)

//        UIGraphicsBeginImageContext(sizeImage)

//        let areaSize = CGRect(x: 0, y: 0, width: sizeImage.width, height: sizeImage.height)

        // background
        var background = UIImage(pixelBuffer: background)

//      crop image to square 1080 x 1080
        background = cropImageToSquare(image:background!)

//        background?.draw(in: areaSize)

        // mask
//        mask.draw(in: areaSize)

        let backgroundCI = CIImage(image: background!)

        let maskCI = CIImage(image: mask)

        let trialImage = blendKernel.apply(foreground: maskCI!, background: backgroundCI!)

        let trialImageUI = UIImage(ciImage:trialImage!)

//        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!

//        UIGraphicsEndImageContext()

        return trialImageUI




Solution

  • I have just found an answer and tried it. Please see below link. As suggested, I resized the UIimage to bounding square and then converted to CIImage. So it works perfectly now.

    CIImage extent in pixels or points?