title: Opencv: how to calculate the degrees(angles) of an object with its coordinates
body: <p>I am currently working on a project that combines motion detection with movement of a toy-gun in the (x,y) axes. I have managed to get the coordinates of the object's position (with the OpenCV proper commands) and sent them to Arduino in order to move the gun,but this did not work correctly. The code I am using is this:</p>
  
  <pre><code>#include &lt;Servo.h&gt;
  
  #define servomaxx 114//max degree servo horizontal (x) can turn
  #define servomaxy 120//max degree servo vertical (y) can turn
  #define screenmaxx 480 //max screen horizontal (x) resolution
  #define screenmaxy 320 //max screen vertical (y) resolution
  #define servocenterx 90 //center position on (x)
  #define servocentery 90 //center position on (y)
  #define servopinx 9 //digital pin for servo x
  #define servopiny 10 //digital pin for servo y
  #define baudrate 9600 //com port speed. 
  #define servorange 180
  
  int x100byte;
  int x010byte;
  int x001byte;
  
  int posx_taken=0;
  int posy_taken=0;
  int posx = 0;
  int posy = 0;
  
  Servo servox;
  Servo servoy;
  
  
  void setup()
  {
      Serial.begin(9600);     
      pinMode(servopinx,OUTPUT);    
      pinMode(servopiny,OUTPUT);    
      pinMode(13, OUTPUT);
  
      servoy.attach(servopiny);
      servox.attach(servopinx);
  
      servox.write(servocenterx);
      delay(200);
      servoy.write(servocentery);
      delay(200);
  }
  
  void loop()
  {
      if (Serial.available() &gt; 3) {
          x100byte = Serial.parseInt();
          delay(5);
          x010byte = Serial.parseInt();
          x001byte = Serial.parseInt();
  
          Serial.println("x100byte");
          Serial.println(x100byte);
          Serial.println("x010byte");
          Serial.println(x010byte);
  
          posx_taken=x100byte;
          posy_taken=x010byte;
  
          if ((posx_taken&gt;screenmaxx) || (posy_taken&gt;screenmaxy)) {
              Serial.println("Wrong number-too big values!");
          } else {
              if (posx_taken == screenmaxx) {
                  posx = servomaxx;
              } else if (posx_taken == 0) {
                  posx = 68;
              } else if (posx_taken == (screenmaxx / 2)) {
                  posx=servorange / 2;
              } else {
                  posx = ((posx_taken * 24) / (screenmaxx / 2)) + 66;
              }
  
              if (posy_taken == screenmaxy) {
                posy = 120;
              } else if (posy_taken == 0) {
                posy = 60;
              } else if (posy_taken == (screenmaxy / 2)){
                posy = 90;
              } else {
                posy = ((posy_taken * 30) / (screenmaxy / 2)) + 60;
              }
          }
  
          servox.write(posx);
          servoy.write(posy);  
  
          if (x001byte == 180) {
              digitalWrite(13, HIGH);  
              delay(1000);             
              digitalWrite(13, LOW); 
          }
      }
  }
  </code></pre>
  
  <p>I have calculated the min and max degree of the vertical and horizontal movement of each servo, according to the horizontal and vertical camera angles (45o V, 58o H). In this case, the horizontal servo's (x) min value is 66 degrees and the max value is 112 degrees. Also, the vertical servo's (y)  min value is 60 degrees and the max value is 120 degrees.</p>
  
  <p>At this point I would like to ask if there is a formula which converts the object's coordinates to degrees based on the camera's angle. The code above does not turn the gun at the correct point. Could anyone please help me?</p>
  
  <p>I am using both an ASUS Xtion Pro Live camera(45o V, 58o H, 70o D) and a USB camera. </p>