c++ - OpenCV - warpPerspective -


i'm trying use function "warpperspective" opencv 3.0. i'm using example:

http://answers.opencv.org/question/98110/how-do-i-stitch-images-with-two-different-angles/

i have create roi on right side of first image , 1 on left side of second image. use orb extract , compute descriptions , match these ones. didn't changed of original code. roi.

the problem every image try warp perspective comes out this:

warped image

i tried multiple pairs of images , problem persists.

#include "opencv2/opencv.hpp" #include <iostream> #include <fstream> #include <ctype.h>  using namespace cv; using namespace std;   int main(int argc, char* argv[]) {  mat img1 = imread("image2.jpg"); mat img2 = imread("image1.jpg"); namedwindow("i2", window_normal); namedwindow("i1", window_normal);  ptr<orb> o1 = orb::create(); ptr<orb> o2 = orb::create(); vector<keypoint> pts1, pts2; mat desc1, desc2; vector<dmatch> matches;  size s = img1.size(); size s2 = img2.size();  rect r1(s.width - 200, 0, 200, s.height); //rectangle(img1, r1, scalar(255, 0, 0), 5); rect r2(0, 0, 200, s2.height); //rectangle(img2, r2, scalar(255, 0, 0), 5); mat mask1 = mat::zeros(img1.size(), cv_8uc1); mat mask2 = mat::zeros(img1.size(), cv_8uc1); mask1(r1) = 1; mask2(r2) = 1; o1->detectandcompute(img1, mask1, pts1, desc1); o2->detectandcompute(img2, mask2, pts2, desc2); bfmatcher descriptormatcher(norm_hamming, true);  descriptormatcher.match(desc1, desc2, matches, mat()); // keep best matches have nice drawing. // sort distance between descriptor matches mat index; int nbmatch = int(matches.size()); mat tab(nbmatch, 1, cv_32f); (int = 0; i<nbmatch / 2; i++) {     tab.at<float>(i, 0) = matches[i].distance; } sortidx(tab, index, sort_every_column + sort_ascending); vector<dmatch> bestmatches; vector<point2f> src, dst; (int = 0; < nbmatch / 2; i++) {     int j = index.at<int>(i, 0);     cout << pts1[matches[j].queryidx].pt << "\t" << pts2[matches[j].trainidx].pt << "\n";     src.push_back(pts1[matches[j].queryidx].pt + point2f(0, img1.rows)); // necessary offset      dst.push_back(pts2[matches[j].trainidx].pt); } cout << "\n"; mat h = findhomography(src, dst, ransac); mat result; cout << h << endl;  warpperspective(img2, result, h.inv(), size(3 * img2.cols + img1.cols, 2 * img2.rows + img1.rows));  imshow("i1", img1); imshow("i2", img2);  mat roi1(result, rect(0, img1.rows, img1.cols, img1.rows)); img1.copyto(roi1); namedwindow("i3", window_normal); imshow("i3", result); imwrite("result.jpg", result); waitkey(); return 0; 

does comes bad matches? missing something? since i'm kind of new topic, or ideas appreciated.

here's quick things need check when warp perspective not working-

  1. did select right points in both images ? reason: need choose same points correspond each other when finding perspective transform. unrelated points ruin it.

  2. are points in right order in array ? r: need put them in right corresponding order in both source , destination before passing findhomography.

  3. are passing in right order findhomography ? try switching in case not sure. doesn't reverse warp it

those mistakes did when first used it. if see images, there's little part overlapping in both images. need more careful on there. rect mask might fault.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -