开发者

Using Objective-C, is there any way to compare two images and get a % difference value returned?

开发者 https://www.devze.com 2023-04-13 04:16 出处:网络
Using Objective-C, is there any way to compare two images and get a % difference value returned? For example, one image will have and X in it. Another image will have an X in it. And a third will ha

Using Objective-C, is there any way to compare two images and get a % difference value returned?

For example, one image will have and X in it. Another image will have an X in it. And a third will have a H in it. All the same font/color.

Obviously the X and the X开发者_Go百科 will have a % difference that is very small. The % between the X and the H would be very big.

How would I write a function that can return me the % difference?


You are going to have to think quite carefully about what kind of differences you care about.

Doing a pixel-by-pixel comparison of identically sized images won't be too hard. Assign an error value to each pixel, and then somehow combine the differences to produce a single figure.

Scaling one or other image if they start off different sizes but the same aspect ratio is also fairly simple.

However, the edge cases get tricky very quickly. For example, what happens if your two X characters are displaced, so one is on the left of the image, and the other on the right? Should these be detected as "similar" somehow?

Also, when comparing H to X, you want a high score - but how then would you rank an X against a colour picture of a butterfly? Even higher, presumably, but how much higher?

Some kind of learning neural network might help you, where you work out what values you would assign yourself to some sample comparisons, then train the network to return those values for your sample images. But that isn't trivial to code.

If your comparison requirements are simpler - for example, you are doing some kind of OCR equivalent, and you know that your inputs are likely to be monochromatic characters, then there are other tricks you can use - but the appropriate ones will depend on what you are trying to achieve.


You can use OpenCV (Open Source Computer Vision) library in iphone.This question has a good discussion on image comparison algorithms. Image processing on the iPhone this link may be helpful.


The API's have nothing like this available.

If you want to do this you'll have to roll your own or use an external library.

I would suggest finding an external library as this is not a trivial task.

For example, You have two images with an x the same font and color. Are the images the same size? If not, are they considered 100% different? If you resize the images to match, the difference is going to be much higher than it would be otherwise.

Or, let's say that you have two images with an x the same font and image size, but the colors are different. How would you weight that as a percent? Totally different or partially different? If you say that color is only partially different then how would you ever get a 100% difference?

Regardless, the basic way to do this (this I can see, I am sure someone much smarter will have a better idea) is to go pixel by pixel and compare the color data (HSV might be a good choice for this). Weight the components and calculate your difference.


Pixel by pixel comparison wouldn't help if source image size and destination image size is different. In this case the scaling ration should be correct. any other solutions for this?

0

精彩评论

暂无评论...
验证码 换一张
取 消