title: Detect Circle, rectangle and triangle and counting in Android by Camera with opencv
body: <p>I want to detect circle, rectangle and triangle from the image with OpenCv in Android, then counting. I had converted code from the tutorial in C++ (This website <a href="http://opencv-code.com/tutorials/detecting-simple-shapes-in-an-image/" rel="nofollow">http://opencv-code.com/tutorials/detecting-simple-shapes-in-an-image</a>). The problem is it cannot counting any circle and rectangle(result is 0), but can detect triangle(not exactly all). Please check my code and correct me if i am wrong somewhere. This is my code</p>
  
  <pre><code>int circle = 0;
      int rec = 0;
      int tri = 0;
      int totalCircle = 0;
      int totalRec = 0;
      int totalTri = 0;
      Mat gray = new Mat();
      Mat imgSource = new Mat();
      Mat bw = new Mat();
      Utils.bitmapToMat(bmpOriginal , imgSource);
      //Imgproc.resize(imgSource, imgSource,new Size(310,175));
      Imgproc.cvtColor(imgSource, gray, Imgproc.COLOR_BGR2GRAY);
      Imgproc.Canny(gray, bw,  0, 50, 5,true);
      List&lt;MatOfPoint&gt; contours = new ArrayList&lt;MatOfPoint&gt;();
      Imgproc.findContours(bw.clone(), contours,bw.clone(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
      MatOfPoint2f approx =  new MatOfPoint2f();
      Mat dst = imgSource.clone();
  
      for (int i = 0; i &lt; contours.size(); i++){
          MatOfPoint tempContour=contours.get(i);
          MatOfPoint2f newMat = new MatOfPoint2f( tempContour.toArray() );
          int contourSize = (int)tempContour.total();
          Imgproc.approxPolyDP(newMat, approx, contourSize*0.02, true);
          MatOfPoint points=new MatOfPoint(approx.toArray());
          if((Math.abs(Imgproc.contourArea(tempContour))&lt;100) || !Imgproc.isContourConvex(points)){
              Log.i(TAG, "::onCameraFrame:" + " too small");
              continue;
          }
          if(approx.toArray().length == 3){
              tri++;
          }
          else if(approx.toArray().length &gt;= 4 &amp;&amp; approx.toArray().length &lt;= 6){
              int vtc = points.toArray().length;
              Vector&lt;Double&gt; cosList = new Vector&lt;Double&gt;();
              for (int j = 2; j &lt; vtc+1; j++){
                  cosList.add(angle(points.toArray()[j%vtc], points.toArray()[j-2], points.toArray()[j-1]));
  
              }   
  
              double mincos = Collections.min(cosList);
              double maxcos = Collections.max(cosList);
              if (vtc == 4 &amp;&amp; mincos &gt;= -0.1 &amp;&amp; maxcos &lt;= 0.3)
              {
                  rec++;
              }
  
          }
          else{
  
              double area = Imgproc.contourArea(newMat);
              Rect r = Imgproc.boundingRect(contours.get(i));
              int radius = r.width/2;
              if((Math.abs((1 - ((double)r.width / r.height)) )&lt;= 0.2) &amp;&amp; 
                      Math.abs(area / (Math.PI * Math.pow(radius, 2)))&lt;=0){
              }
              circle++;
  
          }
          totalCircle = circle;
          totalRec = rec;
          totalTri = tri;
  </code></pre>
  
  <p>This is my image testing <a href="http://image.ohozaa.com/i/1b9/ccdgzQ.png" rel="nofollow">Click this</a></p>
  
  <p>Thanks ;)</p>