How do I rotate an image in the frequency domain?

后端 未结 1 1801
眼角桃花
眼角桃花 2021-01-06 02:04

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 goog

相关标签:
1条回答
  • 2021-01-06 02:43

    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.

    0 讨论(0)
提交回复
热议问题