error with opencv threshold and findcontours [closed]
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 ...
this is the error error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
The error means that the input image must be a 3-channel or a 4-channel mat.
how can I do this ??
the keyPoints Mat is 7 channel how can I convert it to 3 channels so I can apply gray function
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?
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