I have changed your image to write my program because i don't need rectangle. only shape object is needed.

Image is

program is based on this paper (appendix)

```
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <fstream>
using namespace cv;
using namespace std;
class MatchDescriptor {
public :
vector<Point2d> sContour;
vector<complex<float> > b;
vector<complex<float> > a;
vector<float> frequence;
vector<float> rho,psi;
double pi;
int nbDesFit;
public :
MatchDescriptor(){nbDesFit=7;pi=acos(-1.0);};;
float AjustementRtSafe(vector<Point2d> &c,float &alphaMin,float &phiMin,float &sMin);
float Ajustement(vector<Point2d> &c,float &alphaMin,float &phiMin,float &sMin);
void falpha(float x,float *fn,float *df);
void InitFrequence();
float rtsafe(float x1,float x2,float xacc);
float Distance(complex<float> r,float alpha)
{
long i;
complex<float> j(0,1);
float d=0;
for (i=1;i<=nbDesFit;i++)
{
d += abs(a[i]-b[i]*r*exp(j*float(alpha*frequence[i])))+ abs(a[a.size()-i]-b[a.size()-i]*r*exp(j*float(alpha*frequence[a.size()-i])));
}
return d;
};
};
void MatchDescriptor::InitFrequence()
{
long i;
int nbElt=sContour.size();
frequence.resize(sContour.size());
for (i=0;i<=nbElt/2;i++)
frequence[i] = 2*pi*(float)i/nbElt;
for (i=nbElt/2+1;i<nbElt;i++)
frequence[i] = 2*pi*(float)(i-nbElt)/nbElt;
}
void MatchDescriptor::falpha(float x,float *fn,float *df)
{
long n,nbElt = sContour.size();
float s1=0,s2=0,s3=0,s4=0;
float ds1=0,ds2=0,ds3=0,ds4=0;
for (n=1;n<=nbDesFit;n++)
{
s1 += rho[n] * sin(psi[n]+frequence[n]*x) +
rho[nbElt-n] * sin(psi[nbElt-n]+frequence[nbElt-n]*x);
s2 += frequence[n] * rho[n] * cos(psi[n]+frequence[n]*x) +
frequence[nbElt-n] * rho[nbElt-n] * cos(psi[nbElt-n]+frequence[nbElt-n]*x);
s3 += rho[n] * cos(psi[n]+frequence[n]*x) +
rho[nbElt-n] * cos(psi[nbElt-n]+frequence[nbElt-n]*x);
s4 += frequence[n] * rho[n] * sin(psi[n]+frequence[n]*x) +
frequence[nbElt-n] * rho[nbElt-n] * sin(psi[nbElt-n]+frequence[nbElt-n]*x);
ds1 += frequence[n]*rho[n] * cos(psi[n]+frequence[n]*x) +
frequence[nbElt-n]*rho[nbElt-n] * cos(psi[nbElt-n]+frequence[nbElt-n]*x);
ds2 += -frequence[n]*frequence[n] * rho[n] * sin(psi[n]+frequence[n]*x) -
frequence[nbElt-n]*frequence[nbElt-n] * rho[nbElt-n] * sin(psi[nbElt-n]+frequence[nbElt-n]*x);
ds3 += -frequence[n]*rho[n] * sin(psi[n]+frequence[n]*x) -
frequence[nbElt-n]*rho[nbElt-n] * sin(psi[nbElt-n]+frequence[nbElt-n]*x);
ds4 += frequence[n]*frequence[n] * rho[n] * cos(psi[n]+frequence[n]*x) +
frequence[nbElt-n]*frequence[nbElt-n] * rho[nbElt-n] * cos(psi[nbElt-n]+frequence[nbElt-n]*x);
}
*fn = s1 * s2 - s3 *s4;
*df = ds1 * s2 + s1 * ds2 - ds3 * s4 - s3 * ds4;
}
float MatchDescriptor::AjustementRtSafe(vector<Point2d> &c,float &alphaMin,float &phiMin,float &sMin)
{
long n,nbElt = sContour.size();
float s1,s2,sign1,sign2,df,x1=nbElt,x2=nbElt,dx;
float dist,distMin = 10000,alpha,s,phi;
complex<float> j(0,1),zz;
InitFrequence();
rho.resize(nbElt);
psi.resize(nbElt);
b.resize(nbElt ...
```

(more)
What do you mean by rotation? in plan (which one?), in space?

Ok. I am having two images one is template and another is scene which captured from webcam. While i am doing the surf detection it finds the match. but i need to detect the angle of rotation in the scene with respect to the template

So, I am asking you again, what angle do you need: in the plane of the photo?

See the template image and scene image with output . I need

to find the angle based on template.

so, you got the homography matrix, and can apply Rodrigues() on it, to get the euler angles.

but think of it, there will be

3angles involved, not one.