Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I am not sure this is the exact answer for you question but may be helpful. If you consider an image and you want to find the angle at any point from 0,0 you can consider the image as third quadrant and you will get angle in the range of -90 to -179.99 using atan2(). And if your reference is centre of the image the upper quadrants will be in the range of 0 to 180 and the lower quadrants gives approximately -.1 to -179.99. The below code may give you the idea about the arc tangent of any point in the given image. You can use mouse click to point out the co-ordinates.

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
using namespace std;

CvPoint p1={0,0};
IplImage *frame, *src;
CvFont font;
CvPoint cent={0,0} ;//Change to width/2,height/2 for calculating tangent from center of image

double angle=0;
char name[20];
long double distance_of_object=4.5; //metere

void mouseHandler(int event, int x, int y, int flags, void *param)
  {
    switch(event) {
    /* left button down */
    case CV_EVENT_LBUTTONDOWN:

            p1.x=x;
            p1.y=y;

         cvCopy(frame,src,NULL);
         cvLine(src, p1, cent,CV_RGB(0,0,255),1, 8 );
         angle = atan2(cent.y - p1.y,cent.x - p1.x) * 180.0 / CV_PI;

         sprintf(name,"Angle  = %f degree",angle);
         cvPutText(src,name, cvPoint(15, 130), &font, cvScalar(0,255,0));
         cvShowImage("out", src);
         break;


  }

}


int main(int argc, char** argv)
{
  cvInitFont(&font, CV_FONT_HERSHEY_DUPLEX, .9, .9, 0,2, CV_AA);
  frame=cvLoadImage("image.jpeg");
  src=cvCreateImage(cvGetSize(frame),8,3);
  cvCopy(frame,src,NULL);
  cvNamedWindow("out", 0);
  cvShowImage("out", src);
  cvSetMouseCallback( "out", mouseHandler, NULL );
  cvWaitKey(0);
  return 0;
 }

Hope these Helpful.....

I am not sure this is the exact answer for you question but may be helpful. If you consider an image and you want to find the angle at any point from 0,0 you can consider the image as third quadrant and you will get angle in the range of -90 to -179.99 using atan2(). And if your reference is centre of the image the upper quadrants will be in the range of 0 to 180 and the lower quadrants gives approximately -.1 to -179.99. The below code may give you the idea about the arc tangent of any point in the given image. You can use mouse click to point out the co-ordinates.

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
 using namespace std;

CvPoint p1={0,0};
IplImage *frame, *src;
CvFont font;
CvPoint cent={0,0} ;//Change to width/2,height/2 using namespace cv;

Point p1;
Mat frame, src;
Point cent = Point(300,300);// for calculating tangent from center of image
image    
double angle=0;
char name[20];
long double distance_of_object=4.5; //metere

void mouseHandler(int event, int x, int y, int flags, void *param)
  {
    switch(event) {
    /* left button down */
    case CV_EVENT_LBUTTONDOWN:

            p1.x=x;
            p1.y=y;

         cvCopy(frame,src,NULL);
         cvLine(src, frame.copyTo(src);
         line(src, p1, cent,CV_RGB(0,0,255),1, cent,Scalar(255,0,0),2, 8 );
         angle = atan2(cent.y - p1.y,cent.x - p1.x) * 180.0 / CV_PI;

         sprintf(name,"Angle  = %f degree",angle);
         cvPutText(src,name, cvPoint(15, 130), &font, cvScalar(0,255,0));
         cvShowImage("out", putText(src,name, Point(30,30), FONT_HERSHEY_SIMPLEX,1, Scalar(0,255,0),2);
         imshow("out", src);
         break;
   }
 }
 
int main(int argc, char** argv)
{
  cvInitFont(&font, CV_FONT_HERSHEY_DUPLEX, .9, .9, 0,2, CV_AA);
  frame=cvLoadImage("image.jpeg");
  src=cvCreateImage(cvGetSize(frame),8,3);
  cvCopy(frame,src,NULL);
  cvNamedWindow("out", frame= Mat(600,600,CV_8UC3);
  frame.copyTo(src);
  namedWindow("out", 0);
  cvShowImage("out", imshow("out", src);
  cvSetMouseCallback( setMouseCallback( "out", mouseHandler, NULL );
  cvWaitKey(0);
waitKey(0);
  return 0;
 }

Hope these Helpful.....