Hi there,
I want to stitich two images that has taken with the angle of around 80 degree. for finding homography matrix I get 4 points manually which are 4 corners of charucho board. I dont know why after wrapperspective the half of my image gets reverse and taken a parted.
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
// Global variables to store selected points
std::vector<cv::Point2f> points1, points2;
cv::Mat image1, image2,resize_image1,resize_image2;
int point_index = 0;
double scale_factor=0.5;
// Mouse callback function for selecting points
void onMouse(int event, int x, int y, int flags, void* userdata) {
if (event == cv::EVENT_LBUTTONDOWN) {
if (point_index % 2 == 0) {
points1.push_back(cv::Point2f(x, y));
cv::circle(resize_image1, cv::Point(x, y), 3, cv::Scalar(0, 255, 0), -1);
cv::imshow("Image 1", resize_image1);
} else {
points2.push_back(cv::Point2f(x, y));
cv::circle(resize_image2, cv::Point(x, y), 3, cv::Scalar(0, 255, 0), -1);
cv::imshow("Image 2", resize_image2);
}
point_index++;
}
}
int main() {
// Read the images
image1 = cv::imread("image/2/1.png");
image2 = cv::imread("image/2/2.png");
if (image1.empty() || image2.empty()) {
std::cerr << "Error: One or both images not found!" << std::endl;
return -1;
}
//resize images
cv::resize(image1,resize_image1,cv::Size(),scale_factor,scale_factor);
cv::resize(image2,resize_image2,cv::Size(),scale_factor,scale_factor);
// Create windows and set mouse callback for selecting points
cv::namedWindow("Image 1");
cv::namedWindow("Image 2");
cv::setMouseCallback("Image 1", onMouse);
cv::setMouseCallback("Image 2", onMouse);
// Display images and wait for user to select points
cv::imshow("Image 1", resize_image1);
cv::imshow("Image 2", resize_image2);
cv::waitKey(0);
//Draw matches
std::vector<cv::KeyPoint> keypoints1, keypoints2;
std::vector<cv::DMatch> matches;
for (size_t i = 0; i < points1.size(); ++i) {
keypoints1.push_back(cv::KeyPoint(points1[i], 1.0));
keypoints2.push_back(cv::KeyPoint(points2[i], 1.0));
matches.push_back(cv::DMatch(i, i, 0));
}
cv::Mat matches_image;
cv::drawMatches(resize_image1, keypoints1, resize_image2, keypoints2, matches, matches_image);
cv::imshow("Matches", matches_image);
cv::waitKey(0);
// Calculate homography using the selected points
// cv::Mat homography = calculateHomography(points1, points2);
cv::Mat homography = getPerspectiveTransform(points1, points2);
if (homography.empty()) {
std::cerr << "Error: Homography calculation failed!" << std::endl;
return -1;
}
// Warp image2 to the perspective of image1
cv::Mat warped_image2;
cv::warpPerspective(resize_image2, warped_image2, homography, cv::Size(resize_image1.cols + resize_image2.cols, resize_image1.rows));
cv::imshow("Stitched Image", warped_image2);
cv::waitKey(0);
// Copy image1 to the stitched canvas
cv::Mat stitched_image = cv::Mat::zeros(cv::Size(resize_image1.cols + resize_image2.cols, resize_image1.rows), resize_image1.type());
image1.copyTo(stitched_image(cv::Rect(0, 0, resize_image1.cols, resize_image1.rows)));
warped_image2.copyTo(stitched_image(cv::Rect(0, 0, warped_image2.cols, warped_image2.rows)));
// Display the stitched image
cv::imshow("Stitched Image", stitched_image);
cv::waitKey(0);
return 0;
}