iphone - quartz : Getting the diff images of two images

标签 iphone objective-c ipad image-processing core-graphics

我关注了两张图片,背景可能是完全不同的图片,例如不只是纯色。

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 混合任何绘图

CGContextSetBlendMode(kCGBlendModeDifference);

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

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

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

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);

关于iphone - quartz : Getting the diff images of two images,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10672348/

相关文章:

iphone - 计算并显示在标签中

objective-c - 同时运行两个 SKAction

html - Cocoa、Objective-C 到 HTML?

ios - UITableViewCell 分隔符未出现

iphone - 如何缓慢隐藏 UI 元素

ios - 以编程方式快速添加不同数量的标签和 slider

iphone - Facebook 页面链接在 Facebook 应用程序而不是 Safari 浏览器中打开

ios - NSOperation 子类性能或泄漏

iphone - 在 ios6 中使用 facebook 登录无法识别的选择器已发送至实例”

ios - 仅由于内存错误,iOS8 上的应用程序崩溃