error with opencv threshold and findcontours

asked 2017-03-20 07:22:31 -0500 gravatar image

updated 2017-03-20 08:51:23 -0500

Eduardo gravatar image

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.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

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;
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 {
    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);

    protected void onCreate(Bundle savedInstanceState) {
        imageView = (ImageView) this.findViewById(;

    public void 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);
                KeyPoint y = keypoints2.toList().get(match.queryIdx);
               // filtered22.add(;

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

        Mat cannyMat ...
edit retag flag offensive close merge delete


this is the error error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int) gravatar ( 2017-03-20 07:27:12 -0500 )edit

The error means that the input image must be a 3-channel or a 4-channel mat.

Eduardo gravatar imageEduardo ( 2017-03-20 08:53:59 -0500 )edit

how can I do this ?? gravatar ( 2017-03-20 09:03:58 -0500 )edit

the keyPoints Mat is 7 channel how can I convert it to 3 channels so I can apply gray function gravatar ( 2017-03-20 10:02:44 -0500 )edit

Cannot help more. I don't understand what you are trying to do here: goodReferencePoints.convertTo(cannyMat,CV_8UC1);.

A keypoint is roughly only a 2D location. With all keypoints matched, you have a set of points. But here: Imgproc.findContours(threshold_output, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

You are trying to detect contours from a set of points?

Eduardo gravatar imageEduardo ( 2017-03-20 10:55:03 -0500 )edit

Yes I try to get the convex area around the key points to compare the two convex of the matched keypoints of the two images .. so I can dectec the obstacle due to size expansion algorithm gravatar ( 2017-03-20 13:28:36 -0500 )edit