# Finding an Image's Orientation With DFT Frequency Amplitudes

I'm looking to get some general idea of an image's orientation so that I can then rotate it to the nearest 90 degree angle.

My idea on how to do this is to take the DFT on the image, then do a "radar sweep" to find which angle has the greatest sum of frequency amplitudes along the line, and call the winning angle the orientation.

I scan from 0 to 180 degrees instead of from 0 to 360, and make the radar sweep double sided since negative and positive frequencies seem to belong together.

For example, if i take the DFT of this image: The frequency amplitudes are: And if I do a radar sweep, I'd find the winning line / angle here: Using that information, I can know where the nearest 90 degrees is for that blue line, figure out how much i'd need to rotate the dft image to make that blue line vertical, but instead rotate the source image by that amount, to make that image orient towards the nearest 90 degrees.

Something like this, but actually level (I'm working out some bugs at the moment, in my implementation) Is this method sound? Are there any complications that I'm going to encounter when moving to more complex images such as complex patterns or photographs?

Edit: In the above I didn't explain the reasoning for what I am doing.

If you take the dft of simple images - like a single line, or a repeating pattern of black and white stripes - the resulting frequency amplitudes image has data that runs perpendicular to the direction of that line or those stripes.

Line this: DFT'ing to this: May (possibly naive!) understanding is that I should then be able to tell which direction (from the center) has the highest sum, and that will tell me the angle perpendicular to the largest represented orientation of the image.

From there, i can find how much i'd have to rotate that line to get to the nearest 90 degrees, and apply that rotation to the source image, to make it line up to it's nearest 90 degrees.

Is my thinking too simplistic, or is this a solid approach?

• I'd like to give the bounty to someone, but the answers I got, as good as they are (!) don't answer my question, just give alternatives to doing it the way I'm doing it. Aug 10, 2016 at 21:46

Have you heard of the Hough Transform? It is a possible alternative for tackling your problem.

• Let me know, in case you have specific questions. There is a lot of literature available. Implementation of the algorithm is rather easy and can be done in computational efficient ways, by e.g. vectorization/parallelization. Aug 3, 2016 at 21:44

You can also try by computing the gradient directions on every pixel, for instance using the Canny edge detector. You will wrap the angle to the range [0°, 180°).

Then compute the histogram of the angles, say with a 1° resolution. The dominant direction should appear as the most populated bin.

Instead of merely counting the pixels, you can weight the sum with the gradient magnitude, to favor strong edges and reduce the effect of noise.

• Thanks for the alternative, I appreciate hearing about that! Do you know whether the method i describe is sound or not? Aug 5, 2016 at 16:11
• @AlanWolfe: how do you justify it ?
– user7657
Aug 5, 2016 at 16:13
• Edited the question to add that, thank you for asking! Hopefully you'll be able to see what I'm going for... Aug 5, 2016 at 16:23
• A stripe image is essentially 1D and has a single-line DFT. This doesn't generalize to real images.
– user7657
Aug 5, 2016 at 16:31
• Wouldn't there be a "prominant stripe" through the middle perpendicular to where the most orienting angle is? I see evidence of that in images I've DFT'd at least. Aug 5, 2016 at 16:33