Generate hash value of images in MATLAB

27 次查看(过去 30 天)
Please tell me the set of codes or commands to compute the hash value of a grey/color images using SHA-256 hash in MATLAB R2016a. Is there any code to compute SHA256 hash for matrices and images?, if there please sugget me.
Please help me with the above question.
Thanks a lot in advance

回答(1 个)

Vincent Crasborn
Vincent Crasborn 2020-4-3
I recently had the same issue, so here's my code (works only on Windows as it uses .Net)
Note that I'm not a pro matlab user, so if anyone has suggestions on how to do this more efficiently then feel free to let me know.
In addition: I couldn't find a standard way of hashing an image, so what this code does is convert all RGB values to characters, and concatenates them into one long string. the hash of that string is then output, either as a uint8 array or as a hex character array. This, in turn, is from https://uk.mathworks.com/matlabcentral/answers/265421-how-to-compute-the-hash-of-a-string-using-sha-algorithms#answer_207625
I can't attach the image I used directly, since the forum doesn't support .tiff, but it's available here: http://sipi.usc.edu/database/database.php?volume=misc&image=10#top
I hope that helps.
clear all, close all, clc;
image = imread('baboon.tiff'); % Read in the image
[m, n, ~] = size(image); % Gives rows, columns, ignores number of channels
% Starts by separating the image into RGB channels
message_R = image(:,:,1); % Red channel
message_G = image(:,:,2); % Green channel
message_B = image(:,:,3); % Blue channel
flat_R = reshape(image(:,:,1)',[1 m*n]); % Reshapes Red channel matrix into a 1 by m*n uint8 array
flat_G = reshape(image(:,:,2)',[1 m*n]); %
flat_B = reshape(image(:,:,3)',[1 m*n]); %
flat_RGB = [flat_R, flat_G, flat_B]; % Concatenates all RGB vals, into one long 1 by 3*m*n array
string_RGB = num2str(flat_RGB); % Converts numeric matrices to a string
string_RGB = string_RGB(~isspace(num2str(string_RGB))); % Removes spaces - though this is not strictly necessary I think
% Perform hashing
sha256hasher = System.Security.Cryptography.SHA256Managed; % Create hash object (?) - this part was copied from the forum post mentioned above, so no idea what it actually does
imageHash_uint8 = uint8(sha256hasher.ComputeHash(uint8(string_RGB))) % Find uint8 of hash, outputs as a 1x32 uint8 array
imageHash_hex = dec2hex(imageHash_uint8) % Convert uint8 to hex, if necessary. This step is optional depending on your application.

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by