I followed Panorama – Image Stitching in OpenCV to do same task on Android,by using openCV4Android
.My problem is in about warping image by founded homography matrix.I use this code to find homography matrix and warp image:
Mat gray1 = //image1 converted to gray
Mat gray2 = //image2 converted to gray
MatOfDMatch matches = new MatOfDMatch();
MatOfDMatch gm = new MatOfDMatch();
LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
LinkedList<Point> objList = new LinkedList<Point>();
LinkedList<Point> sceneList = new LinkedList<Point>();
MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene = new MatOfKeyPoint();
Mat descriptors_object = new Mat();
Mat descriptors_scene = new Mat();
MatOfPoint2f obj = new MatOfPoint2f();
MatOfPoint2f scene = new MatOfPoint2f();
FeatureDetector fd = FeatureDetector.create(FeatureDetector.ORB);
fd.detect(Mat1, keypoints_object);
fd.detect(Mat2, keypoints_scene);
// – Step 2: Calculate descriptors (feature vectors)
DescriptorExtractor extractor = DescriptorExtractor
.create(DescriptorExtractor.ORB);
extractor.compute(Mat1, keypoints_object, descriptors_object);
extractor.compute(Mat2, keypoints_scene, descriptors_scene);
DescriptorMatcher matcher = DescriptorMatcher
.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
matcher.match(descriptors_object, descriptors_scene, matches);
double max_dist = 0;
double min_dist = 100;
List<DMatch> matchesList = matches.toList();
// – Quick calculation of max and min distances between keypoints
for (int i = 0; i < descriptors_object.rows(); i++) {
Double dist = (double) matchesList.get(i).distance;
if (dist < min_dist)
min_dist = dist;
if (dist > max_dist)
max_dist = dist;
}
for (int i = 0; i < descriptors_object.rows(); i++) {
if (matchesList.get(i).distance <= 3 * min_dist) {
good_matches.addLast(matchesList.get(i));
}
}
gm.fromList(good_matches);
List<KeyPoint> keypoints_objectList = keypoints_object.toList();
List<KeyPoint> keypoints_sceneList = keypoints_scene.toList();
for (int i = 0; i < good_matches.size(); i++) {
objList.addLast(keypoints_objectList.get(good_matches.get(i).queryIdx).pt);
sceneList
.addLast(keypoints_sceneList.get(good_matches.get(i).trainIdx).pt);
}
obj.fromList(objList);
scene.fromList(sceneList);
Mat H = Calib3d.findHomography(obj, scene);
Mat warpimg = new Mat();
Size ims = new Size(Mat1.cols(),Mat1.rows());
Imgproc.warpPerspective(Mat1, warpimg, H, ims);
I tested these images(those are from above linked reference):
image1:
image2:
Result of warping image1 by founded homography is:
You can see that result is good warped but first part of that(left side) is cut! So stitching result was:
And result of above referenced link page,is:
Why warping by homography cut that?Or may be I did some things wrong?