我想提取与我的参考图像匹配的图像部分。
我尝试使用Calib3d.findHomography方法变换图像。
完成此操作后,我将使用Imgproc.warpPerspective进行转换,但效果不佳。我在这里想念什么吗?
我需要使用PerspectiveTransform做些事情吗?我已经尝试过了,但是到目前为止没有任何运气。
这是我的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);
}
最佳答案
我认为您应该使用WARP_INVERSE_MAP
作为warpPerspective
的标志,例如:Imgproc.warpPerspective(src, quad, homography, quad.size(),WARP_INVERSE_MAP);
。
我没有完全使用您的代码,而是仅使用了单应性之后的部分,而且我已经看到图像在镜子中变形了,而不是我们想要的(使用更大的显示图像来确切地看到其中的内容)。实际上,在您发布的带有10张卡片的页面上,该标记用于该标记,很抱歉,我没有想到过提这个。