Skip to content

Instantly share code, notes, and snippets.

@sokolek
Created December 1, 2014 19:20
Show Gist options
  • Save sokolek/1b53bd1d7cdddb84aabf to your computer and use it in GitHub Desktop.
Save sokolek/1b53bd1d7cdddb84aabf to your computer and use it in GitHub Desktop.
import org.opencv.calib3d.Calib3d;
import org.opencv.core.*;
import org.opencv.features2d.*;
import org.opencv.highgui.Highgui;
import utils.MatConverter;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;
/**
* Created by Marcin on 2014-11-04.
*/
public class OpencvMaskTest {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
//setup SIFT detector and SIFT extractor
static DescriptorExtractor siftDescriptor = DescriptorExtractor.create(DescriptorExtractor.SIFT);
static FeatureDetector siftDetector = FeatureDetector.create(FeatureDetector.PYRAMID_SIFT);
public static void main(String[] args) {
// read images as Mat
Mat first = Highgui.imread("Cabildo_Catedralicio_(1913),_de_Refugio_Reyes_Rivas.jpg");
Mat second = Highgui.imread("Cabildo_Catedralicio_(1913),_de_Refugio_Reyes_Rivas_1.jpg");
//detect keyPoints
MatOfKeyPoint keyPointsFirst = getKeyPoints(first);
MatOfKeyPoint keyPointsSecond = getKeyPoints(second);
// calculate descriptors
Mat descriptorsFirst = getDescriptor(first, keyPointsFirst);
Mat descriptorsSecond = getDescriptor(second, keyPointsSecond);
//match points
MatOfDMatch matOfDMatch = getMatOfMatchedPoints(descriptorsFirst, descriptorsSecond);
// find Homography
MatOfPoint2f firstMatOfPoint2f = converMatchedToMatOfPoint2f(keyPointsFirst, matOfDMatch, true);
MatOfPoint2f secondMatOfPoint2f = converMatchedToMatOfPoint2f(keyPointsSecond, matOfDMatch, false);
MatOfByte mask = new MatOfByte();
Calib3d.findHomography(firstMatOfPoint2f, secondMatOfPoint2f, Calib3d.RANSAC, 1, mask);
// sum numbers of matched, just for curious
double sum = Stream.of(MatConverter.matToDouble(mask)).mapToDouble(array -> array[0]).sum();
System.out.println(sum);
Mat output = new Mat();
Features2d.drawMatches(
first, keyPointsFirst,
second, keyPointsSecond,
matOfDMatch,
output,
Scalar.all(-1), Scalar.all(-1),
mask, // MASK!!!!!!!!!!!!!!!!
Features2d.NOT_DRAW_SINGLE_POINTS
);
Highgui.imwrite("matching.jpg", output);
}
public static MatOfKeyPoint getKeyPoints(Mat image) {
MatOfKeyPoint keyPoints = new MatOfKeyPoint();
siftDetector.detect(image, keyPoints);
return keyPoints;
}
public static Mat getDescriptor(Mat image, MatOfKeyPoint keyPoints) {
Mat descriptor = new Mat();
siftDescriptor.compute(image, keyPoints, descriptor);
return descriptor;
}
public static MatOfPoint2f converMatchedToMatOfPoint2f(MatOfKeyPoint keyPoints, MatOfDMatch matOfDMatch, boolean queryIdx) {
MatOfPoint2f convertedMatOfPoint = new MatOfPoint2f();
LinkedList<Point> pointList = new LinkedList<>();
List<KeyPoint> keyPointList = keyPoints.toList();
for (DMatch dMatche : matOfDMatch.toArray()) {
int idxOfQueryOrTrain = queryIdx ? dMatche.queryIdx : dMatche.trainIdx;
pointList.addLast(keyPointList.get(idxOfQueryOrTrain).pt);
}
convertedMatOfPoint.fromList(pointList);
return convertedMatOfPoint;
}
public static MatOfDMatch getMatOfMatchedPoints(Mat descriptorsFirst, Mat descriptorsSecond) {
MatOfDMatch matOfDMatch = new MatOfDMatch();
DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_SL2);
descriptorMatcher.match(descriptorsFirst, descriptorsSecond, matOfDMatch);
return matOfDMatch;
}
}