opencv多幅图像拼接算法(python 图像拼接)
先来看看OpenCV官方的例子得到效果是非常的好,输入的images如下:
效果:
#Stitcher类与detail命名空间
OpenCV提供了高级别的函数封装在Stitcher类中,使用很方便,不用考虑太多的细节。
低级别函数封装在detail命名空间中,展示了OpenCV算法实现的很多步骤和细节,使熟悉如下拼接流水线的用户,方便自己定制。
可见OpenCV图像拼接模块的实现是十分精密和复杂的,拼接的结果很完善,但同时也是费时的,完全不能够实现实时应用。
我在研究detail源码时,由于水平有限,并不能自由灵活地对各种部件取其所需,取舍随意。
官方提供的stitching和stitching_detailed使用示例,分别是高级别和低级别封装这两种方式正确地使用示例。两种结果产生的拼接结果相同,后者却可以允许用户,在参数变量初始化时,选择各项算法。如下所示:
这涉及到以下算法流程:
命令行调用程序,输入源图像以及程序的参数
特征点检测,判断是使用surf还是orb,默认是surf。
对图像的特征点进行匹配,使用最近邻和次近邻方法,
将两个最优的匹配的置信度保存下来。
对图像进行排序以及将置信度高的图像保存到同一个集合中,
删除置信度比较低的图像间的匹配,得到能正确匹配的图像序列。
这样将置信度高于门限的所有匹配合并到一个集合中。
对所有图像进行相机参数粗略估计,然后求出旋转矩阵
使用光束平均法进一步精准的估计出旋转矩阵。
波形校正,水平或者垂直
拼接
融合,多频段融合,光照补偿。
另外在拼接的时候可以设置不同warper,这样会对拼接之后的图像生成不同效果,常见的效果包括
- 鱼眼相机
- 环视(平面曲翘)
- 默认
如下图所示:
代码演示:
#include#includeusing namespace cv; using namespace std; int main(int argc, char** argv) { vectorfiles; glob("D:/images/zsxq/1", files); vectorimages; for (int i = 0; i < files.size(); i ) { printf("image file : %s \n", files[i].c_str()); images.push_back(imread(files[i])); } // 设置拼接模式与参数 Mat result1, result2, result3; Stitcher::Mode mode = Stitcher::PANORAMA; Ptrstitcher = Stitcher::create(mode); // 拼接方式-多通道融合 auto blender = detail::Blender::createDefault(detail::Blender::MULTI_BAND); stitcher->setBlender(blender); // 拼接 Stitcher::Status status = stitcher->stitch(images, result1); // 平面曲翘拼接 auto plane_warper = makePtr(); stitcher->setWarper(plane_warper); status = stitcher->stitch(images, result2); // 鱼眼拼接 auto fisheye_warper = makePtr(); stitcher->setWarper(fisheye_warper); status = stitcher->stitch(images, result3); // 检查返回 if (status != Stitcher::OK) { cout << "Can't stitch images, error code = " << int(status) << endl; return EXIT_FAILURE; } imwrite("D:/result1.png", result1); imwrite("D:/result2.png", result2); imwrite("D:/result3.png", result3); waitKey(0); return 0; }
在来看一组输入4张图像,每张分辨率为327*245,总的拼接时间为9.25s。
演示代码:
#include#include#include#include "opencv2/opencv_modules.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/stitching/detail/autocalib.hpp" #include "opencv2/stitching/detail/blenders.hpp" #include "opencv2/stitching/detail/camera.hpp" #include "opencv2/stitching/detail/exposure_compensate.hpp" #include "opencv2/stitching/detail/matchers.hpp" #include "opencv2/stitching/detail/motion_estimators.hpp" #include "opencv2/stitching/detail/seam_finders.hpp" #include "opencv2/stitching/detail/util.hpp" #include "opencv2/stitching/detail/warpers.hpp" #include "opencv2/stitching/warpers.hpp" using namespace std; using namespace cv; using namespace cv::detail; // #define ENABLE_LOG 1 // Default command line args vectorimg_names; bool preview = false; bool try_gpu = true; double work_megapix = 0.6; double seam_megapix = 0.1; double compose_megapix = -1; float conf_thresh = 1.f; string features_type = "surf"; string ba_cost_func = "ray"; string ba_refine_mask = "xxxxx"; bool do_wave_correct = true; WaveCorrectKind wave_correct = detail::WAVE_CORRECT_HORIZ; bool save_graph = false; std::string save_graph_to; string warp_type = "spherical"; int expos_comp_type = ExposureCompensator::GAIN_BLOCKS; float match_conf = 0.3f; string seam_find_type = "gc_color"; int blend_type = Blender::MULTI_BAND; float blend_strength = 5; string result_name = "result.jpg"; int main(int argc, char* argv[]) { //读入图像 double ttt = getTickCount(); img_names.push_back("E:/workspace/iamge/dataset/yard1.jpg"); img_names.push_back("E:/workspace/iamge/dataset/yard2.jpg"); img_names.push_back("E:/workspace/iamge/dataset/yard3.jpg"); img_names.push_back("E:/workspace/iamge/dataset/yard4.jpg"); #if ENABLE_LOG int64 app_start_time = getTickCount(); #endif cv::setBreakOnError(true); /*int retval = parseCmdArgs(argc, argv); if (retval) return retval;*/ // Check if have enough images int num_images = static_cast(img_names.size()); if (num_images < 2) { LOGLN("Need more images"); return -1; } double work_scale = 1, seam_scale = 1, compose_scale = 1; bool is_work_scale_set = false, is_seam_scale_set = false, is_compose_scale_set = false; LOGLN("Finding features..."); #if ENABLE_LOG int64 t = getTickCount(); #endif Ptrfinder; if (features_type == "surf") { #if defined(HAVE_OPENCV_NONFREE) && defined(HAVE_OPENCV_GPU) if (try_gpu && gpu::getCudaEnabledDeviceCount() > 0) finder = new SurfFeaturesFinderGpu(); else #endif finder = new SurfFeaturesFinder(); } else if (features_type == "orb") { finder = new OrbFeaturesFinder(); } else { cout << "Unknown 2D features type: '" << features_type << "'.\n"; return -1; } Mat full_img, img; vectorfeatures(num_images); vectorimages(num_images); vectorfull_img_sizes(num_images); double seam_work_aspect = 1; for (int i = 0; i < num_images; i) { full_img = imread(img_names[i]); full_img_sizes[i] = full_img.size(); if (full_img.empty()) { LOGLN("Can't open image " << img_names[i]); return -1; } if (work_megapix < 0) { img = full_img; work_scale = 1; is_work_scale_set = true; } else { if (!is_work_scale_set) { work_scale = min(1.0, sqrt(work_megapix * 1e6 / full_img.size().area())); is_work_scale_set = true; } resize(full_img, img, Size(), work_scale, work_scale); } if (!is_seam_scale_set) { seam_scale = min(1.0, sqrt(seam_megapix * 1e6 / full_img.size().area())); seam_work_aspect = seam_scale / work_scale; is_seam_scale_set = true; } (*finder)(img, features[i]); features[i].img_idx = i; LOGLN("Features in image #" << i 1 << ": " collectGarbage(); full_img.release(); img.release(); LOGLN("Finding features, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); LOG("Pairwise matching"); #if ENABLE_LOG t = getTickCount(); #endif vectorpairwise_matches; BestOf2NearestMatcher matcher(try_gpu, match_conf); matcher(features, pairwise_matches); matcher.collectGarbage(); LOGLN("Pairwise matching, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); // Check if we should save matches graph if (save_graph) { LOGLN("Saving matches graph..."); ofstream f(save_graph_to.c_str()); f << matchesGraphAsString(img_names, pairwise_matches, conf_thresh); } // Leave only images we are sure are from the same panorama vectorindices = leaveBiggestComponent(features, pairwise_matches, conf_thresh); vectorimg_subset; vectorimg_names_subset; vectorfull_img_sizes_subset; for (size_t i = 0; i < indices.size(); i) { img_names_subset.push_back(img_names[indices[i]]); img_subset.push_back(images[indices[i]]); full_img_sizes_subset.push_back(full_img_sizes[indices[i]]); } images = img_subset; img_names = img_names_subset; full_img_sizes = full_img_sizes_subset; // Check if we still have enough images num_images = static_cast(img_names.size()); if (num_images < 2) { LOGLN("Need more images"); return -1; } HomographyBasedEstimator estimator; vectorcameras; estimator(features, pairwise_matches, cameras); for (size_t i = 0; i < cameras.size(); i) { Mat R; cameras[i].R.convertTo(R, CV_32F); cameras[i].R = R; LOGLN("Initial intrinsics #" << indices[i] 1 << ":\n" << cameras[i].K()); } Ptradjuster; if (ba_cost_func == "reproj") adjuster = new detail::BundleAdjusterReproj(); else if (ba_cost_func == "ray") adjuster = new detail::BundleAdjusterRay(); else { cout << "Unknown bundle adjustment cost function: '" << ba_cost_func setConfThresh(conf_thresh); Mat_refine_mask = Mat::zeros(3, 3, CV_8U); if (ba_refine_mask[0] == 'x') refine_mask(0,0) = 1; if (ba_refine_mask[1] == 'x') refine_mask(0,1) = 1; if (ba_refine_mask[2] == 'x') refine_mask(0,2) = 1; if (ba_refine_mask[3] == 'x') refine_mask(1,1) = 1; if (ba_refine_mask[4] == 'x') refine_mask(1,2) = 1; adjuster->setRefinementMask(refine_mask); (*adjuster)(features, pairwise_matches, cameras); // Find median focal length vectorfocals; for (size_t i = 0; i < cameras.size(); i) { LOGLN("Camera #" << indices[i] 1 << ":\n" << cameras[i].K()); focals.push_back(cameras[i].focal); } sort(focals.begin(), focals.end()); float warped_image_scale; if (focals.size() % 2 == 1) warped_image_scale = static_cast(focals[focals.size() / 2]); else warped_image_scale = static_cast(focals[focals.size() / 2 - 1] focals[focals.size() / 2]) * 0.5f; if (do_wave_correct) { vectorrmats; for (size_t i = 0; i < cameras.size(); i) rmats.push_back(cameras[i].R.clone()); waveCorrect(rmats, wave_correct); for (size_t i = 0; i < cameras.size(); i) cameras[i].R = rmats[i]; } LOGLN("Warping images (auxiliary)... "); #if ENABLE_LOG t = getTickCount(); #endif vectorcorners(num_images); vectormasks_warped(num_images); vectorimages_warped(num_images); vectorsizes(num_images); vectormasks(num_images); // Preapre images masks for (int i = 0; i < num_images; i) { masks[i].create(images[i].size(), CV_8U); masks[i].setTo(Scalar::all(255)); } // Warp images and their masks Ptrwarper_creator; #if defined(HAVE_OPENCV_GPU) if (try_gpu && gpu::getCudaEnabledDeviceCount() > 0) { if (warp_type == "plane") warper_creator = new cv::PlaneWarperGpu(); else if (warp_type == "cylindrical") warper_creator = new cv::CylindricalWarperGpu(); else if (warp_type == "spherical") warper_creator = new cv::SphericalWarperGpu(); } else #endif { if (warp_type == "plane") warper_creator = new cv::PlaneWarper(); else if (warp_type == "cylindrical") warper_creator = new cv::CylindricalWarper(); else if (warp_type == "spherical") warper_creator = new cv::SphericalWarper(); else if (warp_type == "fisheye") warper_creator = new cv::FisheyeWarper(); else if (warp_type == "stereographic") warper_creator = new cv::StereographicWarper(); else if (warp_type == "compressedPlaneA2B1") warper_creator = new cv::CompressedRectilinearWarper(2, 1); else if (warp_type == "compressedPlaneA1.5B1") warper_creator = new cv::CompressedRectilinearWarper(1.5, 1); else if (warp_type == "compressedPlanePortraitA2B1") warper_creator = new cv::CompressedRectilinearPortraitWarper(2, 1); else if (warp_type == "compressedPlanePortraitA1.5B1") warper_creator = new cv::CompressedRectilinearPortraitWarper(1.5, 1); else if (warp_type == "paniniA2B1") warper_creator = new cv::PaniniWarper(2, 1); else if (warp_type == "paniniA1.5B1") warper_creator = new cv::PaniniWarper(1.5, 1); else if (warp_type == "paniniPortraitA2B1") warper_creator = new cv::PaniniPortraitWarper(2, 1); else if (warp_type == "paniniPortraitA1.5B1") warper_creator = new cv::PaniniPortraitWarper(1.5, 1); else if (warp_type == "mercator") warper_creator = new cv::MercatorWarper(); else if (warp_type == "transverseMercator") warper_creator = new cv::TransverseMercatorWarper(); } if (warper_creator.empty()) { cout << "Can't create the following warper '" << warp_type << "'\n"; return 1; } Ptrwarper = warper_creator->create(static_cast(warped_image_scale * seam_work_aspect)); for (int i = 0; i < num_images; i) { Mat_K; cameras[i].K().convertTo(K, CV_32F); float swa = (float)seam_work_aspect; K(0,0) *= swa; K(0,2) *= swa; K(1,1) *= swa; K(1,2) *= swa; corners[i] = warper->warp(images[i], K, cameras[i].R, INTER_LINEAR, BORDER_REFLECT, images_warped[i]); sizes[i] = images_warped[i].size(); warper->warp(masks[i], K, cameras[i].R, INTER_NEAREST, BORDER_CONSTANT, masks_warped[i]); } vectorimages_warped_f(num_images); for (int i = 0; i < num_images; i) images_warped[i].convertTo(images_warped_f[i], CV_32F); LOGLN("Warping images, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); Ptrcompensator = ExposureCompensator::createDefault(expos_comp_type); compensator->feed(corners, images_warped, masks_warped); Ptrseam_finder; if (seam_find_type == "no") seam_finder = new detail::NoSeamFinder(); else if (seam_find_type == "voronoi") seam_finder = new detail::VoronoiSeamFinder(); else if (seam_find_type == "gc_color") { #if defined(HAVE_OPENCV_GPU) if (try_gpu && gpu::getCudaEnabledDeviceCount() > 0) seam_finder = new detail::GraphCutSeamFinderGpu(GraphCutSeamFinderBase::COST_COLOR); else #endif seam_finder = new detail::GraphCutSeamFinder(GraphCutSeamFinderBase::COST_COLOR); } else if (seam_find_type == "gc_colorgrad") { #if defined(HAVE_OPENCV_GPU) if (try_gpu && gpu::getCudaEnabledDeviceCount() > 0) seam_finder = new detail::GraphCutSeamFinderGpu(GraphCutSeamFinderBase::COST_COLOR_GRAD); else #endif seam_finder = new detail::GraphCutSeamFinder(GraphCutSeamFinderBase::COST_COLOR_GRAD); } else if (seam_find_type == "dp_color") seam_finder = new detail::DpSeamFinder(DpSeamFinder::COLOR); else if (seam_find_type == "dp_colorgrad") seam_finder = new detail::DpSeamFinder(DpSeamFinder::COLOR_GRAD); if (seam_finder.empty()) { cout << "Can't create the following seam finder '" << seam_find_type find(images_warped_f, corners, masks_warped); // Release unused memory images.clear(); images_warped.clear(); images_warped_f.clear(); masks.clear(); LOGLN("Compositing..."); #if ENABLE_LOG t = getTickCount(); #endif Mat img_warped, img_warped_s; Mat dilated_mask, seam_mask, mask, mask_warped; Ptrblender; //double compose_seam_aspect = 1; double compose_work_aspect = 1; for (int img_idx = 0; img_idx < num_images; img_idx) { LOGLN("Compositing image #" 0) compose_scale = min(1.0, sqrt(compose_megapix * 1e6 / full_img.size().area())); is_compose_scale_set = true; // Compute relative scales //compose_seam_aspect = compose_scale / seam_scale; compose_work_aspect = compose_scale / work_scale; // Update warped image scale warped_image_scale *= static_cast(compose_work_aspect); warper = warper_creator->create(warped_image_scale); // Update corners and sizes for (int i = 0; i < num_images; i) { // Update intrinsics cameras[i].focal *= compose_work_aspect; cameras[i].ppx *= compose_work_aspect; cameras[i].ppy *= compose_work_aspect; // Update corner and size Size sz = full_img_sizes[i]; if (std::abs(compose_scale - 1) > 1e-1) { sz.width = cvRound(full_img_sizes[i].width * compose_scale); sz.height = cvRound(full_img_sizes[i].height * compose_scale); } Mat K; cameras[i].K().convertTo(K, CV_32F); Rect roi = warper->warpRoi(sz, K, cameras[i].R); corners[i] = roi.tl(); sizes[i] = roi.size(); } } if (abs(compose_scale - 1) > 1e-1) resize(full_img, img, Size(), compose_scale, compose_scale); else img = full_img; full_img.release(); Size img_size = img.size(); Mat K; cameras[img_idx].K().convertTo(K, CV_32F); // Warp the current image warper->warp(img, K, cameras[img_idx].R, INTER_LINEAR, BORDER_REFLECT, img_warped); // Warp the current image mask mask.create(img_size, CV_8U); mask.setTo(Scalar::all(255)); warper->warp(mask, K, cameras[img_idx].R, INTER_NEAREST, BORDER_CONSTANT, mask_warped); // Compensate exposure compensator->apply(img_idx, corners[img_idx], img_warped, mask_warped); img_warped.convertTo(img_warped_s, CV_16S); img_warped.release(); img.release(); mask.release(); dilate(masks_warped[img_idx], dilated_mask, Mat()); resize(dilated_mask, seam_mask, mask_warped.size()); mask_warped = seam_mask & mask_warped; if (blender.empty()) { blender = Blender::createDefault(blend_type, try_gpu); Size dst_sz = resultRoi(corners, sizes).size(); float blend_width = sqrt(static_cast(dst_sz.area())) * blend_strength / 100.f; if (blend_width < 1.f) blender = Blender::createDefault(Blender::NO, try_gpu); else if (blend_type == Blender::MULTI_BAND) { MultiBandBlender* mb = dynamic_cast(static_cast(blender)); mb->setNumBands(static_cast(ceil(log(blend_width)/log(2.)) - 1.)); LOGLN("Multi-band blender, number of bands: " numBands()); } else if (blend_type == Blender::FEATHER) { FeatherBlender* fb = dynamic_cast(static_cast(blender)); fb->setSharpness(1.f/blend_width); LOGLN("Feather blender, sharpness: " sharpness()); } blender->prepare(corners, sizes); } // Blend the current image blender->feed(img_warped_s, mask_warped, corners[img_idx]); } Mat result, result_mask; blender->blend(result, result_mask); LOGLN("Compositing, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); imwrite(result_name, result); result.convertTo(result,CV_8UC1); imshow("stitch",result); ttt = ((double)getTickCount() - ttt) / getTickFrequency(); cout << "总的拼接时间:" << ttt << endl; waitKey(0); LOGLN("Finished, total time: " << ((getTickCount() - app_start_time) / getTickFrequency()) << " sec"); return 0; }
效果:
赞 (0)