[Github Result]
OutputArray pano = cv::Mat
Stitcher::Status Stitcher::stitch(InputArrayOfArrays images, const std::vector<std::vector<Rect> > &rois, OutputArray pano)
{
Status status = estimateTransform(images, rois);
if (status != OK)
return status;
return composePanorama(pano);
}
다음과 같은 opencv library들을 포함: imgcodecs, highgui, stitching
#include <iostream>
#include <fstream>
// stitching related
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching.hpp"
int main()
{
// Define mode for stitching as panorama
cv::Stitcher::Mode mode = cv::Stitcher::PANORAMA;
// Road images
cv::Mat left = cv::imread("/home/sj/iros2022/image/left.jpg", 1);
cv::Mat front = cv::imread("/home/sj/iros2022/image/front.jpg", 1);
cv::Mat right = cv::imread("/home/sj/iros2022/image/right.jpg", 1);
// Check image read correctly
if(left.cols != 0)
std::cout << "Correctly read left image !!" << std::endl;
if(front.cols != 0)
std::cout << "Correctly read front image !!" << std::endl;
if(right.cols != 0)
std::cout << "Correctly read right image !!" << std::endl;
// Combine three images in one vector
std::vector<cv::Mat> imgs;
imgs.push_back(left);
imgs.push_back(front);
imgs.push_back(right);
// Initialization output image
cv::Mat panorama;
// Add Masking
std::vector<std::vector<cv::Rect>> masks;
std::vector<cv::Rect> mask1;
std::vector<cv::Rect> mask2;
std::vector<cv::Rect> mask3;
// Define cv::Rect what part we focus on !!
cv::Rect left_rect(int(left.cols/2), 0, int(left.cols/2) , left.rows);
cv::Rect front_rect(0, 0, front.cols, front.rows);
cv::Rect right_rect(0, 0, int(right.cols/2), right.rows);
// Set the desired RoI for each image
mask1.push_back(left_rect);
mask2.push_back(front_rect);
mask3.push_back(right_rect);
masks.push_back(mask1);
masks.push_back(mask2);
masks.push_back(mask3);
// Make panorama image using cv::Stitcher adding masks
cv::Ptr<cv::Stitcher> stitcher = cv::Stitcher::create(mode, false);
cv::Stitcher::Status status = stitcher->stitch(imgs, masks, panorama);
// Write and show result image
cv::imwrite("/home/sj/iros2022/image/result.jpg", panorama);
cv::imshow("Result", panorama);
// Wait image not auto cancel
cv::waitKey(0);
return 0;
}
C++을 사용하였고 3개의 라이브러리를 포함하고 있으므로 stitching 이라는 이름을 가진 exeutive file을 만들어줌
g++ stitching.cpp -L/usr/local/include/opencv2 -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -lopencv_stitching -o stitching
해당 파일에 다음과 같이 작성해주면 컴파일한 코드를 빌드 및 실행
# Example of current path: /home/sj/iros2022/src/Stitching
./stitching