# Text angle from image

**URL:** <https://forum.opencv.org/t/text-angle-from-image/13903>\
**Category:** Uncategorized\
**Created:** [July 17, 2023, 9:44am UTC](https://forum.opencv.org/t/text-angle-from-image/13903 "2023-07-17T09:44:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahesh\_Nagmal](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mahesh_nagmal/32/8310_2.png) [@Mahesh\_Nagmal](https://forum.opencv.org/u/Mahesh_Nagmal)\
**Post date:** [July 17, 2023, 9:44am UTC](https://forum.opencv.org/t/text-angle-from-image/13903/1 "2023-07-17T09:44:17Z")

</div>

Hello,  
I am trying to get the text angle from image using contours, PCACompute2, atan2.  
But I am able to get only specific angle value correct(0 - 270).  
I need detect all the angle values correct.  
Your help would be highly appreciated.  
Code -

```auto
def getOrientation(pts, img):
  ## [pca]
  # Construct a buffer used by the pca analysis
  sz = len(pts)
  data_pts = np.empty((sz, 2), dtype=np.float64)
  for i in range(data_pts.shape[0]):
    data_pts[i,0] = pts[i,0,0]
    data_pts[i,1] = pts[i,0,1]

  # Perform PCA analysis
  mean = np.empty((0))
  mean, eigenvectors, eigenvalues = cv.PCACompute2(data_pts, mean)

  # Store the center of the object
  cntr = (int(mean[0,0]), int(mean[0,1]))
  ## [pca]

  ## [visualization]
  # Draw the principal components
  cv.circle(img, cntr, 3, (255, 0, 255), 2)
  p1 = (cntr[0] + 0.02 * eigenvectors[0,0] * eigenvalues[0,0], cntr[1] + 0.02 * eigenvectors[0,1] * eigenvalues[0,0])
  p2 = (cntr[0] - 0.02 * eigenvectors[1,0] * eigenvalues[1,0], cntr[1] - 0.02 * eigenvectors[1,1] * eigenvalues[1,0])
  drawAxis(img, cntr, p1, (255, 255, 0), 1)
  drawAxis(img, cntr, p2, (0, 0, 255), 5)

  angle = atan2(eigenvectors[0,1], eigenvectors[0,0]) # orientation in radians
  ## [visualization]

  angle = np.rad2deg(angle)

  if angle > 0:
    angle = angle + 90
  else:
    angle = angle - 90

  # Label with the rotation angle
  label = str(int(angle)) + " degrees"
  textbox = cv.rectangle(img, (cntr[0], cntr[1]-25), (cntr[0] + 250, cntr[1] + 10), (255,255,255), -1)
  cv.putText(img, label, (cntr[0], cntr[1]), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1, cv.LINE_AA)

  return angle

```

---

<div class="post-metadata">

**Author:** ![sturkmen](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sturkmen/32/13_2.png) [@sturkmen](https://forum.opencv.org/u/sturkmen)\
**Post date:** [July 17, 2023, 11:58am UTC](https://forum.opencv.org/t/text-angle-from-image/13903/2 "2023-07-17T11:58:22Z")

</div>

see also the sample [https://github.com/opencv/opencv/blob/4.x/samples/python/text\_skewness\_correction.py](https://github.com/opencv/opencv/blob/4.x/samples/python/text_skewness_correction.py)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [July 17, 2023, 12:35pm UTC](https://forum.opencv.org/t/text-angle-from-image/13903/3 "2023-07-17T12:35:44Z")

</div>

> [@Mahesh\_Nagmal](#):
>
> But I am able to get only specific angle value correct(0 - 270).

please explain, also take a close look at [atan2 limitations](https://en.wikipedia.org/wiki/Atan2)

> [@Mahesh\_Nagmal](#):
>
> ```auto
> if angle > 0:
> angle = angle + 90
> else:
> angle = angle - 90
> 
> ```

how do you _know_, that it’s the 2nd/3rd quadrant ? (imo, you can’t)

---

<div class="post-metadata">

**Author:** ![Mahesh\_Nagmal](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mahesh_nagmal/32/8310_2.png) [@Mahesh\_Nagmal](https://forum.opencv.org/u/Mahesh_Nagmal)\
**Post date:** [July 19, 2023, 10:38am UTC](https://forum.opencv.org/t/text-angle-from-image/13903/4 "2023-07-19T10:38:59Z")

</div>

Thanks for your suggestion. I have tried sample examples its giving angle as zero. I think using skewness we can find the image rotated angle. I have tested on few image, getting the proper angle values for 1st, 2nd and 3rd quadrant.
