我基本上想用 iPhone 或 iPad 2 上的相机拍摄两张图像,然后将它们相互比较,看看它们是否几乎相同。显然,由于光线等原因,图像永远不会完全相同,因此我想检查大约 90% 的兼容性。
我在这里看到的所有其他类似的问题要么不是针对 iOS 的,要么是针对在图像中定位对象的。我只是想看看两张图片是否相似。
谢谢。
作为一种快速、简单的算法,我建议迭代每个图像中大约 1% 的像素,然后将它们直接相互比较,或者保持运行平均值,然后在最后比较两个平均颜色值。
你可以看看这个答案了解如何确定图像中给定位置的像素颜色。您可能希望对其进行一些优化以更好地适合您的用例(重复查询同一图像),但它应该提供一个良好的起点。
然后你可以使用大致如下的算法:
float numDifferences = 0.0f;
float totalCompares = width * height / 100.0f;
for (int yCoord = 0; yCoord < height; yCoord += 10) {
for (int xCoord = 0; xCoord < width; xCoord += 10) {
int img1RGB[] = [image1 getRGBForX:xCoord andY: yCoord];
int img2RGB[] = [image2 getRGBForX:xCoord andY: yCoord];
if (abs(img1RGB[0] - img2RGB[0]) > 25 || abs(img1RGB[1] - img2RGB[1]) > 25 || abs(img1RGB[2] - img2RGB[2]) > 25) {
//one or more pixel components differs by 10% or more
numDifferences++;
}
}
}
if (numDifferences / totalCompares <= 0.1f) {
//images are at least 90% identical 90% of the time
}
else {
//images are less than 90% identical 90% of the time
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)