Ask Your Question

Revision history [back]

error with opencv threshold and findcontours

I try to load two Images and get the matched keypoints and draw convexhull on the matched keypoints with bigger size ..any help with this error this my code: import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.ImageView;

import org.opencv.android.Utils; import org.opencv.core.Mat; import org.opencv.core.MatOfDMatch; import org.opencv.core.MatOfInt; import org.opencv.core.MatOfKeyPoint; import org.opencv.core.MatOfPoint; import org.opencv.core.MatOfPoint2f; import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.features2d.DMatch; import org.opencv.features2d.DescriptorExtractor; import org.opencv.features2d.DescriptorMatcher; import org.opencv.features2d.FeatureDetector; import org.opencv.features2d.Features2d; import org.opencv.features2d.KeyPoint; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; import org.opencv.utils.Converters;

import java.util.ArrayList; import java.util.Iterator; import java.util.List;

import static java.lang.System.in; import static org.opencv.core.CvType.CV_8UC1; import static org.opencv.core.CvType.CV_8UC3; import static org.opencv.features2d.Features2d.drawKeypoints; import static org.opencv.highgui.Highgui.imread; import static org.opencv.imgproc.Imgproc.THRESH_BINARY; import static org.opencv.imgproc.Imgproc.convexHull; import static org.opencv.imgproc.Imgproc.drawContours; import static org.opencv.imgproc.Imgproc.findContours; import static org.opencv.imgproc.Imgproc.threshold;

public class MainActivity extends AppCompatActivity { static { System.loadLibrary("opencv_java"); System.loadLibrary("nonfree"); } private ImageView imageView; private Bitmap inputImage; // make bitmap from image resource private Bitmap inputImage2; // make bitmap from image resource private FeatureDetector detector = FeatureDetector.create(FeatureDetector.SIFT); DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SIFT);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) this.findViewById(R.id.imageView);
    sift();
}

@Override
public void onResume() {
    super.onResume();
}

public void sift() {
    inputImage = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    inputImage2 = BitmapFactory.decodeResource(getResources(), R.drawable.shot);
    Mat rgba = new Mat();
    Mat rgba2 = new Mat();

    Utils.bitmapToMat(inputImage, rgba);
    Utils.bitmapToMat(inputImage2, rgba2);


    MatOfKeyPoint keyPoints = new MatOfKeyPoint();
    MatOfKeyPoint keypoints2 = new MatOfKeyPoint();

    Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGBA2GRAY);
    Imgproc.cvtColor(rgba2, rgba2, Imgproc.COLOR_RGBA2GRAY);

    detector.detect(rgba, keyPoints);
    detector.detect(rgba2, keypoints2);

    Mat descriptor1 = new Mat();
    Mat descriptor2 = new Mat();

    extractor.compute( rgba, keyPoints, descriptor1 );
    extractor.compute( rgba2, keypoints2, descriptor2 );

    MatOfDMatch matches1 =new MatOfDMatch();

    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
    matcher.match(descriptor2, descriptor1, matches1 );

    List<KeyPoint>obj=new ArrayList<>();
    List<KeyPoint>scene=new ArrayList<>();
    List<KeyPoint> filtered1=new ArrayList<KeyPoint>();
    List<KeyPoint> filtered2=new ArrayList<KeyPoint>();
    List<Point> filtered11=new ArrayList<>();
   // List<Point> filtered22=new ArrayList<>();

// //Log.d("MainActivity","matches "+ matches1.size()); // List<dmatch> good_matches =new ArrayList<>();

    for (int i=0;i<matches1.toArray().length ;i++) {
        if (matches1.toArray()[i].distance <= 150 ) {
           // good_matches.add(matches1.toArray()[i]);
            DMatch match = matches1.toArray()[i];
            KeyPoint x = keyPoints.toList().get(match.trainIdx);
            obj.add(x);
            filtered11.add(x.pt);
            KeyPoint y = keypoints2.toList().get(match.queryIdx);
            scene.add(y);
           // filtered22.add(y.pt);

            if (y.size > x.size) {
                filtered1.add(x);
              //  filtered2.add(y);
            }

        }

    }


    /*********************************/
    Mat cannyMat = new Mat();
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();
    MatOfInt hull = new MatOfInt();
   //  Mat fil1 = Converters.vector_KeyPoint_to_Mat(filtered1);
    final MatOfPoint goodReferencePoints = new MatOfPoint();
    goodReferencePoints.fromList(filtered11);
   // final MatOfPoint2f goodScenePoints = new MatOfPoint2f();
    //goodScenePoints.fromList(filtered22);
    goodReferencePoints.convertTo(cannyMat,CV_8UC1);

    Mat threshold_output=new Mat(cannyMat.height(),cannyMat.width(),CV_8UC1);
    Imgproc.cvtColor(cannyMat, cannyMat, Imgproc.COLOR_RGBA2GRAY);
    Imgproc.threshold(cannyMat, threshold_output, 100, 255,THRESH_BINARY);
    Imgproc.findContours(threshold_output, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    Imgproc.convexHull(contours.get(0), hull);

    Features2d.drawKeypoints(rgba2, keypoints2, rgba2);

    Utils.matToBitmap(cannyMat, inputImage2);
    imageView.setImageBitmap(inputImage2);
    Log.d("MainActivity", "finish ");

}

}

click to hide/show revision 2
No.2 Revision

error with opencv threshold and findcontours

I try to load two Images and get the matched keypoints and draw convexhull on the matched keypoints with bigger size ..any help with this error this my code: code:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;

android.widget.ImageView; import org.opencv.android.Utils; import org.opencv.core.Mat; import org.opencv.core.MatOfDMatch; import org.opencv.core.MatOfInt; import org.opencv.core.MatOfKeyPoint; import org.opencv.core.MatOfPoint; import org.opencv.core.MatOfPoint2f; import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.features2d.DMatch; import org.opencv.features2d.DescriptorExtractor; import org.opencv.features2d.DescriptorMatcher; import org.opencv.features2d.FeatureDetector; import org.opencv.features2d.Features2d; import org.opencv.features2d.KeyPoint; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; import org.opencv.utils.Converters;

org.opencv.utils.Converters; import java.util.ArrayList; import java.util.Iterator; import java.util.List;

java.util.List; import static java.lang.System.in; import static org.opencv.core.CvType.CV_8UC1; import static org.opencv.core.CvType.CV_8UC3; import static org.opencv.features2d.Features2d.drawKeypoints; import static org.opencv.highgui.Highgui.imread; import static org.opencv.imgproc.Imgproc.THRESH_BINARY; import static org.opencv.imgproc.Imgproc.convexHull; import static org.opencv.imgproc.Imgproc.drawContours; import static org.opencv.imgproc.Imgproc.findContours; import static org.opencv.imgproc.Imgproc.threshold;

org.opencv.imgproc.Imgproc.threshold; public class MainActivity extends AppCompatActivity { static { System.loadLibrary("opencv_java"); System.loadLibrary("nonfree"); } private ImageView imageView; private Bitmap inputImage; // make bitmap from image resource private Bitmap inputImage2; // make bitmap from image resource private FeatureDetector detector = FeatureDetector.create(FeatureDetector.SIFT); DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SIFT);

DescriptorExtractor.create(DescriptorExtractor.SIFT);

    @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     imageView = (ImageView) this.findViewById(R.id.imageView);
     sift();
 }

 @Override
 public void onResume() {
     super.onResume();
 }

 public void sift() {
     inputImage = BitmapFactory.decodeResource(getResources(), R.drawable.test);
     inputImage2 = BitmapFactory.decodeResource(getResources(), R.drawable.shot);
     Mat rgba = new Mat();
     Mat rgba2 = new Mat();

     Utils.bitmapToMat(inputImage, rgba);
     Utils.bitmapToMat(inputImage2, rgba2);

      MatOfKeyPoint keyPoints = new MatOfKeyPoint();
     MatOfKeyPoint keypoints2 = new MatOfKeyPoint();

     Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGBA2GRAY);
     Imgproc.cvtColor(rgba2, rgba2, Imgproc.COLOR_RGBA2GRAY);

     detector.detect(rgba, keyPoints);
     detector.detect(rgba2, keypoints2);

     Mat descriptor1 = new Mat();
     Mat descriptor2 = new Mat();

     extractor.compute( rgba, keyPoints, descriptor1 );
     extractor.compute( rgba2, keypoints2, descriptor2 );

     MatOfDMatch matches1 =new MatOfDMatch();

     DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
     matcher.match(descriptor2, descriptor1, matches1 );

     List<KeyPoint>obj=new ArrayList<>();
     List<KeyPoint>scene=new ArrayList<>();
     List<KeyPoint> filtered1=new ArrayList<KeyPoint>();
     List<KeyPoint> filtered2=new ArrayList<KeyPoint>();
     List<Point> filtered11=new ArrayList<>();
    // List<Point> filtered22=new ArrayList<>();

// //Log.d("MainActivity","matches "+ matches1.size()); // List<dmatch> List<DMatch> good_matches =new ArrayList<>();

ArrayList<>();

        for (int i=0;i<matches1.toArray().length ;i++) {
         if (matches1.toArray()[i].distance <= 150 ) {
            // good_matches.add(matches1.toArray()[i]);
             DMatch match = matches1.toArray()[i];
             KeyPoint x = keyPoints.toList().get(match.trainIdx);
             obj.add(x);
             filtered11.add(x.pt);
             KeyPoint y = keypoints2.toList().get(match.queryIdx);
             scene.add(y);
            // filtered22.add(y.pt);

             if (y.size > x.size) {
                 filtered1.add(x);
               //  filtered2.add(y);
             }
          }
      }

      /*********************************/
     Mat cannyMat = new Mat();
     List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
     Mat hierarchy = new Mat();
     MatOfInt hull = new MatOfInt();
    //  Mat fil1 = Converters.vector_KeyPoint_to_Mat(filtered1);
     final MatOfPoint goodReferencePoints = new MatOfPoint();
     goodReferencePoints.fromList(filtered11);
    // final MatOfPoint2f goodScenePoints = new MatOfPoint2f();
     //goodScenePoints.fromList(filtered22);
     goodReferencePoints.convertTo(cannyMat,CV_8UC1);

     Mat threshold_output=new Mat(cannyMat.height(),cannyMat.width(),CV_8UC1);
     Imgproc.cvtColor(cannyMat, cannyMat, Imgproc.COLOR_RGBA2GRAY);
     Imgproc.threshold(cannyMat, threshold_output, 100, 255,THRESH_BINARY);
     Imgproc.findContours(threshold_output, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
     Imgproc.convexHull(contours.get(0), hull);

     Features2d.drawKeypoints(rgba2, keypoints2, rgba2);

     Utils.matToBitmap(cannyMat, inputImage2);
     imageView.setImageBitmap(inputImage2);
     Log.d("MainActivity", "finish ");
     }
}

}