How can I make *this* program faster? image ownership & tampering authentication scheme

My program is written after the paper "A watermarking-based image ownership and tampering authentication scheme" by Chin-Chen Chang, Yih-Shih Hu and Tzu-Chuen Lu, Pattern Recognition Letter 27 (2006) 439-446.
It works so far, but it is extremely slow.
Can anyone give me some tips how to make it faster?
I've seen the vectorization tip of loops, but I don't know how to use it in my case. If this is a possibility can you show me an example of how to do it here?
I also used different Hash functions by Jan Simon (DataHash http://www.mathworks.com/matlabcentral/fileexchange/31272-datahash and this one http://www.mathworks.com/matlabcentral/fileexchange/7919-md5-in-matlab) but it didn't make much time difference.
Edit: you have to call it by rgb2gray(imread('your_picture.type')) Thank you!
function [PixelSignificant, PixelValues] = schemel(I)
%get picture info
A = size(I);
height = A(2);
length = A(1);
%preallocation -- more needed?
vector1 = ones(1,length);
vector2 = 1:length;
P2 = zeros(length, 3);
PixelSignificant = cell(length-2, height-2);
PixelValues = zeros(length, height);
exOrNew = zeros(1,4);
n = 0;
%Get all Pixel values
for i = 1:height
P2 = impixel(I, vector1, vector2);
for j = 1:length
PixelValues(j,i) = P2(j,1);
end
vector1 = vector1+1;
end
%create the new pixel values, with md5
%without edge
%IDK = 56, Users secret key SK =189 vll später über scheme eingeben
for j = 2:length-1
for i = 2:height-1
%feature extraction + hash
ConVal = strcat(num2str(PixelValues(j+1,i-1)), num2str(PixelValues(j,i-1)), num2str(PixelValues(j-1,i-1)), num2str(PixelValues(j-1,i)), num2str(PixelValues(j-1,i+1)), num2str(PixelValues(j,i+1)), num2str(PixelValues(j+1,i+1)), num2str(PixelValues(j+1,i)), num2str(n*(length-2)), '56', '189');
Y1 = mMD5(ConVal);
X1 = dec2bin(hex2dec(Y1(1:32)));
%length correcting: 132'bit' --why isnt't it always 128 bits long?
%somehow I had to concatenate each '0' by itself, otherwise they got lost
if size(X1,2) == 124
X2 = horzcat('0', X1, '0000');
X3 = horzcat('0', X2);
valuesBitwiseLonger = fliplr(horzcat('00', X3));
elseif size(X1,2) == 125
X2 = horzcat('0', X1, '0000');
X3 = horzcat('0', X2);
valuesBitwiseLonger = fliplr(horzcat('0', X3));
elseif size(X1,2) == 126
X2 = horzcat('0',X1,'0000');
valuesBitwiseLonger = fliplr(horzcat('0', X2));
elseif size(X1,2) == 127
valuesBitwiseLonger = fliplr(horzcat('0',X1,'0000'));
elseif size(X1,2) == 128
valuesBitwiseLonger = fliplr(horzcat(X1,'0000'));
end
%sigma and r
sigma = (PixelValues(j+1,i-1) - PixelValues(j,i-1))^2 + (PixelValues(j,i-1) - PixelValues(j-1,i-1))^2 + (PixelValues(j-1,i-1) - PixelValues(j-1,i))^2 + (PixelValues(j-1,i) - PixelValues(j-1,i+1))^2 + (PixelValues(j-1,i+1) - PixelValues(j,i+1))^2 + (PixelValues(j,i+1) - PixelValues(j+1,i+1))^2 +(PixelValues(j+1,i+1) - PixelValues(j+1,i))^2 + (PixelValues(j+1,i) - PixelValues(j+1,i-1))^2;
if (16 <= sigma) && (sigma <= 255)
r = 4;
elseif (8 <= sigma) && (sigma < 16)
r = 3;
else %0 <= sigma < 8
r = 2;
end
%folding start
%first two blocks folded
for u = 1 : r
ExOr1 = valuesBitwiseLonger(u);
ExOr2 = str2num(valuesBitwiseLonger(u + r));
exOrNew(u) = bitxor(str2num(ExOr1), ExOr2);
end
%Rest folded -- very 'slow' here -> 'too' many loops
for l = 2*r : 132 - r + 1
NewExOr = '';
for p = 1 : r
NewExOr = horzcat(NewExOr, num2str(exOrNew(p)));
end
for u = 1 : r
ExOr1 = valuesBitwiseLonger(l + u - 1);
ExOr2 = str2num(NewExOr(u));
exOrNew(u) = bitxor(str2num(ExOr1), ExOr2);
end
end
PixelSignificant(j,i) = {NewExOr};
%folding ende
end
%check wie schnell + pixelindex zähler
n = n+1
end
end

 Accepted Answer

You should be passing a second argument to dec2bin() so that you get out a consistent length.
Be warned that the results you get out of the dec2bin() are going to be mostly nonsense. You are doing hex2dec() on a 32 nibble hex value, which is going to give you a result between 0 and 340282366920938463463374607431768211456 but only 53 of the (up to) 129 bits at a time are ever going to be meaningful. MATLAB does not have any 128 bit integer data type.

More Answers (1)

Thank you so much, together with your help on this question I was able to speed up my program a lot!

Asked:

on 9 Jun 2016

Answered:

on 14 Jun 2016

Community Treasure Hunt

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

Start Hunting!