The Art of Hiding (Part 3)
By: Alex Bișag
Tags: Image Processing, Steganography, Digital, Secrets, Python

This is the second part of The Art of Hiding series which presents steganography concepts and some easy ways in which it can be achieved. If you missed the first two parts or want to skim through them one more time, then look no further — you can find them here and here.

Photo by DavidZydd

Transform Domain Steganography

Compared to the spatial domain, which describes an image the way we perceive it, the frequency domain (the environment in which transform domain steganography operates) is way more abstract.

I mentioned Fourier in a previous article not because I’m into mathematics, but because, using the Fourier transform, we can express the components of images based on their frequencies, rather than their position and color. I know, it seems tricky and it took me a while to internalize what all of this means. Even though I can’t say that I’ve got it a hundred percent right, here are the steps that I’ve taken during my research that have helped me grasp the concept.

If you don’t have an engineering background (which I gracefully don’t) then you’re not playing with frequencies every day — even less with frequencies that describe a two-dimensional object, such as a picture. For a second, let’s talk about sounds. A sound is a wave. The classical approach to describe a wave uses amplitude and time, which looks like this.

Published on Wikipedia

The amplitude of a sound wave expresses how loud the sound it appears to be, but offers nothing more to the table. Applying the Fourier transform, which can be done using a not so pretty looking formulas, we can express the same signal in its frequency domain; a process that adds a little more information to our graphs.

Published on Wikipedia

The relation between the two representations is nicely captured in this animation, which made me understand how this works for sounds and other unidimensional continuous signals (but not for images, which are both two-dimensional and finite).

Later on, I stumbled upon another formula maths magic called the Discrete Fourier Transform (or DFT), which describes how we can produce the same mapping for a discrete signal. The result of DFT is composed of multiple pairs of a real and a complex number and the image can be converted back using the inverse function without damaging its structure.

While signals are nice and frequencies are cool, it’s hard to observe a direct resemblance between a picture of a cat and a sine wave. The thing is that any image, no matter how complex, can be represented as a sum of waves with different frequencies. This video explains it way better than I can, so check it out if you feel that the aforementioned sentence seems weird.

Even though spectral representations produced by DFT offer a nice amount of information, working with DFT (which relies on complex numbers) is not always a pleasure. Based on the Fourier Transform, an electrical engineer called Nasir Ahmed invented the Discrete Cosine Transform which, as you’ve probably guessed, uses cosine as its base function and produces real coefficients. DCT is frequently used in data compression with JPEG being the perfect example.

Lenna

Lenna DFT Representation

Lenna DCT Representation

Now, after we went through the complex concepts that will help us further, I invite you to meet Lenna, the equivalent of cryptography’s Bob & Alice in image processing. The next two images are nothing more than her frequency domain representations obtained using DFT and DCT (applied on blocks of 8x8 pixels). This is exactly how my depiction of a bad TV signal looks like. In this case, though, the images mean a little bit more than that. You’ve probably noticed that, unlike our image, Lenna is black and white. Both DFT and DCT can be applied to two-dimensional objects (such as a matrix) but cannot be applied directly on a colored image which is stored as three different matrices. In our example, we’ll use DCT on a colored image by separating the color planes and process each one independently.

Before we jump to coding, we have to talk a little bit more about frequencies and the frequency domain representations. First of all, the frequency level represents how fast the intensity of pixels changes throughout an image. A picture with a single color has a frequency close to 0 because there’s no variation. A picture with a gradient will have a small frequency because the changes between shades happen gradually, while a picture with black and white pixels placed consecutively will have a big frequency because the variation of intensity that appears between two neighbor pixels is maximum.

When we talk about regular images, high-frequency regions are represented by prominent edges and very detailed components, while low-frequency regions are captured by uniform colored areas. The DFT representation is commonly used for spectral analysis. Understanding it will help us to understand what we can achieve by playing with frequencies. In Lenna’s DFT representation, the zero-frequency component is placed in the center of the image. From black to bright white, from low to high, each pixel in the spectrum shows how much a specific two-dimensional wave pattern is involved in the process of creating the original image.

A regular picture is represented using mostly low-frequency components, so we expect to see a bright core. The further from the center we are, the higher the frequency. In simpler terms; for DFT, the corners represent edges and the center smooth surfaces. DCT representation has a similar interpretation, even though their pictures don’t look alike. In DCT, the zero-frequency point is represented in the upper-left corner while the areas in the opposite direction capture the high-frequency components. Lenna’s DCT looks confusing because DCT was applied for each 8x8 block of pixels from the original image. The resultant coefficients replaced the pixels in the final output. The bigger the coefficient, the whiter its pixel will appear.

We can distinguish the blocks that have a higher frequency in the sections representing Lenna’s contour. When the inverse method is applied, each coefficient is multiplied with the patterns captured in the following image, based on its position inside the matrix.

Published on Wikipedia

After this operation is done, we should obtain a representation that closely resembles our initial picture. Yep, sometimes math is truly magical. In terms of image processing, the frequency domain representation is golden because, using this form, many properties of the original picture can be altered without directly touching the pixels. For example, we can apply a blur filter if we tune out the values of the coefficients placed near the edges in a DFT representation. Steganography tries to employ the same concepts: embedding our message without altering individual pixels, but spreading it throughout the image by changing the coefficients of its DCT representation. We’ve gone through a lot of theory so far, it’s time to put it into practice.