关于iphone:Quartz:获取两个图像的差异图像

Quartz : Getting the diff images of two images

我跟踪了两个图像,背景可能是完全不同的图像,例如不只是纯色。

Image number one enter image description here

所以基本上我想得到这两个图像的差异图像,即

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

我正在寻找基于Core Graphics技术的解决方案,请不要建议在所有像素中循环运行。我很在意性能。

由于我是Quartz的新手,我想知道可以使用口罩来实现吗?
或请提出另一种方法!

使用差异混合模式更新
实际上,如果我使用差异混合模式,则不会解决我的问题,因为它无法保持正确的像素颜色。如果我将差异混合模式应用于以上2张图像,我将得到以下

enter image description here

哪个像素的颜色似乎相反,然后将其反转,就会得到

enter image description here

实际上不是我想要的,因为像素颜色完全不同


您可以使用

在Core Graphics中混合任何图形

1
CGContextSetBlendMode(kCGBlendModeDifference);

通过绘制第一个图像,然后将混合模式设置为差异,然后绘制第二个图像,您将获得差异(正如我在评论中所建议的)。这使您获得倒置的图像(如您的更新问题所示)。要反转图像,您可以使用来用白色填充相同的矩形(因为混合模式仍设置为"差异")。

1
2
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(context, frame);

执行所有这些操作的示例代码(在drawRect:内部)如下所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);


感谢@David R?nnqvist的提示,我已将我的问题解决了2个:)

我已将解决方案发布到我的博客中,网址为http://levonp.blogspot.com/2012/05/quartz-getting-diff-images-of-two.html