with two images
int main(int argc, char** argv)
{
Mat img1 = imread(mosaic_3.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = imread("mosaic_2.jpg", CV_LOAD_IMAGE_GRAYSCALE);
std::vector<cv::KeyPoint> kOrb1; /*<! Point clef de ORB */
std::vector<cv::KeyPoint> kOrb2; /*<! Point clef de ORB */
cv::Mat descORB1; /*<! Descripteur associé à l'un des points clé ORB*/
cv::Mat descORB2; /*<! Descripteur associé à l'un des points clé ORB*/
vector<std::vector<cv::DMatch> > matches; /*<! Descripteur appariés */
cv::Ptr<cv::Feature2D> b;
b = cv::ORB::create(2000);
b->detectAndCompute(img1, Mat(), kOrb1, descORB1);
b->detectAndCompute(img2, Mat(), kOrb2, descORB2);
cv::Ptr<cv::DescriptorMatcher> descriptorMatcher = cv::DescriptorMatcher::create("BruteForce");
matches.clear();
descriptorMatcher->knnMatch(descORB1, descORB2, matches, 1, Mat());
vector<Point> p, q;
Point2f u;
int nb = 0;
double seuil = 0;
for (int i = 0; i<matches.size(); i++)
seuil += matches[i][0].distance;
seuil = seuil / matches.size()*0.5;
double d = 0;
for (int i = 0; i<matches.size(); i++)
{
if (matches[i][0].distance<seuil)
{
u += kOrb1[matches[i][0].queryIdx].pt - kOrb2[matches[i][0].trainIdx].pt;
p.push_back(kOrb1[matches[i][0].queryIdx].pt);
q.push_back(kOrb2[matches[i][0].trainIdx].pt);
nb++;
if (nb>1)
d += norm(p[nb - 1] - p[nb - 2]) / norm(q[nb - 1] - q[nb - 2]);
}
}
d = d / (nb - 1);
Mat img;
resize(img2, img, Size(0, 0), 1, 1);
kOrb2.clear();
Mat descORB;
b->detectAndCompute(img, Mat(), kOrb2, descORB);
matches.clear();
descriptorMatcher->knnMatch(descORB1, descORB, matches, 1, Mat());
p.clear(); q.clear();
seuil = 0;
for (int i = 0; i<matches.size(); i++)
seuil += matches[i][0].distance;
nb = 0;
seuil = seuil / matches.size()*0.5;
d = 0;
for (int i = 0; i<matches.size(); i++)
{
if (matches[i][0].distance<seuil)
{
p.push_back(kOrb1[matches[i][0].queryIdx].pt);
q.push_back(kOrb2[matches[i][0].trainIdx].pt);
nb++;
if (nb>1)
d += norm(p[nb - 1] - p[nb - 2]) / norm(q[nb - 1] - q[nb - 2]);
}
}
Mat_<double> v = estimateRigidTransform(p, q, false);// q=v*p; img2=v*img1
Mat_<double> h(3, 3);
h = Mat::eye(2, 3,CV_64F);
Mat id = Mat::eye(3, 3, CV_64F);
for (int i = 0; i < v.rows; i++)
for (int j = 0; i < v.cols; i++)
h(j, i) = v(j, i);
cout << v << endl;
Mat res1((img1.rows + img2.rows), (img1.cols + img2.cols), img1.type());
Mat res2((img1.rows + img2.rows), (img1.cols + img2.cols), img1.type());
// warpPerspective(img1, img1, id, Size(res.cols, res.rows), WARP_INVERSE_MAP);
warpAffine(img, res2, v, res2.size(), WARP_INVERSE_MAP);
warpAffine(img1, res1, h,res1.size(), WARP_INVERSE_MAP);
imshow("Res", max(res1,res2));
imshow("img1", img1);
imshow("img2", img2);
waitKey();
return 0;
}