Matlab 实现感知哈希法

BinToHex.m

function str=BinToHex(A)
str='';
    for  i=1:8
        for j=1:4:8
            temp = dec2hex(bin2dec(num2str(A(i,j:j+3))));
            str=strcat(str,num2str(temp));
        end
    end
end

Perceptual_hash_algorithm

function A=Perceptual_hash_algorithm(str)

img=imread(str);

%转换为灰度图
temp=rgb2gray(img);

%缩小到8*8
img2=imresize(temp,[8 8]);

%划分灰度等级为64
img2=uint8(double(img2)/4);

%求均值
average=mean(mean(img2));

%生成二进制矩阵
img2(img2<average) = 0;
img2(img2>=average) = 1;
A=img2;
%将二进制转换为16进制,指纹
Str=BinToHex(A)
end

hash.m

clear all;
clc;
E=Perceptual_hash_algorithm('G:\\壁纸\\高达UC\\gundam_uc-026.jpg');
F=Perceptual_hash_algorithm('G:\\壁纸\\高达UC\\26_A.jpg');
G=xor(E,F);
a=sum(G(:)==1)

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/79987380