# Recognizing Hand-Written Shapes Programmatically: Find the Key Points of a Rectangle

### A walkthrough on using computers to recognize drawn shapes.

· Big Data Zone · Opinion
Save
2.77K Views

a far-fetched goal i have is using sketching on a whiteboard as a way to define programs . i mean formal programs that you can execute. of course through your sketches you would define programs in a high level domain specific language (for example describing a state machine or an entity-relationship diagram).

to do so i would like to start recognizing rectangles. then i will move to recognize other shapes, connecting lines, and recognizing text present in the diagram. for now let’s focus on recognizing rectangles.

my general approach would be the following:

1. recognize the meaningful lines
2. recognize key points among those lines
3. classify those key points using ai
4. find shapes by combining the classified key points

ok. this is not going to be something i complete over a week-end.

## the input images

we will use 3 images: two have them have been drawn on a whiteboard by me, under different light conditions. the third one was found on the internet. it has the particularity that the sketch was done on a graph paper (i.e., there is a grid on the paper).   let’s see how we can process these images. we will use java and the boofcv image processing library.

## gray scale

as first thing we convert the image to gray scale. here we get a problem with the image taken under artificial light:

we want to remove that giant gray blob on bottom right corner. to do so we will use derivatives.

## derivatives

we blur the image, to reduce the effect of noise and calculate the derivates. this is a way to capture the sharp variations of colors which happens vertically or horizontally.

we would got something like this for the image taken under natural light:

however for the image taken under artificial light we see the noise:

at this point we take each point of the image and look to see if there is a high number of points with a high derivative (either horizontal or vertical). we keep the points satisfying the condition and we set all the other points to white. we do that a couple of times.

this is the result:

## contours

we do some additional filtering and then we invoke a function to find the contours inside the image. we draw the external contours in red and the internal ones in blue.

we then remove the short contours:

## key points

the contours we get are drawn as a list of segments which are very short. let’s draw the extremes of the segments in blue.

yes, they are very short: you just see a continuous set of extremes, very close one to each other. we want to get fewer segments and much longer.

to do that we use basically two strategies:

1. we simply merge consecutive extremes, which are very close.
2. we take sequences of three consecutive points: a, b, c. if b is very close to the line between a and c we just remove b.

we apply two times both these strategies and get much simpler contours. this is the final result.

## what next

now we have a reasonable number of relevant points. i want to now proceed to classify them through machine learning techniques. for example i want to recognize single points to be a top left corner of a rectangle or a point as part of an arrow. then i will proceed to combine those recognized points to obtain entire shapes (my rectangles!).

right now i am generating the images to classify and i am thinking about which features to use for machine learning. i have some ideas, but we will see them in one of next posts.

training images looks like this: Topics:
artificial intelligence, image recognition

Published at DZone with permission of Federico Tomassetti, DZone MVB.

Opinions expressed by DZone contributors are their own.