Ask Your Question
2

How Do I Calculates The Angle Of Orientation Of The Image With The Fourier Transform?

asked 2014-05-15 18:41:58 -0600

rajib86 gravatar image

updated 2017-08-02 16:22:48 -0600

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <opencv2\opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <stdint.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;
/// Global variables
Mat src, src_gray,canny_image,padded,good_f_t, threshold_image,lowerRect,imageROI,dst, detected_edges;
int edgeThresh = 1;
int lowThreshold;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
char* window_name = "Edge Map";
void CannyThreshold(int, void*)
{
  blur( src_gray, detected_edges, Size(3,3) );
    Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
    dst = Scalar::all(0);
    src.copyTo( dst, detected_edges);
    imshow( window_name, dst );
   Mat padded;                           
    int m = getOptimalDFTSize( dst.rows );
    int n = getOptimalDFTSize( dst.cols ); 
    copyMakeBorder(dst, padded, 0, m - dst.rows, 0, n - dst.cols, BORDER_CONSTANT, Scalar::all(0));
    Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};      
    Mat complexdst;
    merge(planes, 2, complexdst);        
    dft(complexdst, complexdst);            

    split(complexdst, planes);                   
    magnitude(planes[0], planes[1], planes[0]);
    Mat magdst = planes[0];
    magdst += Scalar::all(1);                    
    log(magdst, magdst);
    magdst = magdst(Rect(0, 0, magdst.cols & -2, magdst.rows & -2));
    int cx = magdst.cols/2;
    int cy = magdst.rows/2;
    Mat q0(magdst, Rect(0, 0, cx, cy));   
    Mat q1(magdst, Rect(cx, 0, cx, cy));  
    Mat q2(magdst, Rect(0, cy, cx, cy));  
    Mat q3(magdst, Rect(cx, cy, cx, cy));
     Mat tmp;                           
    q0.copyTo(tmp);
    q3.copyTo(q0);
    tmp.copyTo(q3);

    q1.copyTo(tmp);                    
    q2.copyTo(q1);
    tmp.copyTo(q2);

    normalize(magdst, magdst, 0, 1, CV_MINMAX); 
    imshow("spectrum magnitude", magdst);
    }


int main( int argc, char** argv )
{
    src = imread("C:/Users/Saha/Desktop/DSCF0563.JPG");
    resize(src, src, Size(), 0.2, 0.2, INTER_LANCZOS4);

  if( !src.data )
  { return -1; }

 dst.create( src.size(), src.type() );

 cvtColor( src, src_gray, CV_BGR2GRAY );

 namedWindow( window_name, CV_WINDOW_AUTOSIZE );

   createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );

    CannyThreshold(0, 0);

    waitKey(0);

  return 0;
  }
edit retag flag offensive close merge delete

Comments

Hello, I have an image I applied the fourier transform for the orientation angle,but i face here channels problem.When i build this program,it shows success but it's not run.It is showing "Open cv Error: Assertion failed <_tp>::channels==m.channels<>>in unknown function, file c:\opencv\opencv\build\include\opencv2/core/mat.hpp,line890" i used Breakpoint in this code when it comes this line "Mat planes[] = {Mat_(padded), Mat::zeros(padded.size(), CV_32F)};"then it break.Personally I do not know how i solve this problem because I have no error! Please help me

rajib86 gravatar imagerajib86 ( 2014-05-15 18:58:29 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
2

answered 2015-03-28 05:11:04 -0600

The error you got was due to the fact that you worked with src image, which is a multi channels image. If you change the line:

src.copyTo( dst, detected_edges);

to:

src_gray.copyTo( dst, detected_edges);

the error gone. In case you wish to do the dft with a multi channels image, you have to proceed with each channel individually, and merge the results to get what you want. Plus, you should check if the image was opened before carrying out any operation on it:

if( !src.data )
{
     printf("Can not read input picture");
     return -1;
}
resize(src, src, Size(), 0.2, 0.2, INTER_LANCZOS4);
edit flag offensive delete link more

Comments

So now the OP has a DFT that works (as I do), how do you find the angle from the result?

ClintFromVa gravatar imageClintFromVa ( 2015-03-30 12:52:04 -0600 )edit
1

For computing the orientation part, right after the split statement:

split(complexdst, planes);

add the following command:

phase(planes[0], planes[1], phasePart);

Then, the other needed code is the same as you did with the magnitude part. Additionally, do not overwrite the planes[0] component as you wrote:

 magnitude(planes[0], planes[1], planes[0]);

Instead, you should do like that:

magnitude(planes[0], planes[1], magPart);
tuannhtn gravatar imagetuannhtn ( 2015-03-31 00:02:39 -0600 )edit

OK, pulled out phasePart, but it is still just snow (see above). I am thinking of doing a polar histogram on just magnitude of one quadrant, the biggest pile will be in the direction I care about (maybe).

ClintFromVa gravatar imageClintFromVa ( 2015-03-31 16:01:01 -0600 )edit
2

answered 2015-03-26 13:54:20 -0600

ClintFromVa gravatar image

updated 2015-03-31 15:52:16 -0600

Well, the Assertion is saying that the number of channels is wrong. Best guess is that you still have color when you need to go either grey (or extract one of the BGR components). I thought the imread should have handled that but the flag to convert to grey is not explicit. Try src = imread("Filename", 0); //0 for grey, 1 for color.

The step you reference (line numbers please!) is where the complex dimension gets added to the image data. It looks like you copied the tutorial verbatim. Is padded bigger than I at this point as it should be?

Otherwise, I'm just trying to read through the tutorial. It looks like you copied that anyway, why not run that with your image? Then let me know how you get the angle!

EDIT

I think the spirit of the original question was how to interpret results like these:

image description

EDIT Pulling out phase as suggested. Similar quadrant swap but not log scale of course.

image description

edit flag offensive delete link more
0

answered 2018-08-21 04:11:19 -0600

enxdtw gravatar image

updated 2018-08-21 04:14:09 -0600

The best way is to create a orientated band pass filter and filter the image with it. I made an online app where you can play around with FFT filters called 'Fourifier'

http://www.ejectamenta.com/Imaging-Ex...

This approach is also used with Steerable Filters, see the work of Freeman, Adelson, Castleman for example

http://people.csail.mit.edu/billf/pub...

http://www.tka4.org/materials/lib/Art...

You can also get the orientations directly from a steerable filter, look at page 17 of

http://persci.mit.edu/pub_pdfs/freema...

This is what I did with them, before I ran out of postdoc funding!

http://www.ejectamenta.com/Imaging-Ex...

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-05-15 18:41:58 -0600

Seen: 2,593 times

Last updated: Aug 21 '18