title: Shape Detection in Java using OpenCV
body: <p>I need to detect shapes and their colours on a taken image. These shapes are: a heart, a rectangle, a star and a circle. Each shape has one of 5 predefined colours. Colour recognition works fine, but shape recognition is a real problem.</p>
  
  <p>After hours and hours of googling, trying and tweaking code, the best I have come up with is the following procedure:</p>
  
  <ul>
  <li><p>First, read the image and convert it to grayscale.</p></li>
  <li><p>Then, apply blur to reduce the noise from the background.
  Medianblur seems to work best here. Normal blur has little effect, and Gaussian blur rounds the edges of the shapes which gives trouble.</p></li>
  <li><p>Next, apply threshhold. 
  AdaptiveThreshold doesn't give the results I expected; the result vary widely with each image. I now apply two thresholds: One uses Otsu's algorithm to filter the light shapes, and the other uses the Binary Inverted value for the darker shapes.</p></li>
  <li><p>Finally, find contours on the two threshholds and merge them in one list.
  By the amount of points found in each contour, I decide which shape is found.</p></li>
  </ul>
  
  <p>I have tried adding Canny, sharpening the image, different threshholds, Convex Hull, Houghes, etc. I probably tried every possible value for each method as well. For now, I can get things working with the above procedure on a few images, but then it fails again on a new image. Either it detects too much points in a contour, or it doesn't detect the shape at all.</p>
  
  <p>I am really stuck and dont know what else to try. One thing I still have to work out is using masks, but I can't find much information on that and don't know if it would make any difference at all.</p>
  
  <p>Any advice is more than welcome. If you would like to see my code, please ask. You can find sample images here: <a href="http://tinypic.com/a/34tbr/1" rel="nofollow">http://tinypic.com/a/34tbr/1</a></p>