Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz : Getting the diff images of two images

I've following two images the background may be totally different image, e.g. not just plain color.

Image number oneenter image description here

So basically I want to get the diff image of these two images i.e.

enter image description here

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

enter image description here

Which seems to have inverted colors for the pixels and then if I invert them I get following

enter image description here

Which actually is not what I wanted, as the pixel colors are totally different

like image 636
deimus Avatar asked May 20 '12 09:05

deimus


People also ask

How to find the difference between two images using imagechops?

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.

How to make a comparison between two images?

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

How to store the difference of two images in the same?

#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

What is Image differencing?

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.


2 Answers

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);
like image 145
David Rönnqvist Avatar answered Oct 13 '22 00:10

David Rönnqvist


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

like image 38
deimus Avatar answered Oct 12 '22 22:10

deimus