Image comparison algorithm

前端 未结 9 1634
礼貌的吻别
礼貌的吻别 2020-11-28 00:48

I\'m trying to compare images to each other to find out whether they are different. First I tried to make a Pearson correleation of the RGB values, which works also quite go

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 01:28

    I have one done this with an image histogram comparison. My basic algorithm was this:

    1. Split image into red, green and blue
    2. Create normalized histograms for red, green and blue channel and concatenate them into a vector (r0...rn, g0...gn, b0...bn) where n is the number of "buckets", 256 should be enough
    3. subtract this histogram from the histogram of another image and calculate the distance

    here is some code with numpy and pil

    r = numpy.asarray(im.convert( "RGB", (1,0,0,0, 1,0,0,0, 1,0,0,0) ))
    g = numpy.asarray(im.convert( "RGB", (0,1,0,0, 0,1,0,0, 0,1,0,0) ))
    b = numpy.asarray(im.convert( "RGB", (0,0,1,0, 0,0,1,0, 0,0,1,0) ))
    hr, h_bins = numpy.histogram(r, bins=256, new=True, normed=True)
    hg, h_bins = numpy.histogram(g, bins=256, new=True, normed=True)
    hb, h_bins = numpy.histogram(b, bins=256, new=True, normed=True)
    hist = numpy.array([hr, hg, hb]).ravel()
    

    if you have two histograms, you can get the distance like this:

    diff = hist1 - hist2
    distance = numpy.sqrt(numpy.dot(diff, diff))
    

    If the two images are identical, the distance is 0, the more they diverge, the greater the distance.

    It worked quite well for photos for me but failed on graphics like texts and logos.

提交回复
热议问题