# Image Processing: Recognizing arrows and their orientation/direction

28 views (last 30 days)
Wiseguy on 18 Mar 2020
Commented: Wiseguy on 1 Apr 2020
Hello, I am trying to write a code that will import an image of an arrow, then tell me what direction it is pointing. The end goal was to eventually mature this into showing a simple small robot with a camera on a track a left or right arrow, and having it move in a given direction. My first problem is deciding how to determine which way it is facing. My first thought was that arrows are usually "heavier" on the side they are pointing, so if i could find the centroid, then show that it was to the right of the midpoint (positive distance), I could say that it was pointing to the right. Conversely if it was a negative distance it was pointing left. So I began with this.
Note: I use %% frequently because I am just beginning with MATLAB and these titles help me remember what each section is doing for me.
%% Clean Workspace
clear all; close all; clc;
%% Import Image
%% Display Image
figure
imshow(arrow)
%% Convert to Greyscale
img1BW = rgb2gray(arrow);
%% Display Greyscale image
figure
imshow(img1BW)
%% Convert to Binary Image
Binaryarrow = imbinarize(img1BW);
%% Display Binary Image
figure
imshow(Binaryarrow)
%% Locate & Plot Centroid
C = regionprops(Binaryarrow,'centroid');
centroids = cat(1, C.Centroid);
imshow(Binaryarrow)
hold on
plot(imgca,centroids(:,1), centroids(:,2), 'b*')
hold off
It was when I got to this point that I realized I am going to have problems. I see now that depending on the geometry, some arrows won't obey my notion that the pointing end would be "heavier: , or contain a larger surface area, negating the effectiveness of my centroid - offset idea. That oversight got me thinking there is probably a much better way to go about this, so I figured I would reach out. Attached is a simple arrow I began with. Left/Right is all I need for now, but if there is a framework that could expand to Up/Down or angles later on that would be a nice stretch goal.

Raynier Suresh on 20 Mar 2020
The regionprops in MATLAB could give you the orientation angle, to use regionprops the input must be a Binary Image or Label Image or Connected Components.
The below code would give you the angle at which the arrow is facing,
Ig = rgb2gray(I); % Convert to gray image
e = edge(Ig,'sobel'); % Use edge detection to find arrow
BW = imfill(e,'hole'); % fill the arrow
K = regionprops(BW,'Orientation'); % find the angle, range of angle is –90 to +90
angle = round(K.Orientation);
if angle<0
angle = 180+ angle; % convert the range from 0 to 180, 0 means Right and 180 is left
end
angle % Display the angle
Referring to the following links might be useful:
Instead of edge detection you could also try image segmentation and based on that you can get the orientation of the arrow using regionprops
Wiseguy on 1 Apr 2020
Ok I see why you added 180 to get a range from 0 to 180, but for my purpose the arrow head IS important, the whole point of the code is to tell me which way the arrow is pointing, left or right, to be expanded to up and down later as well. 45 to -45 is right, 135 to 225 = left, etc..