Create a mask from difference between two images (iPhone)

前端 未结 5 1773
臣服心动
臣服心动 2020-12-28 23:06

How can I detect the difference between 2 images, creating a mask of the area that\'s different in order to process the area that\'s common to both images (gaussian blur for

相关标签:
5条回答
  • 2020-12-28 23:42

    Every pixel which does not have a suitably similar pixel in the other image within a certain radius can be deemed to be part of the mask. It's slow, (though there's not much that would be faster) but it works fairly simply.

    0 讨论(0)
  • 2020-12-28 23:50

    Go through the pixels, copy the ones that are different in the lower image to a new one (not opaque).

    Blur the upper one completely, then show the new one above.

    0 讨论(0)
  • 2020-12-28 23:52

    I think the simplest way to do this would be to use a difference blend mode. The following code is based on code I use in CKImageAdditions.

    + (UIImage *) differenceOfImage:(UIImage *)top withImage:(UIImage *)bottom {
        CGImageRef topRef = [top CGImage];
        CGImageRef bottomRef = [bottom CGImage];
    
        // Dimensions
        CGRect bottomFrame = CGRectMake(0, 0, CGImageGetWidth(bottomRef), CGImageGetHeight(bottomRef));
        CGRect topFrame = CGRectMake(0, 0, CGImageGetWidth(topRef), CGImageGetHeight(topRef));
        CGRect renderFrame = CGRectIntegral(CGRectUnion(bottomFrame, topFrame));
    
        // Create context
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        if(colorSpace == NULL) {
            printf("Error allocating color space.\n");
            return NULL;
        }
    
        CGContextRef context = CGBitmapContextCreate(NULL,
                                                     renderFrame.size.width,
                                                     renderFrame.size.height,
                                                     8,
                                                     renderFrame.size.width * 4,
                                                     colorSpace,
                                                     kCGImageAlphaPremultipliedLast);
        CGColorSpaceRelease(colorSpace);
    
        if(context == NULL) {
            printf("Context not created!\n");
            return NULL;
        }
    
        // Draw images
        CGContextSetBlendMode(context, kCGBlendModeNormal);
        CGContextDrawImage(context, CGRectOffset(bottomFrame, -renderFrame.origin.x, -renderFrame.origin.y), bottomRef);
        CGContextSetBlendMode(context, kCGBlendModeDifference);
        CGContextDrawImage(context, CGRectOffset(topFrame, -renderFrame.origin.x, -renderFrame.origin.y), topRef);
    
        // Create image from context
        CGImageRef imageRef = CGBitmapContextCreateImage(context);
        UIImage * image = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
    
        CGContextRelease(context);
    
        return image;
    }
    
    0 讨论(0)
  • 2020-12-28 23:57

    There are three reasons pixels will change from one iPhone photo to the next, the subject changed, the iPhone moved, and random noise. I assume for this question, you're most interested in the subject changes, and you want to process out the effects of the other two changes. I also assume the app intends the user to keep the iPhone reasonably still, so iPhone movement changes are less significant than subject changes.

    To reduce the effects of random noise, just blur the image a little. A simple averaging blur, where each pixel in the resulting image is an average of the original pixel with its nearest neighbors should be sufficient to smooth out any noise in a reasonably well lit iPhone image.

    To address iPhone movement, you can run a feature detection algorithm on each image (look up feature detection on Wikipedia for a start). Then calculate the transforms needed to align the least changed detected features.

    Apply that transform to the blurred images, and find the difference between the images. Any pixels with a sufficient difference will become your mask. You can then process the mask to eliminate any islands of changed pixels. For example, a subject may be wearing a solid colored shirt. The subject may move from one image to the next, but the area of the solid colored shirt may overlap resulting in a mask with a hole in the middle.

    In other words, this is a significant and difficult image processing problem. You won't find the answer in a stackoverflow.com post. You will find the answer in a digital image processing textbook.

    0 讨论(0)
  • 2020-12-29 00:05

    Can't you just subtract pixel values from the images, and process pixels where the difference i 0?

    0 讨论(0)
提交回复
热议问题