Я хочу извлечь часть изображения, которая соответствует моему эталонному изображению. Я пытаюсь преобразовать изображение с помощью метода Calib3d.findHomography. Когда я сделал это, я использую Imgproc.warpPerspective для преобразования, но без хороших результатов. Я что-то пропустил здесь? Нужно ли мне что-то делать сspectiveTransform? Я пробовал это, но пока безуспешно.
Вот мой метод findSceneCorners:
private void findSceneCorners(Mat src) {
mFeatureDetector.detect(src, mSceneKeypoints);
mDescriptorExtractor.compute(src, mSceneKeypoints, mSceneDescriptors);
mDescriptorMatcher.match(mSceneDescriptors, mReferenceDescriptors, mMatches);
List<DMatch> matchesList = mMatches.toList();
if (matchesList.size() < 4) {
// There are too few matches to find the homography.
return;
}
List<KeyPoint> referenceKeypointsList =
mReferenceKeypoints.toList();
List<KeyPoint> sceneKeypointsList =
mSceneKeypoints.toList();
// Calculate the max and min distances between keypoints.
double maxDist = 0.0;
double minDist = Double.MAX_VALUE;
for(DMatch match : matchesList) {
double dist = match.distance;
if (dist < minDist) {
minDist = dist;
}
if (dist > maxDist) {
maxDist = dist;
}
}
// The thresholds for minDist are chosen subjectively
// based on testing. The unit is not related to pixel
// distances; it is related to the number of failed tests
// for similarity between the matched descriptors.
if (minDist > 50.0) {
// The target is completely lost.
// Discard any previously found corners.
mSceneCorners.create(0, 0, mSceneCorners.type());
return;
} else if (minDist > 20.0) {
// The target is lost but maybe it is still close.
// Keep any previously found corners.
return;
}
// Identify "good" keypoints based on match distance.
ArrayList<Point> goodReferencePointsList =
new ArrayList<Point>();
ArrayList<Point> goodScenePointsList =
new ArrayList<Point>();
double maxGoodMatchDist = 1.75 * minDist;
for(DMatch match : matchesList) {
if (match.distance < maxGoodMatchDist) {
goodReferencePointsList.add(
referenceKeypointsList.get(match.trainIdx).pt);
goodScenePointsList.add(
sceneKeypointsList.get(match.queryIdx).pt);
}
}
if (goodReferencePointsList.size() < 4 ||
goodScenePointsList.size() < 4) {
// There are too few good points to find the homography.
return;
}
Log.i(TAG, "Match found");
MatOfPoint2f goodReferencePoints = new MatOfPoint2f();
goodReferencePoints.fromList(goodReferencePointsList);
MatOfPoint2f goodScenePoints = new MatOfPoint2f();
goodScenePoints.fromList(goodScenePointsList);
homography = Calib3d.findHomography(goodReferencePoints, goodScenePoints);
Mat quad = new Mat(mReferenceImage.size(), CvType.CV_32F);
Imgproc.warpPerspective(src, quad, homography, quad.size());
objectDetectedListener.objectDetected(quad);
}