简体   繁体   中英

How can I compare two images to detect duplicates and cropped duplicates?

如何比较两个图像并确定它们是100%相似,还是仅改变颜色或裁剪?

Well, abstractly speaking, you need to define a similarity function, that compares two images. To determine if the images are "100% similar" (equal) you can do the following:

  • compare the sizes of the images
  • if the image sizes are the same simply subtract the pixels from each other
  • if ( sum( abs( pixel_1_i - pixel_2_j ) ) / num_pixels < threshold ) return true

For the case that images are differently colored, or cropped

  • apply an edge detector to both images
  • compute the cross-correlation (in the frequency domain, FFT)
  • find the highest peak
  • place the (smaller) edge map in the determined position
  • calculate the absolute error
  • if (error < threshold) return true

BTW: This approach will not work if your images are scaled or rotated.

Further Research:

  • cross-correlation : FFT (fast fourier transformation, link1 , link2 , FFT in C# ), zero-padding (needed for the FFT if the input signals have different sizes)
  • edge detection: Sobel , Canny (these are very common image processing filters, they should be available in a C# library, just like the FFT)

The following is a fairly simplistic approach to the problem and won't work well with two different photographs of the same subject taken from slightly different angles, but would work if you had two copies of the same image that you wanted to verify.

The case of two identical images is straightforward - just loop through the pixel arrays subtracting on RGB value from the other. If the difference is less than a small tolerance then the pixel is identical. Thus as soon as you find a pixel difference greater than the tolerance you know that the images are different.

You could allow for a certain number or percentage of differences to allow for differences causes by compression artefacts.

To check for alterations in colour you could look at the HLS (Hue, Lightness and Saturation) values instead. If the pixels have the same L & S values but a different H value then it's just the colour that's different (I think).

Cropping is more difficult as you have to try to find the location of the smaller image in the larger one.

You can use object descriptors such as:

SIFT - http://en.wikipedia.org/wiki/Scale-invariant_feature_transform

SURF - http://en.wikipedia.org/wiki/SURF

Then compare images by using calculated descriptors. Those descriptors will enable you to deal with rotated, scaled and slightly changed images.

Also the descriptors consist of oriented gradients meaning that those descriptors are robust to illumination and color changes as well.

You can use Accord.NET (SURF implementation).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM