user717452
user717452

Reputation: 111

UIImage Replace Color with Background Image Washing Out and Overlaying everything Instead of Replacing

My app is trying to choose an image, and replace a given color with a separate image (green-screen). It uses Obj-C. So far, I'm attempting to replace a specific color (sky-blue) on this image with a space background. As you can see from the images below, I'm getting a washed out picture in which almost everything gets replaced instead of the intended sky color only. What am I handling wrong? I know there are some libraries with Swift that will do this, but I need to stay in Obj-C.

My NSObject

+ (UIImage *)replaceWhiteColorWithImage:(UIImage *)image backgroundImage:(UIImage *)backgroundImage {
    CGImageRef rawImageRef = image.CGImage;
    const CGFloat colorMasking[6] = {130/255.0, 1.0, 151/255.0, 1.0, 186/255.0, 1.0};
    UIGraphicsBeginImageContext(image.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Flip the context to match the UIKit coordinate system
    CGContextTranslateCTM(context, 0.0, image.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // Draw the background image
    [backgroundImage drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];

    // Clip to the raw image using the white color as a mask
    CGContextClipToMask(context, CGRectMake(0, 0, image.size.width, image.size.height), rawImageRef);

    // Clear the white color with a transparent background
    CGContextClearRect(context, CGRectMake(0, 0, image.size.width, image.size.height));

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

My code to call this from my ViewController

    -(IBAction)chooseImage {
    // Assuming ImageProcessor is the class with the replaceWhiteColorWithImage method

    // Picking an image using UIImagePickerController (as mentioned in the previous response)
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    [self presentViewController:imagePicker animated:YES completion:nil];

}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    
    UIImage *pickedImage = info[UIImagePickerControllerOriginalImage];

    // Provide the background image you want to use
    UIImage *backgroundImage = [UIImage imageNamed:@"space.jpg"];

    // Call the method to replace white color with the background image
    UIImage *processedImage = [ImageProcessor replaceWhiteColorWithImage:pickedImage backgroundImage:backgroundImage];

    // Update the UIImageView with the processed image
    self.yourImageView.image = processedImage;

    [picker dismissViewControllerAnimated:YES completion:nil];
}

The image I'm attempting to edit: enter image description here

The image I want to be the new background: enter image description here

What I get: enter image description here

Upvotes: 0

Views: 71

Answers (0)

Related Questions