In an effort to produce an image stitching program, I have used this tutorial in order to write the following program:
void stitchImages(Mat imageOne, Mat imageTwo) {
namedWindow("Result", WINDOW_AUTOSIZE);
Mat firstImageDescriptors, secondImageDescriptors;
Mat imageOneGray, imageTwoGray;
cvtColor(imageOne, imageOneGray, CV_RGB2GRAY);
cvtColor(imageTwo, imageTwoGray, CV_RGB2GRAY);
cv::Ptr<Feature2D> featureDetector = xfeatures2d::SURF::create();
vector <KeyPoint> firstImageKeyPoints, secondImageKeyPoints;
cout << "About to detect" << endl;
// featureDetector->detect(imageOneGray, firstImageKeyPoints);
// featureDetector->detect(imageTwoGray, secondImageKeyPoints);
DescriptorExtractor descriptorExtractor;
cout << "About to extract" << endl;
// descriptorExtractor.compute(imageOneGray, firstImageKeyPoints, firstImageDescriptors);
// descriptorExtractor.compute(imageTwoGray, secondImageKeyPoints, secondImageDescriptors);
featureDetector->detectAndCompute(imageOne, Mat(), firstImageKeyPoints, firstImageDescriptors, false);
featureDetector->detectAndCompute(imageTwo, Mat(), secondImageKeyPoints, secondImageDescriptors, false);
cout << "About to Flann it up" << endl;
FlannBasedMatcher matcher;
vector <DMatch> matches;
matcher.match(firstImageDescriptors, secondImageDescriptors, matches);
double max_dist = 0; double min_dist = 100;
for(int i = 0; i < imageOneGray.rows; i++) {
double dist = matches[i].distance;
if(dist < min_dist) min_dist = dist;
if(dist > max_dist) max_dist = dist;
}
vector<DMatch> good_matches;
for(int j = 0; j < imageOneGray.rows; j++) {
if(matches[j].distance < 4*min_dist)
good_matches.push_back(matches[j]);
}
Mat img_matches;
//drawMatches(imageOne, firstImageKeyPoints, imageTwo, secondImageKeyPoints, good_matches, img_matches);
// imshow("Result", img_matches);
// waitKey(0);
vector <Point2f> imageOnePoints;
vector <Point2f> imageTwoPoints;
for(int k = 0; k < good_matches.size(); k++) {
imageOnePoints.push_back(firstImageKeyPoints[good_matches[k].queryIdx].pt);
imageTwoPoints.push_back(secondImageKeyPoints[good_matches[k].trainIdx].pt);
}
Mat homographyMatrix = findHomography(imageOnePoints, imageTwoPoints, 8);
Mat finalImage;
warpPerspective(imageOne, finalImage, homographyMatrix, Size(imageOne.cols + imageTwo.cols, imageOne.rows));
imshow("Result", finalImage);
waitKey(0);
Mat half(finalImage, Rect(0, 0, imageTwo.cols, imageTwo.rows));
imageTwo.copyTo(half);
imshow("Result", finalImage);
waitKey(0);
//set work_megapix
//set work_scale
}
(Please ignore the commented out code and notes to self)
I am using OpenCV 3.0 while this one uses a version of 2.0 to the best of my knowledge, so I had to change some things. I originally tried to stitch to images together, but the program failed. I was finding a decent amount of matches (I checked), but for some reason the transformation using the homography matrix I feel is the most likely culprit. I thought to myself, "this tutorials seems to be for panoramas without camera rotation, and the images I took are meant for a rotated panorama, so I might as well try taking some images where the camera does not rotate". I tried to do so and the results ended up with one image being copied to one half of the final pano and the other half of the pano being a gray smear. At least when I messed around with the rotated images, the first half of the image looked like a rotated version of the first image, yet the part that would have made it a panorama and not a copy of the second image's left part was cut-off, so I am not sure if it was just part of the second image (I feel it was probably legitimately part of the first). I am sorry I cannot post images, the image files for the final panoramas are too big. Any help would be much appreciated though. Thank you,
- Jacob
P.S. I had to insert my code as a javascript/css/html code snippet so it would format correctly, but it obviously won't run in the browser so don't try! Also, I know I have two imshows that are not commented out at the end. I have two to see the final image looks before and after the "second image" is copied to the panorama.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire