Home > Mobile >  Finding Hamming distance between 2 threshold image with python
Finding Hamming distance between 2 threshold image with python

Time:12-07

I'm doing iris recognition and I have 2 threshold images of iris. How can I do hamming distance between the 2 images with Python ? thx

enter image description here

Here you have the original image before polar transform:

enter image description here

and the code:

img_crop1 = cv.imread('crop_1.png')
    polar_img = cv.warpPolar(
        img_crop1, (256, 1024), (self.iris_1[0], self.iris_1[1]), self.iris_1[2] * 2, cv.WARP_POLAR_LINEAR)
    # Rotate it sideways to be more visually pleasing
    polar_img = cv.rotate(polar_img, cv.ROTATE_90_COUNTERCLOCKWISE)

    # crop image
    polar_img = polar_img[int(polar_img.shape[0] / 2)
                          : polar_img.shape[0], 0: polar_img.shape[1]]
    polar_img = cv.cvtColor(polar_img, cv.COLOR_BGR2GRAY)

    _, threshold = cv.threshold(polar_img, 100, 255, cv.THRESH_BINARY)
    cv.imwrite("foreground.png", threshold)

CodePudding user response:

The following should work: sum(im1 xor im2)

CodePudding user response:

I'm going to assume you have 2 thresholded images imageTH_1 and imageTH_2 that is we have binary images, 0 & 1 representing black & white respectively.

First flatten you images so they are now 1D arrays,

import numpy as np

flat_1 = imageTH_1.flatten()
flat_2 = imageTH_2.flatten()

Now you can use Scipy's Hamming distance calculator

from scipy.spatial import distance

ham_dist = distance.hamming(flat_1, flat_2)
  • Related