开发者

Why does XORing the red RGB image channel, saving, reloading and XORing again not give back the original image?

This is the code I use to change the red value

pic=imread('farooq1.jpg');
[imr,imc,clr]=size(pic);

for row=1:imr
    for col=1:imc
        for k=1:clr  
          if(k==1) 
            img2(row,col,k)=bitxor(pic(row,col,k),66);
          else
            img2(row,col,k)=pic(row,col,k);   
          end
        end
    end
end开发者_高级运维

imwrite(img2,'farooq_scr.jpg');
imshow(img2);

But when I use this same code again on the resulting file to undo XORing, the result does not match the original picture.

Please suggest me if you know a better way of doing this.


The reason you're unable to recover your original image has nothing to do with the code for processing the red color plane, which works correctly but can be done more efficiently and succinctly using the vectorized code suggested by Jonas:

img2 = pic;
img2(:,:,1) = bitxor(img2(:,:,1),66);

The problem you're having is actually a result of the format you're saving the image file in. By default, JPEG image files created using IMWRITE are saved using lossy compression to reduce the file size. This results in a loss of image information that makes it impossible to recover your original image when you load that image and reapply your processing.

You can fix your problem by specifying that lossless compression is used when saving your JPEG image, like so:

imwrite(img2,'farooq_scr.jpg','Mode','lossless');


EXAMPLE:

Here's an example using a JPEG image 'greens.jpg' that's included with the Image Processing Toolbox:

rawImg = imread('greens.jpg');  %# Load the original image
subplot(2,2,1);
imshow(rawImg);                 %# Display the original image
title('Original Image');

modImg = rawImg;                           %# Initialize a modified image
modImg(:,:,1) = bitxor(modImg(:,:,1),66);  %# Modify the red plane of the image
subplot(2,2,2);
imshow(modImg);                            %# Display the modified image
title('Modified Image');

imwrite(modImg,'lossy_image.jpg');     %# Save with lossy compression
lossyImg = imread('lossy_image.jpg');          %# Reload the image
lossyImg(:,:,1) = bitxor(lossyImg(:,:,1),66);  %# Reprocess the image
subplot(2,2,3);
imshow(lossyImg);                              %# Display the image
title({'Using Lossy Image' 'to Recover Original'});

imwrite(modImg,'lossless_image.jpg',...      %# Save with lossless compression
        'Mode','lossless');
losslessImg = imread('lossless_image.jpg');          %# Reload the image
losslessImg(:,:,1) = bitxor(losslessImg(:,:,1),66);  %# Reprocess the image
subplot(2,2,4);
imshow(losslessImg);                                 %# Display the image
title({'Using Lossless Image' 'to Recover Original'});

And here's the resulting set of images:

Why does XORing the red RGB image channel, saving, reloading and XORing again not give back the original image?

You can see that saving the image with lossless compression allows you to recover the original when you reapply your processing steps. You can confirm that you get back the exact same image as the original using the function ISEQUAL:

>> isequal(rawImg,lossyImg)     %# Lossy image doesn't match; returns false
ans =
     0
>> isequal(rawImg,losslessImg)  %# Lossless image matches exactly; returns true
ans =
     1


You have to make sure that the new image is of the same class as the old one. Also, you can get rid of a few loops

pic = imread('peppers.png');

%# initialize img2 to the same size and same class as pic
%# by making it a copy of pic
img2 = pic;

%# apply bitxor to the red channel
img2(:,:,1) = bitxor(pic(:,:,1),66);

%# write the data
imwrite(img2,'peppers_scr.png')
imshow(img2)

%# show that the reverse works
img3 = imread('peppers_scr.png');
img3(:,:,1) = bitxor(img3(:,:,1),66);
figure %# create new figure 
imshow(img3)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜