DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Recognizing Hand-Written Shapes Programmatically: Find the Key Points of a Rectangle

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

A walkthrough on using computers to recognize drawn shapes.

Federico Tomassetti user avatar by
Federico Tomassetti
·
Apr. 21, 16 · Opinion
Like (4)
Save
Tweet
Share
3.44K Views

Join the DZone community and get the full member experience.

Join For Free

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).

whiteboard (natural light)

whiteboard (artificial light)

graph 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:

screenshot from 2016-04-02 14-51-23

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:

screenshot from 2016-04-02 14-44-43

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

screenshot from 2016-04-02 14-53-42

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:

screenshot from 2016-04-02 14-56-12

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.

screenshot from 2016-04-02 14-58-19

we then remove the short contours:

screenshot from 2016-04-02 14-59-01

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.

screenshot from 2016-04-02 15-00-19

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.

screenshot from 2016-04-02 15-03-35 screenshot from 2016-04-02 15-03-19 screenshot from 2016-04-02 15-05-50




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:

image title


Machine learning Domain-Specific Language Light (web browser) Data Types Convert (command) Papers (software) Graph (Unix) Diagram Java (programming language)

Published at DZone with permission of Federico Tomassetti, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why Does DevOps Recommend Shift-Left Testing Principles?
  • Why Every Fintech Company Needs DevOps
  • Cloud Native London Meetup: 3 Pitfalls Everyone Should Avoid With Cloud Data
  • Automated Performance Testing With ArgoCD and Iter8

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: