在iPhone上,如何比较一个图像与另一个图像,看看它们是否相似一定百分比? [英] How does one compare one image to another to see if they are similar by a certain percentage, on the iPhone?

查看:166
本文介绍了在iPhone上,如何比较一个图像与另一个图像,看看它们是否相似一定百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想拍摄从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){
//一个或多个像素分量相差10%或更多
numDifferences ++;
}
}
}

if(numDifferences / totalCompares <= 0.1f){
//图像至少90%相同90%的时间
}
else {
//图像小于90%相同90%的时间
}


I basically want to take two images taken from the camera on the iPhone or iPad 2 and compare them to each other to see if they are pretty much the same. Obviously due to light etc the image will never be EXACTLY the same so I would like to check for around 90% compatibility.

All the other questions like this that I saw on here were either not for iOS or were for locating objects in images. I just want to see if two images are similar.

Thank you.

解决方案

As a quick, simple algorithm, I'd suggest iterating through about 1% of the pixels in each image and either comparing them directly against each other or keeping a running average and then comparing the two average color values at the end.

You can look at this answer for an idea of how to determine the color of a pixel at a given position in an image. You may want to optimize it somewhat to better suit your use-case (repeatedly querying the same image), but it should provide a good starting point.

Then you can use an algorithm roughly like:

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
}

这篇关于在iPhone上,如何比较一个图像与另一个图像,看看它们是否相似一定百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆