How can I find the angle of a T shape in OpenCV
<p>I'm using OpenCV 2.4 to do some tracking, and I can get a contour of the shape I want, which is a T.</p>

<p>Input image:
<a href="http://i.stack.imgur.com/iQ6ZW.png" rel="nofollow"><img src="http://i.stack.imgur.com/iQ6ZW.png" alt="enter image description here"></a></p>

<p>I can use <code>cv2.minAreaRect(my_t_contour)</code> and get the angle of that rect, but that only gives me 0-180 degrees. But this is a T shape though, so I want to be able to tell 0-360. I was thinking of:</p>

<ol>
<li>Split the contour into two rects</li>
<li>Get a line through the rects (either using skeletonize > HoughLinesP)</li>
<li>Determine which line is which, determine their gradient (using the coordinates I get from HoughLinesP) and then determine the direction of the T.</li>
</ol>

<p>But I'm stuck at number 1, how can I split a contour into two shapes?</p>

<hr>

<p><strong>Method 1</strong>: draw center of contour and center of minAreaRect of contour</p>

<pre><code>dst = cv2.cvtColor(r_target, cv2.COLOR_BGR2GRAY)
dst = cv2.GaussianBlur(dst, (11, 11), 0)
ret,dst = cv2.threshold(dst,110,255,cv2.THRESH_BINARY_INV)
cnts = cv2.findContours(dst, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in cnts:
    # get minAreaRect around contour and draw its center in red
    rect = cv2.minAreaRect(c)
    cv2.circle(r_target, (int(rect[0][0]), int(rect[0][1])), 7, (0, 0, 255), -1)

    # get moments of contour to get center and draw it in white
    M = cv2.moments(c)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    cv2.circle(r_target, (cX, cY), 7, (255, 255, 255), -1)
</code></pre>

<p><a href="http://i.stack.imgur.com/Py5kb.png" rel="nofollow"><img src="http://i.stack.imgur.com/Py5kb.png" alt="enter image description here"></a></p>

<p>Next step would probably calculate a simple gradient between the centers to determine the angle.</p>

<hr>

<p><strong>Method 2</strong>: skeletonize the image and get lines using HoughLinesP.</p>

<pre><code>dst = cv2.cvtColor(r_target, cv2.COLOR_BGR2GRAY)
dst = cv2.GaussianBlur(dst, (11, 11), 0)
ret,dst = cv2.threshold(dst,110,255,cv2.THRESH_BINARY)
dst = 1 - dst / 255
dst = skimage.morphology.skeletonize(dst).astype(np.uint8)
rho = 1
theta = np.pi / 180
threshold = 1
minLineLength = 30
maxLineGap = 15
lines = cv2.HoughLinesP(dst, rho, theta, threshold, minLineLength=minLineLength, maxLineGap=maxLineGap)
for line in lines[0]:
    cv2.line(r_target, (line[0], line[1]), (line[2], line[3]), (0, 255, 0), 1, 8)
</code></pre>

<p>But the lines don't come out nicely. This is how the skeleton looks like:</p>

<p><a href="http://i.stack.imgur.com/2f9KL.png" rel="nofollow"><img src="http://i.stack.imgur.com/2f9KL.png" alt="enter image description here"></a></p>

<p>I'm still experimenting with the variables but is there a specific thought process around using HoughLinesP?</p>

<p>As a variant you can use PCA, find first component direction, and use it as an searced angle. You can check here for an example: <a href="http://docs.opencv.org/trunk/d1/dee/tutorial_introduction_to_pca.html" rel="nofollow">http://docs.opencv.org/trunk/d1/dee/tutorial_introduction_to_pca.html</a></p>

python opencv