Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rotate an image in the frequency domain?

I've heard that it should be possible to do a lossless rotation on a jpeg image. That means you do the rotation in the frequency domain without an IDCT. I've tried to google it but haven't found anything. Could someone bring some light to this?

What I mean by lossless is that I don't lose any additional information in the rotation. And of course that's probably only possible when rotating multiples of 90 degrees.

like image 812
onemasse Avatar asked Feb 13 '11 09:02

onemasse


People also ask

How do I change the rotating image?

Select the object rotate. Under Drawing Tools (or Picture Tools if you're rotating a picture), on the Format tab, in the Arrange group, select Rotate, and then select More Rotation Options.

How do I rotate an image in html5?

Syntax: transform: rotate(90deg);

What is the frequency domain of an image?

In the frequency domain, a digital image is converted from spatial domain to frequency domain. In the frequency domain, image filtering is used for image enhancement for a specific application. A Fast Fourier transformation is a tool of the frequency domain used to convert the spatial domain to the frequency domain.

Which function is used to rotate the image?

rotate() function is used to rotate an image by an angle in Python.


1 Answers

You do not need to IDCT an image to rotate it losslessly (note that lossless rotation for raster images is only possible for angles that are multiples of 90 degrees).

The following steps achieve a transposition of the image, in the DCT domain:

  1. transpose the elements of each DCT block
  2. transpose the positions of each DCT block

I'm going to assume you can already do the following:

  • Grab the raw DCT coefficients from the JPEG image (if not, see here)
  • Write the coefficients back to the file (if you want to save the rotated image)

I can't show you the full code, because it's quite involved, but here's the bit where I IDCT the image (note the IDCT is for display purposes only):

Size s = coeff.size();
Mat result = cv::Mat::zeros(s.height, s.width, CV_8UC1);

for (int i = 0; i < s.height - DCTSIZE + 1; i += DCTSIZE)
for (int j = 0; j < s.width  - DCTSIZE + 1; j += DCTSIZE)
{
    Rect rect = Rect(j, i, DCTSIZE, DCTSIZE);
    Mat dct_block = cv::Mat::Mat(coeff, rect);
    idct_step(dct_block, i/DCTSIZE, j/DCTSIZE, result);
}

This is the image that is shown:

Lenna

Nothing fancy is happening here -- this is just the original image.

Now, here's the code that implements both the transposition steps I mentioned above:

Size s = coeff.size();
Mat result = cv::Mat::zeros(s.height, s.width, CV_8UC1);

for (int i = 0; i < s.height - DCTSIZE + 1; i += DCTSIZE)
for (int j = 0; j < s.width  - DCTSIZE + 1; j += DCTSIZE)
{
    Rect rect = Rect(j, i, DCTSIZE, DCTSIZE);
    Mat dct_block = cv::Mat::Mat(coeff, rect);
    Mat dct_bt(cv::Size(DCTSIZE, DCTSIZE), coeff.type());
    cv::transpose(dct_block, dct_bt);                // First transposition
    idct_step(dct_bt, j/DCTSIZE, i/DCTSIZE, result); // Second transposition, swap i and j
}

This is the resulting image:

transposed

You can see that the image is now transposed. To achieve proper rotation, you need to combine reflection with transposition.

EDIT

Sorry, I forgot that reflection is also not trivial. It also consists of two steps:

  1. Obviously, reflect the positions of each DCT block in the required axis
  2. Less obviously, invert (multiply by -1) each odd row OR column in each DCT block. If you're flipping vertically, invert odd rows. If you're flipping horizontally, invert odd columns.

Here's code that performs a vertical reflection after the transposition.

for (int i = 0; i < s.height - DCTSIZE + 1; i += DCTSIZE)
for (int j = 0; j < s.width  - DCTSIZE + 1; j += DCTSIZE)
{
    Rect rect = Rect(j, i, DCTSIZE, DCTSIZE);
    Mat dct_block = cv::Mat::Mat(coeff, rect);

    Mat dct_bt(cv::Size(DCTSIZE, DCTSIZE), coeff.type());
    cv::transpose(dct_block, dct_bt);

    // This is the less obvious part of the reflection.
    Mat dct_flip = dct_bt.clone();
    for (int k = 1; k < DCTSIZE; k += 2)
    for (int l = 0; l < DCTSIZE; ++l)
        dct_flip.at<double>(k, l) *= -1;

    // This is the more obvious part of the reflection.
    idct_step(dct_flip, (s.width - j - DCTSIZE)/DCTSIZE, i/DCTSIZE, result);
}

Here's the image you get:

final

You will note that this constitutes a rotation by 90 degrees counter-clockwise.

like image 109
mpenkov Avatar answered Sep 23 '22 16:09

mpenkov