如何在iPhone上比较一个图像与另一个图像以确定它们是否相似一定比例?

Sol*_*444 13 iphone xcode compare image uiimage

我基本上想要拍摄iPhone或iPad 2上从相机拍摄的两张图像,并将它们相互比较,看看它们是否完全相同.显然由于光线等,图像永远不会完全相同,所以我想检查大约90%的兼容性.

我在这里看到的所有其他类似问题要么不适用于iOS,要么是用于在图像中定位对象.我只是想看看两个图像是否相似.

谢谢.

aro*_*oth 8

作为一种快速,简单的算法,我建议迭代每个图像中大约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
}
Run Code Online (Sandbox Code Playgroud)