I've following two images the background may be totally different image, e.g. not just plain color.
So basically I want to get the diff image of these two images i.e.
The diff image of two images is the image with the same size but the pixels are set to be transparent that haven't been changed. The difference image is constructed from the diff pixels with the color from the second image
I'm looking for the solution based on Core Graphics techniques, please don't suggest to run all over the pixels in loop. I do care about the performance.
As I'm new to Quartz I would like to know is it possible to achieve this with masks ? Or please suggest another approach !
Update on using difference blending mode Actually if I use the difference blending mode it doesn't solve my problem as it doesn't keep right colors of the pixels. If I apply difference blend mode to above 2 images I'll get following
Which seems to have inverted colors for the pixels and then if I invert them I get following
Which actually is not what I wanted, as the pixel colors are totally different
Now let's use ImageChops to find the difference for us! Here we have a simple function that we can use to find differences in images. All you need to do is pass it three paths! The first two paths are for the images that we want to compare. The last path is where to save the diff image, if we find a diff.
Both of the images have has to be the same size and channels. Each of the pixels has to be the same value. We can do it in the following seven steps: Load the original image and the second one Check the size of the images Find what’s different between two images Convert them into grayscale Increasing the size of the differences
#create a copy of original image so that we can store the#difference of 2 images in the same ondiff = original.copy() cv2.absdiff(original, new, diff) Step 3: Convert the Image into Grayscale
Image differencing is an image processing technique used to determine changes between images. The difference between two images is calculated by finding the difference between each pixel in each image, and generating an image based on the result.
You can blend any drawing in Core Graphics using
CGContextSetBlendMode(kCGBlendModeDifference);
By drawing the first image and then setting the blend mode to difference and drawing the second image you will get the difference (as I suggested in my comment). This gives you an inverted image (as seen in your updates question). To invert the image you can then fill the same rect with white color using (since the blend mode is still set to "difference").
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(context, frame);
Sample code to do all this (inside drawRect:) is seen below.
CGContextRef context = UIGraphicsGetCurrentContext();
// Your two images
CGImageRef img1 = [[UIImage imageNamed:@"Ygsvt.png"] CGImage];
CGImageRef img2 = [[UIImage imageNamed:@"ay5DB.png"] CGImage];
// Some arbitrary frame
CGRect frame = CGRectMake(30, 30, 100, 100);
// Invert the coordinates to not draw upside down
CGContextTranslateCTM(context, 0, frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Draw original image
CGContextDrawImage(context, frame, img1);
// Draw the second image using difference blend more over the first
CGContextSetBlendMode(context, kCGBlendModeDifference);
CGContextDrawImage(context, frame, img2);
// Fill the same rect with white color to invert
// (still difference blend mode)
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(context, frame);
Thanks to @David Rönnqvist for the tip I've got my problem solved +2 to him :)
I've posted the solution in my blog here http://levonp.blogspot.com/2012/05/quartz-getting-diff-images-of-two.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With