将RGB图像读入二进制文件并在Matlab中将其显示为RGB [英] Read RGB image into binary and display it as RGB in Matlab

查看:614
本文介绍了将RGB图像读入二进制文件并在Matlab中将其显示为RGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题基于之前提出的问题通过LSB替换方法理解图像隐写术

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

为了使代码高效并减少均方误差(MSE),建议是:读取文件原样并将其转换为 de2bi(fread(fopen(filename)),8)。将这些位嵌入封面图像中,所需的最小k因子,大概是1或2.当你提取你的秘密时,你将能够重建原始文件。这是我一直在尝试但在某些地方我做错了,因为我没有得到任何显示。然而,MSE确实减少了。基本上,我很困惑如何将图像转换为二进制,对该数据执行算法并在提取后显示图像。

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.

有人可以帮忙吗?

推荐答案

加载字节流而不是秘密的像素数组将导致更小的有效负载。它的尺寸会有多小,取决于图像格式以及颜色的重复程度。

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()要求文件名并在所述文件名是有效图像文件时加载像素数组。加载文件的字节流并将其传递给 imread()是没有意义的。你想要的是这个

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.

有效负载的大小为 length(secretBytes)* 8 ,这必须适合您的封面图片。如果您决定为每个像素嵌入 k 位,则必须满足以下要求

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

如果您只想嵌入一个颜色平面,无论您的封面介质是RGB还是灰度,您都需要将其修改为

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


  • 停止流程

  • 要求用户填写较小的文件

  • 增加k

  • 包含更多颜色平面(如果有)

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

以下是仅在最低有效位(k = 1)中嵌入一个色彩平面的原型。

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.

如果使用k> 1或多个颜色平面嵌入,逻辑会变得更复杂,您必须小心如何实现更改。

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.

例如,您可以选择一次嵌入每个颜色平面,直到用完要隐藏的位为止,或者您可以用<展平整个像素阵列code> coverImage(:) ,它将嵌入每个像素的RGB中,一次一个像素,直到你用完比特为止。

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.

如果嵌入k> 1,则必须用0填充向量,直到其长度可被 k 。然后你可以将你的比特组合成k与

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)')';

要嵌入它们,你想要回归使用 bitand() bitor()

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);

还有更多细节,比如为消息长度准确提取24位而且我压力不够你必须非常仔细地考虑如何实现所有这些事情。你不能只是从不同的代码片段中缝合部分,并希望一切都能按你想要的去做。

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.

这篇关于将RGB图像读入二进制文件并在Matlab中将其显示为RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆