Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The homography tutorial in java

Hi,

i'm trying to redo this tutorial in Java... but it doesn't work. I've sometimes (i know it's weird) some errors when i'm truing to get the keypoints from the good matches, and the findHomography function didn't work, and the error message isn't very clear for me :

OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function, file ..\..\..\src\opencv\modules\calib3d\src\fundam.cpp, line 1074

I realy hope someone will be able to help me... i'm a novice with OpenCV

Here is my code, sorry it's a bit long :

import java.util.LinkedList;

import java.util.List; import org.opencv.calib3d.Calib3d; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.MatOfDMatch; 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.highgui.Highgui;

class FindObject { public void run(String pathObject, String pathScene) {

System.out.println("\nRunning FindObject");

Mat img_object = Highgui.imread("D:/workspace/HelloCV/".concat(pathObject), 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
Mat img_scene = Highgui.imread("D:/workspace/HelloCV/".concat(pathScene), 0);

FeatureDetector detector = FeatureDetector.create(4); //4 = SURF

MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene  = new MatOfKeyPoint();

detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene);

DescriptorExtractor extractor = DescriptorExtractor.create(2); //2 = SURF;

Mat descriptor_object = new Mat();
Mat descriptor_scene = new Mat() ;

extractor.compute(img_object, keypoints_object, descriptor_object);
extractor.compute(img_scene, keypoints_scene, descriptor_scene);

DescriptorMatcher matcher = DescriptorMatcher.create(1); // 1 = FLANNBASED
MatOfDMatch matches = new MatOfDMatch();

matcher.match(descriptor_object, descriptor_scene, matches);
List<DMatch> matchesList = matches.toList();

Double max_dist = 0.0;
Double min_dist = 100.0;

for(int i = 0; i < descriptor_object.rows(); i++){
    Double dist = (double) matchesList.get(i).distance;
    if(dist < min_dist) min_dist = dist;
    if(dist > max_dist) max_dist = dist;
}   

LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();

for(int i = 0; i < descriptor_object.rows(); i++){
    if(matchesList.get(i).distance < 3*min_dist){
        good_matches.addLast(matchesList.get(i));
    }
}

gm.fromList(good_matches);

Mat img_matches = new Mat();
Features2d.drawMatches(
        img_object,
        keypoints_object, 
        img_scene, 
        keypoints_scene, 
        gm, 
        img_matches, 
        new Scalar(255,0,0), 
        new Scalar(0,0,255), 
        new MatOfByte(), 
        2);

MatOfPoint2f obj = new MatOfPoint2f();
MatOfPoint2f scene = new MatOfPoint2f();

//I've sometimes a bug here, depending of the pictures...
for(int i = 0; i<good_matches.size(); i++){
    //Récupération des points clés des bonnes correspondances
    obj.push_back(keypoints_object.row(good_matches.get(i).queryIdx));
    scene.push_back(keypoints_object.row(good_matches.get(i).trainIdx));
}

//DON'T WORK :
//OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function
Mat H = Calib3d.findHomography(obj, scene);

LinkedList<Point> cornerList = new LinkedList<Point>();
cornerList.add(new Point(0,0));
cornerList.add(new Point(img_object.cols(),0));
cornerList.add(new Point(img_object.cols(),img_object.rows()));
cornerList.add(new Point(0,img_object.rows()));
MatOfPoint obj_corners = new MatOfPoint();
obj_corners.fromList(cornerList);

MatOfPoint scene_corners = new MatOfPoint();

//Later... when findHomoraphy will work
//Core.perspectiveTransform(obj_corners, scene_corners, H);

//Green Lines on the pictures... later
/*
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4); //de 1 à 2
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4); //de 2 à 3
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4); //de 3 à 0
*/

String filename = "result.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, img_matches);

} }

public class Finder { public static void main(String[] args) { // Load the native library. System.loadLibrary("opencv_java244");

new FindObject().run("objectclean.jpg", "scene2clean.jpg");

} }

The homography tutorial in java

Hi,

i'm trying to redo this tutorial in Java... but it doesn't work. I've sometimes (i know it's weird) some errors when i'm truing to get the keypoints from the good matches, and the findHomography function didn't work, and the error message isn't very clear for me :

OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function, file ..\..\..\src\opencv\modules\calib3d\src\fundam.cpp, line 1074

I realy hope someone will be able to help me... i'm a novice with OpenCV

Here is my code, sorry it's a bit long :

import java.util.LinkedList;

import java.util.List; import org.opencv.calib3d.Calib3d; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.MatOfDMatch; 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.highgui.Highgui;

class FindObject { public void run(String pathObject, String pathScene) {

System.out.println("\nRunning FindObject");

Mat img_object = Highgui.imread("D:/workspace/HelloCV/".concat(pathObject), 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
Mat img_scene = Highgui.imread("D:/workspace/HelloCV/".concat(pathScene), 0);

FeatureDetector detector = FeatureDetector.create(4); //4 = SURF

MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene  = new MatOfKeyPoint();

detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene);

DescriptorExtractor extractor = DescriptorExtractor.create(2); //2 = SURF;

Mat descriptor_object = new Mat();
Mat descriptor_scene = new Mat() ;

extractor.compute(img_object, keypoints_object, descriptor_object);
extractor.compute(img_scene, keypoints_scene, descriptor_scene);

DescriptorMatcher matcher = DescriptorMatcher.create(1); // 1 = FLANNBASED
MatOfDMatch matches = new MatOfDMatch();

matcher.match(descriptor_object, descriptor_scene, matches);
List<DMatch> matchesList = matches.toList();

Double max_dist = 0.0;
Double min_dist = 100.0;

for(int i = 0; i < descriptor_object.rows(); i++){
    Double dist = (double) matchesList.get(i).distance;
    if(dist < min_dist) min_dist = dist;
    if(dist > max_dist) max_dist = dist;
}   

LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();

for(int i = 0; i < descriptor_object.rows(); i++){
    if(matchesList.get(i).distance < 3*min_dist){
        good_matches.addLast(matchesList.get(i));
    }
}

gm.fromList(good_matches);

Mat img_matches = new Mat();
Features2d.drawMatches(
        img_object,
        keypoints_object, 
        img_scene, 
        keypoints_scene, 
        gm, 
        img_matches, 
        new Scalar(255,0,0), 
        new Scalar(0,0,255), 
        new MatOfByte(), 
        2);

MatOfPoint2f obj = new MatOfPoint2f();
MatOfPoint2f scene = new MatOfPoint2f();

//I've sometimes a bug here, depending of the pictures...
for(int i = 0; i<good_matches.size(); i++){
    //Récupération des points clés des bonnes correspondances
    obj.push_back(keypoints_object.row(good_matches.get(i).queryIdx));
    scene.push_back(keypoints_object.row(good_matches.get(i).trainIdx));
}

//DON'T WORK :
//OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function
Mat H = Calib3d.findHomography(obj, scene);

LinkedList<Point> cornerList = new LinkedList<Point>();
cornerList.add(new Point(0,0));
cornerList.add(new Point(img_object.cols(),0));
cornerList.add(new Point(img_object.cols(),img_object.rows()));
cornerList.add(new Point(0,img_object.rows()));
MatOfPoint obj_corners = new MatOfPoint();
obj_corners.fromList(cornerList);

MatOfPoint scene_corners = new MatOfPoint();

//Later... when findHomoraphy will work
//Core.perspectiveTransform(obj_corners, scene_corners, H);

//Green Lines on the pictures... later
/*
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4); //de 1 à 2
4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4); //de 2 à 3
4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4); //de 3 à 0
4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
*/

String filename = "result.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, img_matches);

} }

public class Finder { public static void main(String[] args) { // Load the native library. System.loadLibrary("opencv_java244");

new FindObject().run("objectclean.jpg", "scene2clean.jpg");

} }

The homography tutorial in java

Hi,

i'm trying to redo this tutorial in Java... but it doesn't work. I've sometimes (i know it's weird) some errors when i'm truing to get the keypoints from the good matches, and the findHomography function didn't work, and the error message isn't very clear for me :

OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function, file ..\..\..\src\opencv\modules\calib3d\src\fundam.cpp, line 1074

I realy hope someone will be able to help me... i'm a novice with OpenCV

Here is my code, sorry it's a bit long :

import java.util.LinkedList;

import java.util.List; import org.opencv.calib3d.Calib3d; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.MatOfDMatch; 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.highgui.Highgui;

class FindObject { public void run(String pathObject, String pathScene) {

System.out.println("\nRunning FindObject");

Mat img_object = Highgui.imread("D:/workspace/HelloCV/".concat(pathObject), 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
Mat img_scene = Highgui.imread("D:/workspace/HelloCV/".concat(pathScene), 0);

FeatureDetector detector = FeatureDetector.create(4); //4 = SURF

MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene  = new MatOfKeyPoint();

detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene);

DescriptorExtractor extractor = DescriptorExtractor.create(2); //2 = SURF;

Mat descriptor_object = new Mat();
Mat descriptor_scene = new Mat() ;

extractor.compute(img_object, keypoints_object, descriptor_object);
extractor.compute(img_scene, keypoints_scene, descriptor_scene);

DescriptorMatcher matcher = DescriptorMatcher.create(1); // 1 = FLANNBASED
MatOfDMatch matches = new MatOfDMatch();

matcher.match(descriptor_object, descriptor_scene, matches);
List<DMatch> matchesList = matches.toList();

Double max_dist = 0.0;
Double min_dist = 100.0;

for(int i = 0; i < descriptor_object.rows(); i++){
    Double dist = (double) matchesList.get(i).distance;
    if(dist < min_dist) min_dist = dist;
    if(dist > max_dist) max_dist = dist;
}   

LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();

for(int i = 0; i < descriptor_object.rows(); i++){
    if(matchesList.get(i).distance < 3*min_dist){
        good_matches.addLast(matchesList.get(i));
    }
}

gm.fromList(good_matches);

Mat img_matches = new Mat();
Features2d.drawMatches(
        img_object,
        keypoints_object, 
        img_scene, 
        keypoints_scene, 
        gm, 
        img_matches, 
        new Scalar(255,0,0), 
        new Scalar(0,0,255), 
        new MatOfByte(), 
        2);

MatOfPoint2f obj = new MatOfPoint2f();
MatOfPoint2f scene = new MatOfPoint2f();

//I've sometimes a bug here, depending of the pictures...
for(int i = 0; i<good_matches.size(); i++){
    //Récupération des points clés des bonnes correspondances
//Get the keypoints from the good matches
    obj.push_back(keypoints_object.row(good_matches.get(i).queryIdx));
    scene.push_back(keypoints_object.row(good_matches.get(i).trainIdx));
}

//DON'T WORK :
//OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function
Mat H = Calib3d.findHomography(obj, scene);

LinkedList<Point> cornerList = new LinkedList<Point>();
cornerList.add(new Point(0,0));
cornerList.add(new Point(img_object.cols(),0));
cornerList.add(new Point(img_object.cols(),img_object.rows()));
cornerList.add(new Point(0,img_object.rows()));
MatOfPoint obj_corners = new MatOfPoint();
obj_corners.fromList(cornerList);

MatOfPoint scene_corners = new MatOfPoint();

//Later... when findHomoraphy will work
//Core.perspectiveTransform(obj_corners, scene_corners, H);

//Green Lines on the pictures... later
/*
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
*/

String filename = "result.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, img_matches);

} }

public class Finder { public static void main(String[] args) { // Load the native library. System.loadLibrary("opencv_java244");

new FindObject().run("objectclean.jpg", "scene2clean.jpg");

} }

The homography tutorial in java

Hi,

i'm trying to redo this tutorial in Java... but it doesn't work. I've sometimes (i know it's weird) some errors when i'm truing to get the keypoints from the good matches, and the The findHomography function didn't work, and the error message isn't very clear for me :

OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function, file ..\..\..\src\opencv\modules\calib3d\src\fundam.cpp, line 1074

I realy hope someone will be able to help me... i'm a novice with OpenCV

Here is my code, sorry it's a bit long :

(Edit 10 minutes later, sorry i've forgot some comment in french ^^')

import java.util.LinkedList;

import java.util.List; import org.opencv.calib3d.Calib3d; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.MatOfDMatch; 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.highgui.Highgui;

class FindObject { public void run(String pathObject, String pathScene) {

System.out.println("\nRunning FindObject");

Mat img_object = Highgui.imread("D:/workspace/HelloCV/".concat(pathObject), 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
Mat img_scene = Highgui.imread("D:/workspace/HelloCV/".concat(pathScene), 0);

FeatureDetector detector = FeatureDetector.create(4); //4 = SURF

MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene  = new MatOfKeyPoint();

detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene);

DescriptorExtractor extractor = DescriptorExtractor.create(2); //2 = SURF;

Mat descriptor_object = new Mat();
Mat descriptor_scene = new Mat() ;

extractor.compute(img_object, keypoints_object, descriptor_object);
extractor.compute(img_scene, keypoints_scene, descriptor_scene);

DescriptorMatcher matcher = DescriptorMatcher.create(1); // 1 = FLANNBASED
MatOfDMatch matches = new MatOfDMatch();

matcher.match(descriptor_object, descriptor_scene, matches);
List<DMatch> matchesList = matches.toList();

Double max_dist = 0.0;
Double min_dist = 100.0;

for(int i = 0; i < descriptor_object.rows(); i++){
    Double dist = (double) matchesList.get(i).distance;
    if(dist < min_dist) min_dist = dist;
    if(dist > max_dist) max_dist = dist;
}   

LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();

for(int i = 0; i < descriptor_object.rows(); i++){
    if(matchesList.get(i).distance < 3*min_dist){
        good_matches.addLast(matchesList.get(i));
    }
}

gm.fromList(good_matches);

Mat img_matches = new Mat();
Features2d.drawMatches(
        img_object,
        keypoints_object, 
        img_scene, 
        keypoints_scene, 
        gm, 
        img_matches, 
        new Scalar(255,0,0), 
        new Scalar(0,0,255), 
        new MatOfByte(), 
        2);

MatOfPoint2f obj = new MatOfPoint2f();
MatOfPoint2f scene = new MatOfPoint2f();

//I've sometimes a bug here, depending of the pictures...
for(int i = 0; i<good_matches.size(); i++){
    //Get the keypoints from the good matches
    obj.push_back(keypoints_object.row(good_matches.get(i).queryIdx));
    scene.push_back(keypoints_object.row(good_matches.get(i).trainIdx));
scene.push_back(keypoints_scene.row(good_matches.get(i).trainIdx));
}

//DON'T WORK :
//OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function
Mat H = Calib3d.findHomography(obj, scene);

LinkedList<Point> cornerList = new LinkedList<Point>();
cornerList.add(new Point(0,0));
cornerList.add(new Point(img_object.cols(),0));
cornerList.add(new Point(img_object.cols(),img_object.rows()));
cornerList.add(new Point(0,img_object.rows()));
MatOfPoint obj_corners = new MatOfPoint();
obj_corners.fromList(cornerList);

MatOfPoint scene_corners = new MatOfPoint();

//Later... when findHomoraphy will work
//Core.perspectiveTransform(obj_corners, scene_corners, H);

//Green Lines on the pictures... later
/*
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
*/

String filename = "result.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, img_matches);

} }

public class Finder { public static void main(String[] args) { // Load the native library. System.loadLibrary("opencv_java244");

new FindObject().run("objectclean.jpg", "scene2clean.jpg");

} }

The homography tutorial in java

Hi,

i'm trying to redo this tutorial in Java... but it doesn't work. The findHomography function didn't work, and the error message isn't very clear for me :

OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function, file ..\..\..\src\opencv\modules\calib3d\src\fundam.cpp, line 1074

I realy hope someone will be able to help me... i'm a novice with OpenCV

Here is my code, sorry it's a bit long :

(Edit 10 minutes later, sorry i've forgot some comment in french ^^')

import java.util.LinkedList;

import java.util.List; import org.opencv.calib3d.Calib3d; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.MatOfDMatch; 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.highgui.Highgui;

class FindObject { public void run(String pathObject, String pathScene) {

System.out.println("\nRunning FindObject");

Mat img_object = Highgui.imread("D:/workspace/HelloCV/".concat(pathObject), 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
Mat img_scene = Highgui.imread("D:/workspace/HelloCV/".concat(pathScene), 0);

FeatureDetector detector = FeatureDetector.create(4); //4 = SURF

MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene  = new MatOfKeyPoint();

detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene);

DescriptorExtractor extractor = DescriptorExtractor.create(2); //2 = SURF;

Mat descriptor_object = new Mat();
Mat descriptor_scene = new Mat() ;

extractor.compute(img_object, keypoints_object, descriptor_object);
extractor.compute(img_scene, keypoints_scene, descriptor_scene);

DescriptorMatcher matcher = DescriptorMatcher.create(1); // 1 = FLANNBASED
MatOfDMatch matches = new MatOfDMatch();

matcher.match(descriptor_object, descriptor_scene, matches);
List<DMatch> matchesList = matches.toList();

Double max_dist = 0.0;
Double min_dist = 100.0;

for(int i = 0; i < descriptor_object.rows(); i++){
    Double dist = (double) matchesList.get(i).distance;
    if(dist < min_dist) min_dist = dist;
    if(dist > max_dist) max_dist = dist;
}   

LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();

for(int i = 0; i < descriptor_object.rows(); i++){
    if(matchesList.get(i).distance < 3*min_dist){
        good_matches.addLast(matchesList.get(i));
    }
}

gm.fromList(good_matches);

Mat img_matches = new Mat();
Features2d.drawMatches(
        img_object,
        keypoints_object, 
        img_scene, 
        keypoints_scene, 
        gm, 
        img_matches, 
        new Scalar(255,0,0), 
        new Scalar(0,0,255), 
        new MatOfByte(), 
        2);

MatOfPoint2f obj = new MatOfPoint2f();
MatOfPoint2f scene = new MatOfPoint2f();

**//I think the problem probably come from here...**
for(int i = 0; i<good_matches.size(); i++){
    //Get the keypoints from the good matches
    obj.push_back(keypoints_object.row(good_matches.get(i).queryIdx));
    scene.push_back(keypoints_scene.row(good_matches.get(i).trainIdx));
}

//DON'T WORK :
//OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function
Mat H = Calib3d.findHomography(obj, scene);

LinkedList<Point> cornerList = new LinkedList<Point>();
cornerList.add(new Point(0,0));
cornerList.add(new Point(img_object.cols(),0));
cornerList.add(new Point(img_object.cols(),img_object.rows()));
cornerList.add(new Point(0,img_object.rows()));
MatOfPoint obj_corners = new MatOfPoint();
obj_corners.fromList(cornerList);

MatOfPoint scene_corners = new MatOfPoint();

//Later... when findHomoraphy will work
//Core.perspectiveTransform(obj_corners, scene_corners, H);

//Green Lines on the pictures... later
/*
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
*/

String filename = "result.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, img_matches);

} }

public class Finder { public static void main(String[] args) { // Load the native library. System.loadLibrary("opencv_java244");

new FindObject().run("objectclean.jpg", "scene2clean.jpg");

} }

The homography tutorial in java

EDIT : some clue in comments

Hi,

i'm trying to redo this tutorial in Java... but it doesn't work. The findHomography function didn't work, and the error message isn't very clear for me :

OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function, file ..\..\..\src\opencv\modules\calib3d\src\fundam.cpp, line 1074

I realy hope someone will be able to help me... i'm a novice with OpenCV

Here is my code, sorry it's a bit long :

(Edit 10 minutes later, sorry i've forgot some comment in french ^^')

import java.util.LinkedList;

import java.util.List; import org.opencv.calib3d.Calib3d; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.MatOfDMatch; 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.highgui.Highgui;

class FindObject { public void run(String pathObject, String pathScene) {

System.out.println("\nRunning FindObject");

Mat img_object = Highgui.imread("D:/workspace/HelloCV/".concat(pathObject), 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
Mat img_scene = Highgui.imread("D:/workspace/HelloCV/".concat(pathScene), 0);

FeatureDetector detector = FeatureDetector.create(4); //4 = SURF

MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene  = new MatOfKeyPoint();

detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene);

DescriptorExtractor extractor = DescriptorExtractor.create(2); //2 = SURF;

Mat descriptor_object = new Mat();
Mat descriptor_scene = new Mat() ;

extractor.compute(img_object, keypoints_object, descriptor_object);
extractor.compute(img_scene, keypoints_scene, descriptor_scene);

DescriptorMatcher matcher = DescriptorMatcher.create(1); // 1 = FLANNBASED
MatOfDMatch matches = new MatOfDMatch();

matcher.match(descriptor_object, descriptor_scene, matches);
List<DMatch> matchesList = matches.toList();

Double max_dist = 0.0;
Double min_dist = 100.0;

for(int i = 0; i < descriptor_object.rows(); i++){
    Double dist = (double) matchesList.get(i).distance;
    if(dist < min_dist) min_dist = dist;
    if(dist > max_dist) max_dist = dist;
}   

LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();

for(int i = 0; i < descriptor_object.rows(); i++){
    if(matchesList.get(i).distance < 3*min_dist){
        good_matches.addLast(matchesList.get(i));
    }
}

gm.fromList(good_matches);

Mat img_matches = new Mat();
Features2d.drawMatches(
        img_object,
        keypoints_object, 
        img_scene, 
        keypoints_scene, 
        gm, 
        img_matches, 
        new Scalar(255,0,0), 
        new Scalar(0,0,255), 
        new MatOfByte(), 
        2);

MatOfPoint2f obj = new MatOfPoint2f();
MatOfPoint2f scene = new MatOfPoint2f();

**//I think the problem probably come from here...**
for(int i = 0; i<good_matches.size(); i++){
    //Get the keypoints from the good matches
    obj.push_back(keypoints_object.row(good_matches.get(i).queryIdx));
    scene.push_back(keypoints_scene.row(good_matches.get(i).trainIdx));
}

//DON'T WORK :
//OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function
Mat H = Calib3d.findHomography(obj, scene);

LinkedList<Point> cornerList = new LinkedList<Point>();
cornerList.add(new Point(0,0));
cornerList.add(new Point(img_object.cols(),0));
cornerList.add(new Point(img_object.cols(),img_object.rows()));
cornerList.add(new Point(0,img_object.rows()));
MatOfPoint obj_corners = new MatOfPoint();
obj_corners.fromList(cornerList);

MatOfPoint scene_corners = new MatOfPoint();

//Later... when findHomoraphy will work
//Core.perspectiveTransform(obj_corners, scene_corners, H);

//Green Lines on the pictures... later
/*
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
*/

String filename = "result.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, img_matches);

} }

public class Finder { public static void main(String[] args) { // Load the native library. System.loadLibrary("opencv_java244");

new FindObject().run("objectclean.jpg", "scene2clean.jpg");

} }

The homography tutorial in java

EDIT : some clue main problem solved (see in commentscomments) ! But i now have a problem with perspectiveTransform()...

OpenCV Error: Assertion failed (scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F)) in unknown function, file ......\src\opencv\modules\core\src\matmul.cpp, line 1926


Original post

Hi,

i'm trying to redo this tutorial in Java... but it doesn't work. The findHomography function didn't work, and the error message isn't very clear for me :

OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function, file ..\..\..\src\opencv\modules\calib3d\src\fundam.cpp, line 1074

I realy hope someone will be able to help me... i'm a novice with OpenCV

Here is my code, sorry it's a bit long :

import java.util.LinkedList;

import java.util.List; import org.opencv.calib3d.Calib3d; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.MatOfDMatch; 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.highgui.Highgui;

class FindObject { public void run(String pathObject, String pathScene) {

System.out.println("\nRunning FindObject");

Mat img_object = Highgui.imread("D:/workspace/HelloCV/".concat(pathObject), 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
Mat img_scene = Highgui.imread("D:/workspace/HelloCV/".concat(pathScene), 0);

FeatureDetector detector = FeatureDetector.create(4); //4 = SURF

MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene  = new MatOfKeyPoint();

detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene);

DescriptorExtractor extractor = DescriptorExtractor.create(2); //2 = SURF;

Mat descriptor_object = new Mat();
Mat descriptor_scene = new Mat() ;

extractor.compute(img_object, keypoints_object, descriptor_object);
extractor.compute(img_scene, keypoints_scene, descriptor_scene);

DescriptorMatcher matcher = DescriptorMatcher.create(1); // 1 = FLANNBASED
MatOfDMatch matches = new MatOfDMatch();

matcher.match(descriptor_object, descriptor_scene, matches);
List<DMatch> matchesList = matches.toList();

Double max_dist = 0.0;
Double min_dist = 100.0;

for(int i = 0; i < descriptor_object.rows(); i++){
    Double dist = (double) matchesList.get(i).distance;
    if(dist < min_dist) min_dist = dist;
    if(dist > max_dist) max_dist = dist;
}   

LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();

for(int i = 0; i < descriptor_object.rows(); i++){
    if(matchesList.get(i).distance < 3*min_dist){
        good_matches.addLast(matchesList.get(i));
    }
}

gm.fromList(good_matches);

Mat img_matches = new Mat();
Features2d.drawMatches(
        img_object,
        keypoints_object, 
        img_scene, 
        keypoints_scene, 
        gm, 
        img_matches, 
        new Scalar(255,0,0), 
        new Scalar(0,0,255), 
        new MatOfByte(), 
        2);

MatOfPoint2f obj = new MatOfPoint2f();
MatOfPoint2f scene = new MatOfPoint2f();

**//I think the problem probably come from here...**
for(int i = 0; i<good_matches.size(); i++){
    //Get the keypoints from the good matches
    obj.push_back(keypoints_object.row(good_matches.get(i).queryIdx));
    scene.push_back(keypoints_scene.row(good_matches.get(i).trainIdx));
}

//DON'T WORK :
//OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function
Mat H = Calib3d.findHomography(obj, scene);

LinkedList<Point> cornerList = new LinkedList<Point>();
cornerList.add(new Point(0,0));
cornerList.add(new Point(img_object.cols(),0));
cornerList.add(new Point(img_object.cols(),img_object.rows()));
cornerList.add(new Point(0,img_object.rows()));
MatOfPoint obj_corners = new MatOfPoint();
obj_corners.fromList(cornerList);

MatOfPoint scene_corners = new MatOfPoint();

//Later... when findHomoraphy will work
//Core.perspectiveTransform(obj_corners, scene_corners, H);

//Green Lines on the pictures... later
/*
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
*/

String filename = "result.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, img_matches);

} }

public class Finder { public static void main(String[] args) { // Load the native library. System.loadLibrary("opencv_java244");

new FindObject().run("objectclean.jpg", "scene2clean.jpg");

} }

The homography tutorial in java

EDIT : main problem solved (see in comments) ! But i now have a problem with perspectiveTransform()...

OpenCV Error: Assertion failed (scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F)) in unknown function, file ......\src\opencv\modules\core\src\matmul.cpp, line 1926


Original post

Hi,

i'm trying to redo this tutorial in Java... but it doesn't work. The findHomography function didn't work, and the error message isn't very clear for me :

OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function, file ..\..\..\src\opencv\modules\calib3d\src\fundam.cpp, line 1074

I realy hope someone will be able to help me... i'm a novice with OpenCV

Here is my code, sorry it's a bit long :

CODE UPDATED

import java.util.LinkedList;

import java.util.List; import org.opencv.calib3d.Calib3d; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.MatOfDMatch; 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.highgui.Highgui;

class FindObject { public void run(String pathObject, String pathScene) {

pathScene, String pathResult) {

System.out.println("\nRunning FindObject");

Mat img_object = Highgui.imread("D:/workspace/HelloCV/".concat(pathObject), Highgui.imread("D:/workspaceSeirich/HelloCV/".concat(pathObject), 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
Mat img_scene = Highgui.imread("D:/workspace/HelloCV/".concat(pathScene), Highgui.imread("D:/workspaceSeirich/HelloCV/".concat(pathScene), 0);

FeatureDetector detector = FeatureDetector.create(4); //4 = SURF
SURF 

MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene  = new MatOfKeyPoint();

detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene);

DescriptorExtractor extractor = DescriptorExtractor.create(2); //2 = SURF;

Mat descriptor_object = new Mat();
Mat descriptor_scene = new Mat() ;

extractor.compute(img_object, keypoints_object, descriptor_object);
extractor.compute(img_scene, keypoints_scene, descriptor_scene);

DescriptorMatcher matcher = DescriptorMatcher.create(1); // 1 = FLANNBASED
MatOfDMatch matches = new MatOfDMatch();

matcher.match(descriptor_object, descriptor_scene, matches);
List<DMatch> matchesList = matches.toList();

Double max_dist = 0.0;
Double min_dist = 100.0;

for(int i = 0; i < descriptor_object.rows(); i++){
    Double dist = (double) matchesList.get(i).distance;
    if(dist < min_dist) min_dist = dist;
    if(dist > max_dist) max_dist = dist;
} }

System.out.println("-- Max dist : " + max_dist);
System.out.println("-- Min dist : " + min_dist);    

LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();

for(int i = 0; i < descriptor_object.rows(); i++){
    if(matchesList.get(i).distance < 3*min_dist){
        good_matches.addLast(matchesList.get(i));
    }
}

gm.fromList(good_matches);

Mat img_matches = new Mat();
Features2d.drawMatches(
        img_object,
        keypoints_object, 
        img_scene, 
img_scene,
        keypoints_scene, 
        gm, 
        img_matches, 
        new Scalar(255,0,0), 
        new Scalar(0,0,255), 
        new MatOfByte(), 
        2);

MatOfPoint2f obj = new MatOfPoint2f();
MatOfPoint2f scene = new MatOfPoint2f();

**//I think the problem probably come from here...**
LinkedList<Point> objList = new LinkedList<Point>();
LinkedList<Point> sceneList = new LinkedList<Point>();

List<KeyPoint> keypoints_objectList = keypoints_object.toList();
List<KeyPoint> keypoints_sceneList = keypoints_scene.toList();

for(int i = 0; i<good_matches.size(); i++){
    //Get the keypoints from the good matches
    obj.push_back(keypoints_object.row(good_matches.get(i).queryIdx));
    scene.push_back(keypoints_scene.row(good_matches.get(i).trainIdx));
objList.addLast(keypoints_objectList.get(good_matches.get(i).queryIdx).pt);
    sceneList.addLast(keypoints_sceneList.get(good_matches.get(i).trainIdx).pt);
}

//DON'T WORK :
//OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function
MatOfPoint2f obj = new MatOfPoint2f();
obj.fromList(objList);

MatOfPoint2f scene = new MatOfPoint2f();
scene.fromList(sceneList);

Mat H = Calib3d.findHomography(obj, scene);

LinkedList<Point> cornerList = new LinkedList<Point>();
cornerList.add(new Point(0,0));
cornerList.add(new Point(img_object.cols(),0));
cornerList.add(new Point(img_object.cols(),img_object.rows()));
cornerList.add(new Point(0,img_object.rows()));
 MatOfPoint obj_corners = new MatOfPoint();
obj_corners.fromList(cornerList);

MatOfPoint scene_corners = new MatOfPoint();

//Later... //ERROR HERE :
//OpenCV Error: Assertion failed (scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F)) in unknown function, file ..\..\..\src\opencv\modules\core\src\matmul.cpp, line 1926
Core.perspectiveTransform(obj_corners, scene_corners, H);

//Draw the lines... later, when findHomoraphy the homography will work
//Core.perspectiveTransform(obj_corners, scene_corners, H);

//Green Lines on the pictures... later
/*
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);
*/

String filename = "result.png";
//Sauvegarde du résultat
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, pathResult));
Highgui.imwrite(pathResult, img_matches);

} }

public class Finder {
public static void main(String[] args) {
// Load the native library.
System.loadLibrary("opencv_java244");

new FindObject().run("objectclean.jpg", "scene2clean.jpg");
System.loadLibrary("opencv_java244");
new FindObject().run("object.jpg", "scene.jpg", "resultat.png");
}
}

} }