问题描述
我基本上想从 iPhone 或 iPad 2 上的相机拍摄两张图像,然后将它们相互比较,看看它们是否几乎相同.显然由于光线等原因,图像永远不会完全相同,所以我想检查一下 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.
我在这里看到的所有其他类似问题要么不是针对 iOS 的,要么是针对在图像中定位对象的.我只是想看看两张图片是否相似.
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.
谢谢.
推荐答案
作为一种快速、简单的算法,我建议迭代每张图像中大约 1% 的像素,然后将它们直接相互比较或保留运行平均值,然后在最后比较两个平均颜色值.
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.
你可以看看 this answer 了解如何确定图像中给定位置的像素颜色.您可能希望对其进行一些优化以更好地适应您的用例(重复查询相同的图像),但它应该提供一个很好的起点.
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 上将一张图像与另一张图像进行比较,以查看它们是否有一定百分比的相似性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!