%%OPAP
I1 = imread('Lena.jpg');
[M N] = size(I1);
I2 = I1;
x = input('input bits');
for i = 1 : M
for j = 1 : N
S = round(rand(1 , x))
pixel = I2(i,j);
count = 1;
for k = 1 : x
pixel2 = bitset(pixel , count , S(k));
count = count + 1;
if (count > x)
break
end
end
sgm = pixel2 - pixel;
if(sgm > 2^(x-1) && sgm < 2^x && pixel >= 2^x)
finalP = pixel2 - 2^x;
else if(sgm < -2^(x-1) && sgm > -2^x && pixel <= 255 - 2^x)
finalP = pixel2 + 2^x;
else
finalP = pixel2;
end
end
I2(i,j) = finalP;
end
end
imshow(I2)
来源:https://blog.csdn.net/V1AN_zzz/article/details/100533433