'How to detect different types of arrows in image?

Is there a Contour Method to detect arrows in Python CV? Maybe with Contours, Shapes, and Vertices.

enter image description here

# find contours in the thresholded image and initialize the shape detector
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
perimeterValue = cv2.arcLength(cnts , True)
vertices = cv2.approxPolyDP(cnts , 0.04 * perimeterValue, True)

Perhaps we can look at tips of the contours, and also detect triangles?

Hopefully it can detect arrows among different objects, among squares, rectangles, and circles. (otherwise, will have to use machine learning). Also nice to get these three results if possible (arrow length, thickness, directionAngle)

This question recommends template matching, and doesn't specify any code base. Looking for something workable that can be code created

how to detect arrows using open cv python?

If PythonOpenCV doesn't have capability, open to utilizing another library.



Solution 1:[1]

The solution you are asking for is too complex to be solved by one function or particular algorithm. In fact, the problem could be broken down into smaller steps, each with their own algorithms and solutions. Instead of offering you a free, complete, copy-paste solution, I'll give you a general outline of the problem and post part of the solution I'd design. These are the steps I propose:

  1. Identify and extract all the arrow blobs from the image, and process them one by one.

  2. Try to find the end-points of the arrow. That is end and starting point (or "tail" and "tip")

  3. Undo the rotation, so you have straightened arrows always, no matter their angle.

  4. After this, the arrows will always point to one direction. This normalization let's itself easily for classification.

After processing, you can pass the image to a Knn classifier, a Support Vector Machine or even (if you are willing to call the "big guns" on this problem) a CNN (in which case, you probably won't need to undo the rotation - as long as you have enough training samples). You don't even have to compute features, as passing the raw image to a SVM would be probably enough. However, you need more than one training sample for each arrow class.

Alright, let's see. First, let's extract each arrow from the input. This is done using cv2.findCountours, this part is very straightforward:

# Imports:
import cv2
import math
import numpy as np

# image path
path = "D://opencvImages//"
fileName = "arrows.png"

# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)

# Grayscale conversion:
grayscaleImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
grayscaleImage = 255 - grayscaleImage

# Find the big contours/blobs on the binary image:
contours, hierarchy = cv2.findContours(grayscaleImage, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

Now, let's check out the contours and process them one by one. Let's compute a (non-rotated) bounding box of the arrow and crop that sub-image. Now, note that some noise could come up. In which case, we won't be processing that blob. I apply an area filter to bypass blobs of small area. Like this:

# Process each contour 1-1:
for i, c in enumerate(contours):

    # Approximate the contour to a polygon:
    contoursPoly = cv2.approxPolyDP(c, 3, True)

    # Convert the polygon to a bounding rectangle:
    boundRect = cv2.boundingRect(contoursPoly)

    # Get the bounding rect's data:
    rectX = boundRect[0]
    rectY = boundRect[1]
    rectWidth = boundRect[2]
    rectHeight = boundRect[3]

    # Get the rect's area:
    rectArea = rectWidth * rectHeight

    minBlobArea = 100

We set a minBlobArea and process that contour. Crop the image if the contour is above that area threshold value:

        # Check if blob is above min area:
        if rectArea > minBlobArea:

            # Crop the roi:
            croppedImg = grayscaleImage[rectY:rectY + rectHeight, rectX:rectX + rectWidth]

            # Extend the borders for the skeleton:
            borderSize = 5        
            croppedImg = cv2.copyMakeBorder(croppedImg, borderSize, borderSize, borderSize, borderSize, cv2.BORDER_CONSTANT)

            # Store a deep copy of the crop for results:
            grayscaleImageCopy = cv2.cvtColor(croppedImg, cv2.COLOR_GRAY2BGR)

            # Compute the skeleton:
            skeleton = cv2.ximgproc.thinning(croppedImg, None, 1)

There are some couple of things going on here. After I crop the ROI of the current arrow, I extend borders on that image. I store a deep-copy of this image for further processing and, lastly, I compute the skeleton. The border-extending is done prior to skeletonizing because the algorithm produces artifacts if the contour is too close to the image limits. Padding the image in all directions prevents these artifacts. The skeleton is needed for the way I'm finding ending and starting points of the arrow. More of this latter, this is the first arrow cropped and padded:

This is the skeleton:

Note that the "thickness" of the contour is normalized to 1 pixel. That's cool, because that's what I need for the following processing step: Finding start/ending points. This is done by applying a convolution with a kernel designed to identify one-pixel wide end-points on a binary image. Refer to this post for the specifics. We will prepare the kernel and use cv2.filter2d to get the convolution:

            # Threshold the image so that white pixels get a value of 0 and
            # black pixels a value of 10:
            _, binaryImage = cv2.threshold(skeleton, 128, 10, cv2.THRESH_BINARY)

            # Set the end-points kernel:
            h = np.array([[1, 1, 1],
                          [1, 10, 1],
                          [1, 1, 1]])

            # Convolve the image with the kernel:
            imgFiltered = cv2.filter2D(binaryImage, -1, h)

            # Extract only the end-points pixels, those with
            # an intensity value of 110:
            binaryImage = np.where(imgFiltered == 110, 255, 0)
            # The above operation converted the image to 32-bit float,
            # convert back to 8-bit uint
            binaryImage = binaryImage.astype(np.uint8)

After the convolution, all end-points have a value of 110. Setting these pixels to 255, while the rest are set to black, yields the following image (after proper conversion):

Those tiny pixels correspond to the "tail" and "tip" of the arrow. Notice there's more than one point per "Arrow section". This is because the end-points of the arrow do not perfectly end in one pixel. In the case of the tip, for example, there will be more end-points than in the tail. This is a characteristic we will exploit latter. Now, pay attention to this. There are multiple end-points but we only need an starting point and an ending point. I'm gonna use K-Means to group the points in two clusters.

Using K-means will also let me identify which end-points belong to the tail and which to the tip, so I'll always know the direction of the arrow. Let's roll:

            # Find the X, Y location of all the end-points
            # pixels:
            Y, X = binaryImage.nonzero()

            # Check if I got points on my arrays:
            if len(X) > 0 or len(Y) > 0:

                # Reshape the arrays for K-means
                Y = Y.reshape(-1,1)
                X = X.reshape(-1,1)
                Z = np.hstack((X, Y))

                # K-means operates on 32-bit float data:
                floatPoints = np.float32(Z)

                # Set the convergence criteria and call K-means:
                criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
                _, label, center = cv2.kmeans(floatPoints, 2, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

Be careful with the data types. If I print the label and center matrices, I get this (for the first arrow):

Center:
[[  6.  102. ]
 [104.   20.5]]

Labels:
[[1]
 [1]
 [0]]

center tells me the center (x,y) of each cluster – That is the two points I was originally looking for. label tells me on which cluster the original data falls in. As you see, there were originally 3 points. 2 of those points (the points belonging to the tip of the arrow) area assigned to cluster 1, while the remaining end-point (the arrow tail) is assigned to cluster 0. In the centers matrix the centers are ordered by cluster number. That is – first center is that one of cluster 0, while second cluster is the center of cluster 1. Using this info I can easily look for the cluster that groups the majority of points - that will be the tip of the arrow, while the remaining will be the tail:

                # Set the cluster count, find the points belonging
                # to cluster 0 and cluster 1:
                cluster1Count = np.count_nonzero(label)
                cluster0Count = np.shape(label)[0] - cluster1Count

                # Look for the cluster of max number of points
                # That cluster will be the tip of the arrow:
                maxCluster = 0
                if cluster1Count > cluster0Count:
                    maxCluster = 1

                # Check out the centers of each cluster:
                matRows, matCols = center.shape
                # We need at least 2 points for this operation:
                if matCols >= 2:
                    # Store the ordered end-points here:
                    orderedPoints = [None] * 2
                    # Let's identify and draw the two end-points
                    # of the arrow:
                    for b in range(matRows):
                        # Get cluster center:
                        pointX = int(center[b][0])
                        pointY = int(center[b][1])
                        # Get the "tip"
                        if b == maxCluster:
                            color = (0, 0, 255)
                            orderedPoints[0] = (pointX, pointY)
                        # Get the "tail"
                        else:
                            color = (255, 0, 0)
                            orderedPoints[1] = (pointX, pointY)
                        # Draw it:
                        cv2.circle(grayscaleImageCopy, (pointX, pointY), 3, color, -1)
                        cv2.imshow("End Points", grayscaleImageCopy)
                        cv2.waitKey(0)

This is the result; the tip of the end-point of the arrow will always be in red and the end-point for the tail in blue:

Now, we know the direction of the arrow, let's compute the angle. I will measure this angle from 0 to 360. The angle will always be the one between the horizon line and the tip. So, we manually compute the angle:

                        # Store the tip and tail points:
                        p0x = orderedPoints[1][0]
                        p0y = orderedPoints[1][1]
                        p1x = orderedPoints[0][0]
                        p1y = orderedPoints[0][1]
                        # Compute the sides of the triangle:
                        adjacentSide = p1x - p0x
                        oppositeSide = p0y - p1y
                        # Compute the angle alpha:
                        alpha = math.degrees(math.atan(oppositeSide / adjacentSide))

                        # Adjust angle to be in [0,360]:
                        if adjacentSide < 0 < oppositeSide:
                            alpha = 180 + alpha
                        else:
                            if adjacentSide < 0 and oppositeSide < 0:
                                alpha = 270 + alpha
                            else:
                                if adjacentSide > 0 > oppositeSide:
                                    alpha = 360 + alpha

Now you have the angle, and this angle is always measured between the same references. That's cool, we can undo the rotation of the original image like follows:

                        # Deep copy for rotation (if needed):
                        rotatedImg = croppedImg.copy()
                        # Undo rotation while padding output image:
                        rotatedImg = rotateBound(rotatedImg, alpha)
                        cv2. imshow("rotatedImg", rotatedImg)
                        cv2.waitKey(0)

                else:
                    print( "K-Means did not return enough points, skipping..." )
            else:
                 print( "Did not find enough end points on image, skipping..." )

This yields the following result:

The arrow will always point top the right regardless of its original angle. Use this as normalization for a batch of training images, if you want to classify each arrow in its own class. Now, you noticed that I used a function to rotate the image: rotateBound. This function is taken from here. This functions correctly pads the image after rotation, so you do not end up with a rotated image that is cropped incorrectly.

This is the definition and implementation of rotateBound:

def rotateBound(image, angle):
    # grab the dimensions of the image and then determine the
    # center
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)
    # grab the rotation matrix (applying the negative of the
    # angle to rotate clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])
    # compute the new bounding dimensions of the image
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))
    # adjust the rotation matrix to take into account translation
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY
    # perform the actual rotation and return the image
    return cv2.warpAffine(image, M, (nW, nH))

These are results for the rest of your arrows. The tip (always in red), the tail (always in blue) and their "projective normalization" - always pointing to the right:


What remains is collect samples of your different arrow classes, set up a classifier, train it with your samples and test it with the straightened image coming from the last processing block we examined.

Some remarks: Some arrows, like the one that is not filled, failed the end-point identification part, thus, not yielding enough points for clustering. That arrow is by-passed by the algorithm. The problem is tougher than initially though, right? I recommend doing some research on the topic, because not matter how "easy" the task seems, at the end, it will be performed by an automated "smart" system. And those systems aren't really that smart at the end of the day.

Solution 2:[2]

Here is an approach with cv2.connectedComponentsWithStats. After extracting every arrow individually, I am getting the farthest points on the arrow. The distance between these points give me (more or less) the length of the arrow. Also, I am calculating the angle of the arrow by using these two points, i.e., slope between two points. Finally, in order to find the thickness, I am drawing a straight line between these points. And, I am calculating the shortest distance of each pixel of the arrow to the line. The most repeated distance value should give me the thickness of arrow.

The algorithm is not perfect, as it is. Especially, if the arrow is tilted. But, I feel like it is a good starting point and you can improve it.

import cv2
import numpy as np
import matplotlib.pyplot as plt

from scipy.spatial import distance
import math



img = cv2.imread('arrows.png',0)

_,img = cv2.threshold(img,10,255,cv2.THRESH_BINARY_INV)

labels, stats = cv2.connectedComponentsWithStats(img, 8)[1:3]



for label in np.unique(labels)[1:]:

    arrow = labels==label

    indices = np.transpose(np.nonzero(arrow)) #y,x

    dist = distance.cdist(indices, indices, 'euclidean')


    far_points_index = np.unravel_index(np.argmax(dist), dist.shape) #y,x


    far_point_1 = indices[far_points_index[0],:] # y,x
    far_point_2 = indices[far_points_index[1],:] # y,x


    ### Slope
    arrow_slope = (far_point_2[0]-far_point_1[0])/(far_point_2[1]-far_point_1[1])  
    arrow_angle = math.degrees(math.atan(arrow_slope))

    ### Length
    arrow_length = distance.cdist(far_point_1.reshape(1,2), far_point_2.reshape(1,2), 'euclidean')[0][0]


    ### Thickness
    x = np.linspace(far_point_1[1], far_point_2[1], 20)
    y = np.linspace(far_point_1[0], far_point_2[0], 20)
    line = np.array([[yy,xx] for yy,xx in zip(y,x)])
    thickness_dist = np.amin(distance.cdist(line, indices, 'euclidean'),axis=0).flatten()

    n, bins, patches = plt.hist(thickness_dist,bins=150)

    thickness = 2*bins[np.argmax(n)]

    print(f"Thickness: {thickness}")
    print(f"Angle: {arrow_angle}")
    print(f"Length: {arrow_length}\n")
    plt.figure()
    plt.imshow(arrow,cmap='gray')
    plt.scatter(far_point_1[1],far_point_1[0],c='r',s=10)
    plt.scatter(far_point_2[1],far_point_2[0],c='r',s=10)
    plt.scatter(line[:,1],line[:,0],c='b',s=10)
    plt.show()

arrow_1

  • Thickness: 4.309328382835436
  • Angle: 58.94059117029002
  • Length: 102.7277956543408

arrow_2

  • Thickness: 7.851144897915465
  • Angle: -3.366460663429801
  • Length: 187.32325002519042

arrow_3

  • Thickness: 2.246710258748367
  • Angle: 55.51004336926862
  • Length: 158.93709447451215

arrow_4

  • Thickness: 25.060450615293227
  • Angle: -37.184706453233126
  • Length: 145.60219778561037

Solution 3:[3]

As your main concern is to filter out arrows from different shapes. I have implemented a method using convexityDefects. you can read more about convexity defects here.

Also, I have added more arrow inside other shapes to demonstrate the robustness of the method.

Updated Image

Updated Image


Method to filter arrows from image using convexity defects.

def get_filter_arrow_image(threslold_image):
    blank_image = np.zeros_like(threslold_image)

    # dilate image to remove self-intersections error
    kernel_dilate = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
    threslold_image = cv2.dilate(threslold_image, kernel_dilate, iterations=1)

    contours, hierarchy = cv2.findContours(threslold_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    if hierarchy is not None:

        threshold_distnace = 1000

        for cnt in contours:
            hull = cv2.convexHull(cnt, returnPoints=False)
            defects = cv2.convexityDefects(cnt, hull)

            if defects is not None:
                for i in range(defects.shape[0]):
                    start_index, end_index, farthest_index, distance = defects[i, 0]

                    # you can add more filteration based on this start, end and far point
                    # start = tuple(cnt[start_index][0])
                    # end = tuple(cnt[end_index][0])
                    # far = tuple(cnt[farthest_index][0])

                    if distance > threshold_distnace:
                        cv2.drawContours(blank_image, [cnt], -1, 255, -1)

        return blank_image
    else:
        return None

filter arrow image

Filterd Image


I have added methods for the angle and length of the arrow, If this isn't good enough, let me know; there are more complicated methods for angle detection based on 3 coordinate points.
def get_max_distace_point(cnt):
    max_distance = 0
    max_points = None
    for [[x1, y1]] in cnt:
        for [[x2, y2]] in cnt:
            distance = get_length((x1, y1), (x2, y2))

            if distance > max_distance:
                max_distance = distance
                max_points = [(x1, y1), (x2, y2)]

    return max_points


def angle_beween_points(a, b):
    arrow_slope = (a[0] - b[0]) / (a[1] - b[1])
    arrow_angle = math.degrees(math.atan(arrow_slope))
    return arrow_angle

def get_arrow_info(arrow_image):
    arrow_info_image = cv2.cvtColor(arrow_image.copy(), cv2.COLOR_GRAY2BGR)
    contours, hierarchy = cv2.findContours(arrow_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    arrow_info = []
    if hierarchy is not None:

        for cnt in contours:
            # draw single arrow on blank image
            blank_image = np.zeros_like(arrow_image)
            cv2.drawContours(blank_image, [cnt], -1, 255, -1)

            point1, point2 = get_max_distace_point(cnt)

            angle = angle_beween_points(point1, point2)
            lenght = get_length(point1, point2)

            cv2.line(arrow_info_image, point1, point2, (0, 255, 255), 1)

            cv2.circle(arrow_info_image, point1, 2, (255, 0, 0), 3)
            cv2.circle(arrow_info_image, point2, 2, (255, 0, 0), 3)

            cv2.putText(arrow_info_image, "angle : {0:0.2f}".format(angle),
                        point2, cv2.FONT_HERSHEY_PLAIN, 0.8, (0, 0, 255), 1)
            cv2.putText(arrow_info_image, "lenght : {0:0.2f}".format(lenght),
                        (point2[0], point2[1]+20), cv2.FONT_HERSHEY_PLAIN, 0.8, (0, 0, 255), 1)

        return arrow_info_image, arrow_info
    else:
        return None, None

angle and length image

angle and length image

CODE

import math
import cv2
import numpy as np


def get_filter_arrow_image(threslold_image):
    blank_image = np.zeros_like(threslold_image)

    # dilate image to remove self-intersections error
    kernel_dilate = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
    threslold_image = cv2.dilate(threslold_image, kernel_dilate, iterations=1)

    contours, hierarchy = cv2.findContours(threslold_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    if hierarchy is not None:

        threshold_distnace = 1000

        for cnt in contours:
            hull = cv2.convexHull(cnt, returnPoints=False)
            defects = cv2.convexityDefects(cnt, hull)

            if defects is not None:
                for i in range(defects.shape[0]):
                    start_index, end_index, farthest_index, distance = defects[i, 0]

                    # you can add more filteration based on this start, end and far point
                    # start = tuple(cnt[start_index][0])
                    # end = tuple(cnt[end_index][0])
                    # far = tuple(cnt[farthest_index][0])

                    if distance > threshold_distnace:
                        cv2.drawContours(blank_image, [cnt], -1, 255, -1)

        return blank_image
    else:
        return None


def get_length(p1, p2):
    line_length = ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5
    return line_length


def get_max_distace_point(cnt):
    max_distance = 0
    max_points = None
    for [[x1, y1]] in cnt:
        for [[x2, y2]] in cnt:
            distance = get_length((x1, y1), (x2, y2))

            if distance > max_distance:
                max_distance = distance
                max_points = [(x1, y1), (x2, y2)]

    return max_points


def angle_beween_points(a, b):
    arrow_slope = (a[0] - b[0]) / (a[1] - b[1])
    arrow_angle = math.degrees(math.atan(arrow_slope))
    return arrow_angle


def get_arrow_info(arrow_image):
    arrow_info_image = cv2.cvtColor(arrow_image.copy(), cv2.COLOR_GRAY2BGR)
    contours, hierarchy = cv2.findContours(arrow_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    arrow_info = []
    if hierarchy is not None:

        for cnt in contours:
            # draw single arrow on blank image
            blank_image = np.zeros_like(arrow_image)
            cv2.drawContours(blank_image, [cnt], -1, 255, -1)

            point1, point2 = get_max_distace_point(cnt)

            angle = angle_beween_points(point1, point2)
            lenght = get_length(point1, point2)

            cv2.line(arrow_info_image, point1, point2, (0, 255, 255), 1)

            cv2.circle(arrow_info_image, point1, 2, (255, 0, 0), 3)
            cv2.circle(arrow_info_image, point2, 2, (255, 0, 0), 3)

            cv2.putText(arrow_info_image, "angle : {0:0.2f}".format(angle),
                        point2, cv2.FONT_HERSHEY_PLAIN, 0.8, (0, 0, 255), 1)
            cv2.putText(arrow_info_image, "lenght : {0:0.2f}".format(lenght),
                        (point2[0], point2[1] + 20), cv2.FONT_HERSHEY_PLAIN, 0.8, (0, 0, 255), 1)

        return arrow_info_image, arrow_info
    else:
        return None, None


if __name__ == "__main__":
    image = cv2.imread("image2.png")

    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, thresh_image = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY_INV)
    cv2.imshow("thresh_image", thresh_image)

    arrow_image = get_filter_arrow_image(thresh_image)
    if arrow_image is not None:
        cv2.imshow("arrow_image", arrow_image)
        cv2.imwrite("arrow_image.png", arrow_image)

        arrow_info_image, arrow_info = get_arrow_info(arrow_image)
        cv2.imshow("arrow_info_image", arrow_info_image)
        cv2.imwrite("arrow_info_image.png", arrow_info_image)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

convexity defects on a thin arrow.

  • Blue point - start point of defect
  • Green point - far point if defect
  • Red point - end point of defect
  • yellow line = defect line from start point to end point.
image defect-1 defect-2 and so on...
enter image description here enter image description here enter image description here ..

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Prefect
Solution 3