Skip to content

DHash

Bases: BaseHasher

dHash compare algorithm

Source code in tools/comparer/img_comparer/hasher/dhash.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class DHash(BaseHasher):
    """dHash compare algorithm"""

    @staticmethod
    def compute_hash(image_path: Path, core_size: int) -> Union[np.ndarray, None]:
        image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

        if image is None:
            return None

        resized_image = cv2.resize(image, (core_size + 1, core_size), interpolation=cv2.INTER_AREA)
        # лівий піксель яскравіший за правий — True, інакше False
        gradient_difference = resized_image[:, 1:] > resized_image[:, :-1]

        return gradient_difference.flatten()