Skip to content

Instantly share code, notes, and snippets.

@sokolek sokolek/OpencvMaskTest.java Secret
Created Dec 1, 2014

Embed
What would you like to do?
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;
}
}
You can’t perform that action at this time.