简体   繁体   中英

Android Shape Recognition on Screen

我想识别在屏幕上绘制的圆形,三角形和矩形等形状。我的主要目的是用户在屏幕上绘制形状,我需要一个代码来识别该形状。我该如何解决这个问题?

What you are trying to achieve can be quite tricky, but I happened to implement something similar a while ago, and here is the approach that I used:

  • stick to black & white drawings
  • have a smallish database of (black & white) drawings (50 or so) with a fixed resolution, let's say 256x256 (you can store them in sqlite as binary blobs if you wish). Make sure that you use decently thick lines for these drawings (10 px should be OK, or something about twice as thick as the user's input drawing). Also, the drawings should be normalized, meaning that they must have at least one of their dimensions as large as the image itself.
  • extract the shape drawn by the user and process it:

a) if it has an aspect ratio close to a square, then simply crop the white space around it and enlarge it such that it has the same size as your database images

b) Otherwise, it will most likely have one dimension about two times larger than the other one, in which case you crop the white space, rotate it to have the height as it's biggest dimension, enlarge it to 256x128 and then add on both sides 64 px of white space.

  • you'll have to compare your drawing with each of your database images pixel by pixel and determine the amount of black pixels which overlap for each database image. Then you sort these numbers and you'll get the best match. Even if the best match has less than 20% overlapping pixels, the results are usually good.
  • Because some shapes can be considered the same, even if they are rotated (imagine various ways to place a triangle in an image: one tip pointing up, or down, or towards one side etc), you'll probably want to rotate your input drawing around 12 - 24 times (by 15 - 30 degrees at each step) and compare each rotation to every image in your database. Given that this step will most likely require a lot of processing power, you might consider storing all the rotations of your initial database drawings in the database, as different pictures, thus making the database bigger, but saving you the effort of rotating the input image, which is costly.

Given that the above algorithm is a bit of a resource hog, you might consider having a server somewhere, which can do the actual comparisons, especially if you want to add many images to your database. Since I already implemented this algorithm for a demo application, I can already tell you that you're going to have to do a lot of pixel operations. Also, rotating images with the Android SDK can be annoying, because it changes the image dimensions...

If you are feeling adventurous, here are a couple of papers describing state of the art algorithms for tackling this problem: " Shape contexts enable efficient retrieval of similar shapes " by Greg Mori, Serge Belongie and Jitendra Malik (2001) and " Shape Matching: Similarity Measures and Algorithms " by Remco C. Veltkamp (2001). The maths might be a bit heavy, though.

You should look into GestureOverlayView. A good tutorial is: http://www.vogella.com/articles/AndroidGestures/article.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM