我关注了两张图片,背景可能是完全不同的图片,例如不只是纯色。
所以基本上我想得到这两张图片的差异图像,即
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 张图像应用差异混合模式,我将得到以下结果
这似乎对像素有反转的颜色,然后如果我反转它们,我就会跟随
这实际上不是我想要的,因为像素颜色完全不同
最佳答案
您可以使用 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/