Skip to content

Instantly share code, notes, and snippets.

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

Embed
What would you like to do?
import org.opencv.core.*;
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;
/**
* 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);
MatOfByte mask = new MatOfByte(new byte[matOfDMatch.height()]);
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("matched.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 MatOfDMatch getMatOfMatchedPoints(Mat descriptorsFirst, Mat descriptorsSecond) {
MatOfDMatch matOfDMatch = new MatOfDMatch();
DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_SL2);
descriptorMatcher.match(descriptorsFirst, descriptorsSecond, matOfDMatch);
return matOfDMatch;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.