我试图理解为什么原始图像没有附带此代码。生成的图片receive
颜色为黄色,而不是与图片Img_new
相似。
Img=imread(‘lena_color.tif’);
Img_new=rgb2gray(img);
Send=zeroes(size(Img_new);
Receive= zeroes(size(Img_new);
Mask= rand(size(Img_new);
for i=1 :256
for j=1:256
Send(i,j)=xor( Img_new(i,j),mask(i,j));
End
End
image(send);
imshow(send);
for i=1 :256
for j=1:256
receive(i,j)=xor( send(i,j),mask(i,j));
End
End
image(receive);
imshow(receive);
我做错了什么?
答案 0 :(得分:5)
您的代码中存在几个问题。
end
和End
不一样。 receive
和send
也是如此。 MATLAB的xor
返回逻辑xor
,因此当它看到两个值(或值的矩阵)时,无论是234 xor 123
还是12 xor 23
都无关紧要因为它相当于1 xor 1
和1 xor 1
。 您正在寻找 bitxor
,它在矩阵的每个元素上执行按位xor
,我在下面的代码中使用了它。这是您使用pixel == xor(xor(pixel,key),key)
操作检索信息的唯一方法(假设您想要这样做)。
rand
返回0 - 1
的实际值;因此,要成功按位xor
,您需要0 - 255
中的数字。因此,在我的代码中,您会看到mask
具有来自0-255
的随机值。
注意:我使用过peppers.png
因为它在MATLAB中可用。将其替换为lena_color.tif
。
%%# Load and convert the image to gray
img = imread('peppers.png');
img_new = rgb2gray(img);
%%# Get the mask matrix
mask = uint8(rand(size(img_new))*256);
%%# Get the send and receive matrix
send = bitxor(img_new,mask);
receive = bitxor(send,mask);
%%# Check and display
figure;imshow(send,[0 255]);
figure;imshow(receive,[0 255]);
%%# Get mask and img somehow (imread, etc.)
img = double(img);
mask_rgb = double(repmat(mask,[1 1 3]));
bitxor(img,mask);
如果相反,您选择将所有内容uint8
而不是双倍,那么我建议您检查是否在任何地方丢失数据。 img
为uint8
,因此没有任何损失,但如果mask
的任何值大于255
,则将其double
设为{{1}}将导致损失在数据中。