Loren on the Art of MATLAB

February 24th, 2009

Decomposing Embedded Images

Today I’d like to introduce a guest blogger, Jiro, who is an application engineer here at The MathWorks. Some of you may know him as one of the bloggers for the File Exchange Pick of the Week.

Contents

I recently presented to a group of freshman engineering students at Virginia Commonwealth University. I wanted to show something that was fun, eye-catching, and relatively easy to explain. So I created this image-related demo. I am not an expert in image processing, but I certainly had a lot of fun working on this example. For anyone interested in hardcore image processing, I would suggest also taking a look at Steve's Image Processing blog.

This example demonstrates how to embed an image into another image and how to decompose the images. The embedded image is created by storing two 8-bit RGB image as 16-bit RGB image. The primary image is stored in the most significant byte, while the secondary image is stored in the least significant byte. (See the section titled "Embed Function & Decode Function" of this post for the code). With this method, the secondary image can be concealed inside the primary image, without any loss of information.

This post focuses on the decomposition part of the demo.

This demo uses functions from the Image Processing Toolbox.

Setup

curImshowBorder = iptgetpref('ImshowBorder');
iptsetpref('ImshowBorder', 'tight');

Show Images

Here are two images that look the same. But are they??

  • peppers_BlueHills.png

  • peppers_trees.png

Would you believe me if I say these were very different images? Perhaps you'll believe MATLAB:

isequal(imread('peppers_BlueHills.png'), imread('peppers_trees.png'))
ans =
     0

In fact, they have completely different images embedded in them. You just can't tell with the naked eye.

Image Decomposition

Let's try to decompose the image and see what's hidden inside.

These are 16-bit RGB images. See the "Embed Function & Decode Function" section of this report to see the function that I used for creating these images.

imData = imread('peppers_BlueHills.png');
whos imData
  Name          Size                 Bytes  Class     Attributes

  imData      384x512x3            1179648  uint16              

The image contains two 8-bit images. The primary image is stored in the most significant byte and the secondary image is stored in the least significant byte.

Convert RGB 3-D Array to a Vector

We'll be using TYPECAST to convert the data type, and the function requires a vector.

pixelVals = imData(:);
pixelVals(1:10)
ans =
  16097
  16352
  16862
  16348
  16346
  16344
  16087
  16854
  16082
  15822

Convert UINT16 to UINT8

Next, convert the data type from UINT16 to UINT8. In doing so, we'll use TYPECAST (instead of CAST) to preserve the data.

pixelValsConv = typecast(pixelVals, 'uint8');
whos pixelVals*
  Name                     Size              Bytes  Class     Attributes

  pixelVals           589824x1             1179648  uint16              
  pixelValsConv      1179648x1             1179648  uint8               

Notice that pixelValsConv has twice as many elements. This is because there are two 8-bit values to a 16-bit value.

Separate Two Images

We'll reshape them to separate out the least and the most significant bytes.

pixelValsConv = reshape(pixelValsConv, 2, [])';
pixelValsConv(1:10, :)
ans =
  225   62
  224   63
  222   65
  220   63
  218   63
  216   63
  215   62
  214   65
  210   62
  206   61

On a system with "little-endian" architecture, the first column is the least significant byte and the second column is the most significant column.

 (first pixel)  62*256 + 225 = 16097

On a "big-endian" architecture system, the order is switched.

[cmp,maxsize,endian] = computer

if strcmp(endian, 'L')
  imOrder = [2 1];
else
  imOrder = [1 2];
end
cmp =
PCWIN
maxsize =
  2.1475e+009
endian =
L

We'll take each column and reshape them as the primary and secondary images.

imDataPrimary   = reshape(pixelValsConv(:, imOrder(1)), size(imData));
imDataSecondary = reshape(pixelValsConv(:, imOrder(2)), size(imData));

We can see that we end up with two images, both of which are now UINT8 images.

whos imData*
  Name                    Size                 Bytes  Class     Attributes

  imData                384x512x3            1179648  uint16              
  imData2Primary        384x512x3             589824  uint8               
  imData2Secondary      384x512x3             589824  uint8               
  imDataPrimary         384x512x3             589824  uint8               
  imDataSecondary       384x512x3             589824  uint8               

Show Primary and Secondary Images

figure;imshow(imDataPrimary);
figure;imshow(imDataSecondary);

Decomposing the Second Image

We'll do the same thing for the second image we read in. Let's see what's embedded in that one. We'll use decodeImage.m which is a function with the above algorithm.

[imData2Primary, imData2Secondary] = decodeImage('peppers_trees.png');
figure; imshow(imData2Primary);
figure; imshow(imData2Secondary);

How It Works

So, how does this work? Why is the secondary image unrecognizable by the naked eye? That's because the secondary image is stored in the least significant byte.

To understand this, let's take a look at a single row of pixels in one of the RGB planes. We'll look at row 150 of the red plane.

pixelRow16 = imData(150, :, 1);  % row 150, red plane

We covert this vector to UINT8 using TYPECAST:

pixelRow8 = typecast(pixelRow16, 'uint8');
pixelRow8 = reshape(pixelRow8, 2, []);
pixelRow8(:, 1:10)
ans =
   64   60   61   61   57   60   61   61   61   61
   67   70   71   70   70   72   74   69   63   63

On a little-endian architecture system, the first row is the secondary image, and the second row is the primary image. Next, we'll create a UINT16 vector with only the primary image.

% set the secondary image vector to ZERO
pixelRow8Main  = [zeros(1, size(pixelRow8, 2), 'uint8'); pixelRow8(2, :)];
pixelRow16Main = typecast(pixelRow8Main(:)', 'uint16');

Now, let's compare the values of the total image vector with those of the primary image vector.

figure;
ax1 = axes;hold on;
plot(pixelRow16);
plot(pixelRow16Main, 'r');
xlabel('Pixel Count'); ylabel('Pixel Value (UINT16)');
legend('Total Image', 'Primary Image', 'Location', 'NorthWest');
rectX = 160; rectY = 15000; rectW = 30; rectH = 3500;
rectangle('Position', [rectX, rectY, rectW, rectH]);
dar = get(ax1, 'DataAspectRatio'); dar = dar(2)/dar(1);
w = .3; h = w*(rectH/rectW)/dar;
ax2 = axes(...
  'Units', 'Normalized', ...
  'Position', [.6 .2 w h], ...
  'Box', 'on', ...
  'LineWidth', 2, ...
  'XTick', [], ...
  'Ytick', [], ...
  'Color', [.95 .95 .95]);hold on;
xlabel('Magnified Region');
plot(pixelRow16);
plot(pixelRow16Main, 'r');
xlim([rectX, rectX+rectW]);
ylim([rectY, rectY+rectH]);

As this figure shows, the primary image represents the majority of the information. The information for the secondary image is much smaller relative to the primary image. But if we look at the data for the secondary image by itself, you see that we have the full 8-bit information.

figure;
plot(pixelRow8(1, :), 'g'); ylim([0 300]);
legend('Secondary Image', 'Location', 'NorthWest');
title('Secondary Image');
xlabel('Pixel Count'); ylabel('Pixel Value (UINT8)');

The following animations show how the secondary image data becomes more apparent as we subtract out the primary image data. Animations created using ANYMATE.

  • 2-D Animation

  • 3-D Animation

Embed Function & Decode Function

These are the actual functions for creating and decoding embedded images.

help embedImage
  EMBEDIMAGE  Embed an image into another image
    EMBEDIMAGE(PRIMARYIMAGE, IMAGETOEMBED) embeds the image file
    IMAGETOEMBED into the image file PRIMARYIMAGE. Both PRIMARYIMAGE and
    IMAGETOEMBED must be valid file names.
 
    The image files must be 8-bit images. The output image file will be a
    16-bit PNG image, named PRIMARYIMAGE_IMAGETOEMBED.png. The primary
    image data will be stored in the most significant byte and the
    embedded image data will be stored in the least significant byte.
 
    Example:
      embedImage('trees.tif', 'football.jpg');
 
    See also DECODEIMAGE.
 
  Jiro Doke
  Jan 24, 2009.

help decodeImage
  DECODEIMAGE  Decode embedded image.
    DECODEIMAGE(IMAGEFILE) decodes embedded image file IMAGEFILE.
    IMAGEFILE must be a valid file name. It will display a figure with 3
    axes. The top axis is the original image. The bottom left is the
    primary image, and the bottom right is the embedded hidden image.
 
    [PRIMARY, HIDDEN] = DECODEIMAGE(IMAGEFILE) returns the primary image
    and the hidden image data as RGB data.
 
    This function only works on images created by embedImage.m.
 
    Example:
      % create embedded image
      embedImage('trees.tif', 'football.jpg');
      decodeImage('trees_football.png');
 
    See also EMBEDIMAGE.
 
  Jiro Doke
  Jan 24, 2009.

Cleanup

iptsetpref('ImshowBorder', curImshowBorder);

Comments?

I hope you liked this example. Images make nice examples because they are visual. Let me know if you have other fun, pedagogical examples that involve graphics. Put them on the File Exchange, and I'll be sure to take a look at it as a potential Pick of the Week!


Get the MATLAB code

Published with MATLAB® 7.7

10 Responses to “Decomposing Embedded Images”

  1. George Kapodistrias replied on :

    Hi Loren,

    Great blog. Thanks,

    gk.

  2. adam replied on :

    one of the coolest posts in a while. i can’t wait to really go through it in detail.

  3. Seth Popinchalk replied on :

    Jiro - great example! This reminds me of Steve’s post on the MATLAB default image. Do you ever use this technique to hide information?

  4. Jiro replied on :

    Glad you guys enjoyed it.

    Seth, I actually didn’t know about Steve’s post and the default images. I knew about the little boy, but I had no idea all of these other images existed. That’s really cool!

  5. Gavin replied on :

    Does this only work with 2 images or can it be applied to multiple layers of image?

    Could for instance a 16bit tiff contain several hundred layers of 8bit images?

  6. Loren replied on :

    Gavin-

    You can pack more than 2 images into 64 bits, but you’d have to know how they are packed together. See this post on Steve’s blog for how 17 images are part of MATLAB’s default image.

    –Loren

  7. OysterEngineer replied on :

    This is a very complex topic for a layman like me. But, I’m happy to see it & I’m learning a bit.

    A couple basic questions:

    Up in near the top of the Image Decomposition section you do a whos on imData. How do you tell from the returned data that the image contains two 8-bit images? I don’t see anything in the returned data that would even hint about this.

    How does the recast operation on pixelValsConv automatically put the least & most significant bytes in the appropriate columns? Isn’t the output of the recast operation just the 1st half of the input vector in the 1st column of the output & the 2nd half in the 2nd column?

    Finally, this post pointed out a major shortcoming of the FRP for the cast function: It doesn’t have any text to warn users that the data may be changed.

  8. Jiro replied on :

    @OysterEngineer

    Thanks for your comments. I’m no expert in image processing, so it was certainly a learning and fun experience for me.

    1. You’re right. Just from the information provided by WHOS, you can’t tell that it contains two 8-bit images. It says the datatype is uint16, so I’m just telling you that I have stored two 8-bit data into 16-bits.

    2. (I’m not entirely sure what you mean by “recast”. I used TYPECAST and RESHAPE in my code.) TYPECAST puts the data starting from the least significant byte to the most significant byte (or vice versa, depending on the endiannes). When I passed a vector into TYPECAST, it did the conversion for each element (and returned a vector), so I had to RESHAPE it to put the different bytes into different columns.

    3. Good point about the lack of warning in CAST. I will submit a enhancement request for this.

  9. OysterEngineer replied on :

    My typo, I should have said reshape rather than recast.

    I guess the question really is, how do you know that the 1st half of the vector is the one level of byte while the 2nd half is the other level of byte? I understand how reshape splits the input vector to make an array. What I don’t understand is how you can count on typecast putting the pairs of bytes in each half of the output vector. The FRP for typecast is disturbingly vague on this detail.

    In reading the last paragraph of the Description in the FRP for typecast, I would conclude that the bytes in the output would be, in this case, in adjacent pairs of bytes, one per row & not separated into halves of the output, shaped into a vector.

  10. Jiro replied on :

    @OysterEngineer,

    I suppose you’re right that the FRP for TYPECAST does not explicitly explain how the output is formatted if the input is a vector. The example 2 in the FRP describes the behavior. Perhaps this behavior should be explicitly mentioned in the description section.

    I’ll pass this along. Thanks.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • Jun: I totally can not believe it, Loren. You are really helpful. Thank you so much, MATLAB master!
  • Loren: Wow folks- Always lots of interest when there’s a quickie to try out! I will only make 2 general...
  • Loren: Jun- ismember is your friend here: >> [aa,ind] = ismember(Array2,Arra y1) aa = 1 1 1 1 1 1 1 ind = 1 2 1 4 4 3...
  • Dan: I like the first way better than the second way. Combining the arrays into one and running any is nice, although...
  • James Myatt: How about I = (a == 0 | b == 0); a(I) = []; b(I) = [];
  • Tunc: Hello Loren, love your blog because of such inspiring and challenging comments to such ’small’...
  • Pekka Kumpulainen: Here is my tradeoff. I usually want to keep the original variables as they are most probably...
  • Iain: Followup: Of course, to allow NaNs (counting them as non-zero): mask = (a~=0) & (b~=0); The mask says “a...
  • Matt Fig: I would usually go with something like this: y = a&b; x = a(y); y = b(y); But I was surprised to find...
  • kk: c=all([a;b]) a(c) a(b)

These postings are the author's and don't necessarily represent the opinions of The MathWorks.