Read RGB image into binary and display it as RGB in Matlab

我的未来我决定 提交于 2019-12-11 16:43:56

问题


This question is based on the one asked earlier Understanding image steganography by LSB substitution method

In order to make the code efficient and reduce the mean square error (MSE) the suggestion was: "read the file as is with and convert it to bits with de2bi(fread(fopen(filename)), 8). Embed these bits to your cover image with the minimum k factor required, probably 1 or 2. When you extract your secret, you'll be able to reconstruct the original file." This is what I have been trying but somewhere I am doing wrong as I am not getting any display. However, the MSE has indeed reduced. Basically, I am confused as to how to convert the image to binary, perform the algorithm on that data and display the image after extraction.

Can somebody please help?


回答1:


Loading the byte stream instead of the pixel array of the secret will result to a smaller payload. How smaller it'll be depends on the image format and how repetitive the colours are.

imread() requires a filename and loads a pixel array if said filename is a valid image file. Loading the byte stream of the file and passing that to imread() makes no sense. What you want is this

% read in the byte stream of a file
fileID = fopen(filename);
secretBytes = fread(fileID);
fclose(fileID);

% write it back to a file
fileID = fopen(filename);
fwrite(fileID, secretBytes);
fclose(fileID);

Note that the cover image is loaded as a pixel array, because you'll need to modify it.

The size of your payload is length(secretBytes) * 8 and this must fit in your cover image. If you decide to embed k bits per pixel, for all your colour planes, the following requirement must be met

secretBytes * 8 <= prod(size(coverImage)) * k

If you want to embed in only one colour plane, regardless of whether your cover medium is an RGB or greyscale, you need to modify that to

secretBytes * 8 <= size(coverImage,1) * size(coverImage,2) * k

If this requirement isn't met, you can choose to

  • stop the process
  • ask the user for a smaller file to embed
  • increase k
  • include more colour planes, if available

The following is a prototype for embedding in one colour plane in the least significant bit only (k = 1).

HEADER_LEN = 24;

coverImage = imread('lena.png');
secretBytes = uint8('Hello world'); % this could be any byte stream

%% EMBEDDING
coverPlane = coverImage(:,:,1);     % this assumes an RGB image
bits = de2bi(secretBytes,8)';
bits = [de2bi(numel(bits), HEADER_LEN) bits(:)'];
nBits = length(bits);
coverPlane(1:nBits) = bitset(coverPlane(1:nBits),1,bits);
coverImage(:,:,1) = coverPlane;

%% EXTRACTION
nBits = bi2de(bitget(coverPlane(1:HEADER_LEN),1));
extBits = bitget(coverPlane(HEADER_LEN+1:HEADER_LEN+nBits),1);
extractedBytes = bi2de(reshape(extBits',8,length(extBits)/8)')';

Along with your message bytes you have to embed the length of the secret, so the extractor knows how many bits to extract.

If you embed with k > 1 or in more than one colour planes, the logic becomes more complicated and you have to be careful how you implement the changes.

For example, you can choose to embed in each colour plane at a time until you run out of bits to hide, or you can flatten the whole pixel array with coverImage(:), which will embed in the RGB of each pixel, one pixel at a time until you run out of bits.

If you embed with k > 1, you have to pad your bits vector with 0s until its length is divisible by k. Then you can combine your bits in groups of k with

bits = bi2de(reshape(a',k,length(bits)/k)')';

And to embed them, you want to resort back to using bitand() and bitor().

coverPlane(1:nBits) = bitor(bitand(coverPlane(1:nBits), bitcmp(2^k-1,'uint8')), bits);

There are more details, like extracting exactly 24 bits for the message length and I can't stress enough you have to think very carefully how you implement all of those things. You can't just stitch parts from different code snippets and expect everything to do what you want it to do.




回答2:


I've made some modifications to your code to get this to work regardless of what the actual image is. However, they both need to be either colour or grayscale. There are also some errors your code that would not allow me to run it on my version of MATLAB.

Firstly, you aren't reading in the images properly. You're opening up a byte stream for the images, then using imread on the byte stream to read in the image. That's wrong - just provide a path to the actual file.

Secondly, the images are already in uint8, so you can perform the permuting and shifting of bits natively on this.

The rest of your code is the same as before, except for the image resizing. You don't need to specify the number of channels. Also, there was a syntax error with bitcmp. I used 'uint8' instead of the value 8 as my version of MATLAB requires that you specify a string of the expected data type. The value 8 here I'm assuming you mean 8 bits, so it makes sense to put 'uint8' here.

I'll also read your images directly from Stack Overflow. I'll assume the dinosaur image is the cover while the flower is the message:

%%% Change
x = imread('https://i.stack.imgur.com/iod2d.png');         % cover message
y  = imread('https://i.stack.imgur.com/Sg5mr.png');    % message image
n = input('Enter the no of LSB bits to be subsituted- ');

                                     %%% Change
S = uint8(bitor(bitand(x,bitcmp(2^n-1,'uint8')),bitshift(y,n-8))); %Stego
E = uint8(bitand(255,bitshift(S,8-n))); %Extracted

origImg = double(y);   %message image
distImg = double(E);   %extracted image

[M N d] = size(origImg);
distImg1=imresize(distImg,[M N]); % Change

figure(1),imshow(x);title('1.Cover image')
figure(2),imshow(y);title('2.Message to be hide')
figure(3),imshow((abs(S)),[]);title('3.Stegnographic image')
figure(4),imshow(real(E),[]); title('4.Extracted image');

This runs for me and I manage to reconstruct the message image. Choosing the number of bits to be about 4 gives you a good compromise between the cover and message image.



来源:https://stackoverflow.com/questions/49679958/read-rgb-image-into-binary-and-display-it-as-rgb-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!