 <p>You can use the Circularity algorithm as a first approach, which is very easy to compute:</p> <p>C = p<sup>2</sup>/a where <code>p</code> is the perimeter (border area) and <code>a</code> is shape area.</p> <p>To know how to read/write pixels quickly, take a look <a href="http://www.bobpowell.net/lockingbits.htm" rel="nofollow noreferrer">here</a></p> <p>Alternatively look for shape signature algorithm available at Rafael Gonzales book. In this algorithm you compute the center of the object using central momentum, the you compute the distance between the center and each border pixel. You'll end up with a 1D signal where peaks represent bigger distance from the center. In a square, you have 4 symmetric peaks while in a rectangle 2 big peaks and 2 smaller ones.</p>