Skip to content Skip to sidebar Skip to footer

How To Find The Direction Of Triangles In An Image Using Opencv

I am trying to find the direction of triangles in an image. below is the image: These triangles are pointing upward/downward/leftward/rightward. This is not the actual image. I ha

Solution 1:

Assuming that you only have four cases: [up, down, left, right], this code should work well for you.

The idea is simple:

  1. Get the bounding rectangle for your contour. Use: box = cv2.boundingRect(contour_pnts)
  2. Crop the image using the bounding rectangle.
  3. Reduce the image vertically and horizontally using the Sum option. Now you have the sum of pixels along each axis. The axis with the largest sum determines whether the triangle base is vertical or horizontal.
  4. To identify whether the triangle is pointing left/right or up/down: you need to check whether the bounding rectangle center is before or after the max col/row:

The code (assumes you start from the cropped image):

ver_reduce = cv2.reduce(img,  0, cv2.REDUCE_SUM, None, cv2.CV_32F)
hor_reduce = cv2.reduce(img,  1, cv2.REDUCE_SUM, None, cv2.CV_32F)

#For smoothing the reduced vector, could be removed
ver_reduce = cv2.GaussianBlur(ver_reduce, (3, 1), 0)
hor_reduce = cv2.GaussianBlur(hor_reduce, (1, 3), 0)

_,ver_max, _, ver_col = cv2.minMaxLoc(ver_reduce)
_,hor_max, _, hor_row = cv2.minMaxLoc(hor_reduce)

ver_col = ver_col[0]
hor_row = hor_row[1]

contour_pnts = cv2.findNonZero(img) #in my code I do not have the original contour points

rect_center, size,  angle = cv2.minAreaRect(contour_pnts )

print(rect_center)

if ver_max > hor_max:
    if rect_center[0] > ver_col:
        print ('right')
    else:
        print ('left')
else:
    if rect_center[1] > hor_row:
        print ('down')
    else:
        print ('up')

Photos:

Up case

Right case

Down case

Left case

Solution 2:

Well, Mark has mentioned a solution that may not be as efficient but perhaps more accurate. I think this one should be equally efficient but perhaps less accurate. But since you already have a code that finds triangles, try adding the following code after you have found triangle contour:

hull = cv2.convexHull(cnt)    # convex hull of contourhull = cv2.approxPolyDP(hull,0.1*cv2.arcLength(hull,True),True)
# You can double check if the contour is a triangle here# by something like len(hull) == 3

You should get 3 hull points for a triangle, these should be the 3 vertices of your triangles. Given your triangles always 'face' only in 4 directions; Y coordinate of the hull will have close value to the Y coordinate of the centroid for triangle facing left or right and whether it's pointing left or right will depend on whether hull X is less than or greater than centroid X. Similarly use hull and centroid X and Y for triangle pointing up or down.

Post a Comment for "How To Find The Direction Of Triangles In An Image Using Opencv"