1. 程式人生 > >Matlab 實現感知雜湊法

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)