Adding MatOfPoint items to ArrayList-shape detection for Android

asked 2015-12-13 03:59:00 -0500

I'm using Java bindings for detecting simple shapes on Android. Here's my code following the OpenCV tutorial. However, I get a null pointer exception when adding the approximate curve matrix (approx) to detected rectangles list (rectangles). What am I doing wrong?

public boolean detectRectangle(){

    boolean detected=false;

    Imgproc.findContours(touchedRegionBin.clone(), mContours= new ArrayList<MatOfPoint>(), hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    //      private List<Point> approx = new ArrayList <Point>();
    //      private MatOfPoint2f contours2f= new MatOfPoint2f();
    //      private MatOfPoint2f approx2f = new MatOfPoint2f();
    //      private List<MatOfPoint> mContours = new ArrayList<MatOfPoint>();


    for (int i = 0; i < mContours.size(); i++)
    {

        mContours.get(i).convertTo(contours2f, CvType.CV_32FC2);

        // Approximate contour with accuracy proportional
        // to the contour perimeter
        Imgproc.approxPolyDP(contours2f, approx2f, Imgproc.arcLength(contours2f, true)*0.02, true);

        Converters.Mat_to_vector_Point(approx2f, approx);


        // number of vertices
        if (approx.size() == 4 
                // && Math.abs(Imgproc.contourArea(contours2f)) > 1000 
                // && Imgproc.isContourConvex((MatOfPoint)approx)
                )
        {

            double maxCosine = 0;

            // Get the degree (in cosines) of all corners
            for( int j = 2; j < 5; j++ )
            {

                double cosine = Math.abs(angle( (approx.get(j%4)), approx.get(j-2), approx.get(j-1)));
                maxCosine = Math.max(maxCosine, cosine);
            }

            if( maxCosine <= 0.3 ){
                rectangles.add((MatOfPoint) approx);

                boundingR = Imgproc.boundingRect(mContours.get(i));
                detected= true;
            }
        }

    }//end of for

    return detected;

}
edit retag flag offensive close merge delete