use of org.opencv.core.DMatch in project PhotonCamera by eszdman.
the class AlignAndMergeCV method findFrameHomography.
Mat findFrameHomography(Mat need, Mat from) {
Mat descriptors2 = new Mat();
MatOfKeyPoint keyPoints2 = new MatOfKeyPoint();
if (keypointsRef == null) {
akaze.detectAndCompute(need, new Mat(), keyPoints1, descriptors1);
descriptors1.convertTo(descriptors1, CvType.CV_8UC1);
}
akaze.detectAndCompute(from, new Mat(), keyPoints2, descriptors2);
descriptors2.convertTo(descriptors2, CvType.CV_8UC1);
MatOfDMatch matches = new MatOfDMatch();
Log.d("AlignAndMergeCV", "src1:" + descriptors1.type() + ":" + descriptors1.cols() + ":" + descriptors1.rows());
Log.d("AlignAndMergeCV", "src2:" + descriptors2.type() + ":" + descriptors2.cols() + ":" + descriptors2.rows());
keypointsRef = keyPoints1.toList();
Log.d("AlignAndMergeCV", "keyp size:" + keyPoints1.size());
if (keypointsRef.size() < 10) {
return OneMatrix();
}
Log.d(Name, "Descr:" + descriptors1.toString());
matcher.match(descriptors1, descriptors2, matches, new Mat());
MatOfPoint2f points1 = new MatOfPoint2f(), points2 = new MatOfPoint2f();
DMatch[] arr = matches.toArray();
List<KeyPoint> keypoints2 = keyPoints2.toList();
if (PhotonCamera.getSettings().DebugData) {
Mat imMatches = new Mat();
Mat keyPoints = new Mat();
drawMatches(need, keyPoints1, from, keyPoints2, matches, imMatches);
drawKeypoints(need, keyPoints1, keyPoints);
Imgcodecs.imwrite(jpgFilePathToSave.toString().replace(".jpg", "cvin.jpg"), imMatches);
Imgcodecs.imwrite(jpgFilePathToSave.toString().replace(".jpg", "kp.jpg"), keyPoints);
imMatches.release();
keyPoints.release();
}
ArrayList<Point> keypoints1f = new ArrayList<>();
ArrayList<Point> keypoints2f = new ArrayList<>();
int step = arr.length / 3500 + 1;
for (int i = 0; i < arr.length; i += step) {
DMatch dMatch = arr[i];
Point on1 = keypointsRef.get(dMatch.queryIdx).pt;
Point on2 = keypoints2.get(dMatch.trainIdx).pt;
keypoints1f.add(on1);
keypoints2f.add(on2);
}
Log.d(Name, "After filtering:" + keypoints1f.size());
if (arr.length < 5) {
return OneMatrix();
}
points1.fromArray(keypoints1f.toArray(new Point[keypoints1f.size()]));
points2.fromArray(keypoints2f.toArray(new Point[keypoints2f.size()]));
Mat h = null;
if (!points1.empty() && !points2.empty()) {
Log.d(Name, "points1:" + points1.toString());
int iters = 7000 / (arr.length / 3000 + 1);
h = findHomography(points1, points2, RANSAC, 3.0, new Mat(), iters);
}
// keyPoints1.release();
keyPoints2.release();
if (h == null || h.size().height == 0)
return OneMatrix();
return h;
}
use of org.opencv.core.DMatch in project Water_meter_identification by cqlaiyang.
the class Converters method vector_DMatch_to_Mat.
public static Mat vector_DMatch_to_Mat(List<DMatch> matches) {
Mat res;
int count = (matches != null) ? matches.size() : 0;
if (count > 0) {
res = new Mat(count, 1, CvType.CV_64FC4);
double[] buff = new double[count * 4];
for (int i = 0; i < count; i++) {
DMatch m = matches.get(i);
buff[4 * i] = m.queryIdx;
buff[4 * i + 1] = m.trainIdx;
buff[4 * i + 2] = m.imgIdx;
buff[4 * i + 3] = m.distance;
}
res.put(0, 0, buff);
} else {
res = new Mat();
}
return res;
}
use of org.opencv.core.DMatch in project Relic_Main by TeamOverdrive.
the class MatOfDMatch method fromArray.
public void fromArray(DMatch... a) {
if (a == null || a.length == 0)
return;
int num = a.length;
alloc(num);
float[] buff = new float[num * _channels];
for (int i = 0; i < num; i++) {
DMatch m = a[i];
buff[_channels * i + 0] = m.queryIdx;
buff[_channels * i + 1] = m.trainIdx;
buff[_channels * i + 2] = m.imgIdx;
buff[_channels * i + 3] = m.distance;
}
// TODO: check ret val!
put(0, 0, buff);
}
use of org.opencv.core.DMatch in project Relic_Main by TeamOverdrive.
the class Converters method vector_DMatch_to_Mat.
public static Mat vector_DMatch_to_Mat(List<DMatch> matches) {
Mat res;
int count = (matches != null) ? matches.size() : 0;
if (count > 0) {
res = new Mat(count, 1, CvType.CV_64FC4);
double[] buff = new double[count * 4];
for (int i = 0; i < count; i++) {
DMatch m = matches.get(i);
buff[4 * i] = m.queryIdx;
buff[4 * i + 1] = m.trainIdx;
buff[4 * i + 2] = m.imgIdx;
buff[4 * i + 3] = m.distance;
}
res.put(0, 0, buff);
} else {
res = new Mat();
}
return res;
}
use of org.opencv.core.DMatch in project Relic_Main by TeamOverdrive.
the class Converters method Mat_to_vector_DMatch.
public static void Mat_to_vector_DMatch(Mat m, List<DMatch> matches) {
if (matches == null)
throw new java.lang.IllegalArgumentException("Output List can't be null");
int count = m.rows();
if (CvType.CV_64FC4 != m.type() || m.cols() != 1)
throw new java.lang.IllegalArgumentException("CvType.CV_64FC4 != m.type() || m.cols()!=1\n" + m);
matches.clear();
double[] buff = new double[4 * count];
m.get(0, 0, buff);
for (int i = 0; i < count; i++) {
matches.add(new DMatch((int) buff[4 * i], (int) buff[4 * i + 1], (int) buff[4 * i + 2], (float) buff[4 * i + 3]));
}
}
Aggregations