使用liner、feather、multiband对已经拼接的数据进行融合(下)

简介: 理解mulitband。所谓的mulitband,其实就是一种多尺度的样条融合,其实现的主要方法就是laplace金字塔。高斯金字塔是向下采样,而laplace金字塔式向上采样(也就是恢复),采用的都是差值的方法。
理解mulitband。所谓的mulitband,其实就是一种多尺度的样条融合,其实现的主要方法就是laplace金字塔。
高斯金字塔是向下采样,而laplace金字塔式向上采样(也就是恢复),采用的都是差值的方法。如何能够在金字塔各个层次上面进行图像的融合,结果证明是相当不错的。网络上面流传的一个类解释了这个问题,并且能够拿来用:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
using namespace cv;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define DllExport _declspec (dllexport)

/*
1.设计一个mask(一半全1,一半全0),并计算level层的gaussion_mask[i];
2.计算两幅图像每一层的Laplacian[i],并与gaussion_mask[i]相乘,合成一幅result_lapacian[i];
3.对两幅图像不断求prydown,并把最高层保存在gaussion[i],与gaussion_mask[i]相乘,合成一幅result_gaussion;
4,对result_gaussion不断求pryup,每一层都与result_lapacian[i]合成,最后得到原-图像大小的融合图像。
*/

class LaplacianBlending { 
private: 
                Mat_<Vec3f> top; 
                Mat_<Vec3f> down; 
                Mat_< float> blendMask; 

                vector<Mat_<Vec3f> > topLapPyr,downLapPyr,resultLapPyr; //Laplacian Pyramids  
                Mat topHighestLevel, downHighestLevel, resultHighestLevel; 
                vector<Mat_<Vec3f> > maskGaussianPyramid; //masks are 3-channels for easier multiplication with RGB  

                 int levels; 

                 //创建金字塔
                 void buildPyramids() { 
                                buildLaplacianPyramid(top,topLapPyr,topHighestLevel);  
                                buildLaplacianPyramid(down,downLapPyr,downHighestLevel); 
                                buildGaussianPyramid(); 
                } 
                 //创建gauss金字塔
                 void buildGaussianPyramid() {//金字塔内容Y为a每一层的掩模  
                                assert(topLapPyr.size()>0); 

                                maskGaussianPyramid.clear(); 
                                Mat currentImg; 
                                 //blendMask就是掩码
                                cvtColor(blendMask, currentImg, CV_GRAY2BGR); //store color img of blend mask into maskGaussianPyramid  
                                maskGaussianPyramid.push_back(currentImg); //0-level  

                                currentImg = blendMask; 
                                 for (int l=1; l<levels+1; l++) { 
                                                Mat _down; 
                                                 if (topLapPyr.size() > l) 
                                                                pyrDown(currentImg, _down, topLapPyr[l].size()); 
                                                 else 
                                                                pyrDown(currentImg, _down, topHighestLevel.size()); //lowest level  

                                                Mat down; 
                                                cvtColor(_down, down, CV_GRAY2BGR); 
                                                maskGaussianPyramid.push_back(down); //add color blend mask into mask Pyramid  
                                                currentImg = _down; 
                                } 
                } 

                 //创建laplacian金字塔
                 void buildLaplacianPyramid(const Mat& img, vector<Mat_<Vec3f> >& lapPyr, Mat& HighestLevel) { 
                                lapPyr.clear(); 
                                Mat currentImg = img; 
                                 for (int l=0; l<levels; l++) { 
                                                Mat down,up; 
                                                pyrDown(currentImg, down); 
                                                pyrUp(down, up,currentImg.size()); 
                                                Mat lap = currentImg - up;  //存储的就是残D差
                                                lapPyr.push_back(lap); 
                                                currentImg = down; 
                                } 
                                currentImg.copyTo(HighestLevel); 
                } 

                Mat_<Vec3f> reconstructImgFromLapPyramid() { 
                                 //将左右laplacian图像拼成的resultLapPyr金字塔中每一层  
                                 //从上到下插值放大并相加,即得blend图像结果  
                                Mat currentImg = resultHighestLevel; 
                                 for (int l=levels-1; l>=0; l--) { 
                                                Mat up; 
                                                pyrUp(currentImg, up, resultLapPyr[l].size()); 
                                                currentImg = up + resultLapPyr[l]; 
                                } 
                                 return currentImg; 
                } 

                 void blendLapPyrs() { 
                                 //获得每层金字塔中直接用左右两图Laplacian变换拼成的图像
                                 //一半的一半就是在这个地方计算的。 是基于掩模的方式进行的.
                                resultHighestLevel = topHighestLevel.mul(maskGaussianPyramid.back()) + 
                                                downHighestLevel.mul(Scalar(1.0,1.0,1.0) - maskGaussianPyramid.back()); 
                                 for (int l=0; l<levels; l++) { 
                                                Mat A = topLapPyr[l].mul(maskGaussianPyramid[l]); 
                                                Mat antiMask = Scalar(1.0,1.0,1.0) - maskGaussianPyramid[l]; 
                                                Mat B = downLapPyr[l].mul(antiMask); 
                                                Mat_<Vec3f> blendedLevel = A + B; 
                                                resultLapPyr.push_back(blendedLevel); 
                                } 
                } 

public: 
                LaplacianBlending( const Mat_<Vec3f>& _top, const Mat_<Vec3f>& _down, const Mat_< float>& _blendMask, int _levels)://缺省数y据Y,使1用 LaplacianBlending lb(l,r,m,4);  
                  top(_top),down(_down),blendMask(_blendMask),levels(_levels) 
                  { 
                                  assert(_top.size() == _down.size()); 
                                  assert(_top.size() == _blendMask.size()); 
                                  buildPyramids();  //创建laplacian金字塔和gauss金字塔
                                  blendLapPyrs();   //将左右金字塔融合成为a一个图片  
                  }; 

                  Mat_<Vec3f> blend() { 
                                  return reconstructImgFromLapPyramid();//reconstruct Image from Laplacian Pyramid  
                  } 
}; 

Mat_<Vec3f> LaplacianBlend( const Mat_<Vec3f>& t, const Mat_<Vec3f>& d, const Mat_< float>& m) { 
                LaplacianBlending lb(t,d,m,4); 
                 return lb.blend(); 
} 


DllExport double aValue =1.5;
DllExport int dlladd()
{
                 return 5;
}
DllExport int dlladd( int a,int b)
{
                 return a+b;
}
DllExport cv::Mat imagetest()
{
                cv::Mat image1= cv::imread( "C:\\apple.png",1);
                cv::Mat image2= cv::imread( "C:\\orange.png",1);

                Mat_<Vec3f> t; image1.convertTo(t,CV_32F,1.0/255.0); //Vec3f表示有三个通道,即 [row][column][depth]  
                Mat_<Vec3f> d; image2.convertTo(d,CV_32F,1.0/255.0); 

                Mat_< float> m(t.rows,d.cols,0.0);                 //将m全部赋3值为a0  
                 //m(Range::all(),Range(0,m.cols/2)) = 1.0;    //原来初始的掩码是在这里
                m(Range(0,m.rows/2),Range::all())=1.0;
                Mat_<Vec3f> blend = LaplacianBlend(t,d, m); 

                imshow( "blended",blend); 
                 return blend;
}
需要注意的是, m(Range(0,m.rows/2),Range::all())=1.0表明了原始图像的掩码,这个掩码就是那个分界的地方。
比如比如:
img_07600e8f4c8f5b174408b45db545d7c9.png ;
img_e1045e2b337d038fb21c4492ba9c4bd7.png
永达实际的项目上面,应该是这样。
在使用这种方法进行大规模拼接的时候,主要是两个问题
一个是效果,在上面的那个橘子苹果的例子中,只有前景的颜色有变化,实际上其它几个地方色彩亮度变化不是很大。但是对于实际情况下的拼接来说,亮度有变化,比较难以处理。
img_e5b903531a89b92d242b376ea0222184.jpe
二是效率。laplacian需要大量的过程,造成结果内存的需求很大,一时半会很难优化好。multband看来只能够在符合要求的简单拼接中去实现使用。




目前方向:图像拼接融合、图像识别 联系方式:jsxyhelu@foxmail.com
目录
相关文章
|
前端开发 JavaScript
【Ant Design Pro】使用ant design pro做为你的开发模板(一)拉取项目
【Ant Design Pro】使用ant design pro做为你的开发模板(一)拉取项目
2038 0
【Ant Design Pro】使用ant design pro做为你的开发模板(一)拉取项目
|
机器学习/深度学习 数据挖掘
R实战|从文献入手谈谈logistic回归、Cox回归以及Lasso分析(一)
R实战|从文献入手谈谈logistic回归、Cox回归以及Lasso分析(一)
2242 0
|
5月前
|
数据采集 JSON 数据挖掘
淘宝商品评论 API(taobao.item.reviews.get)完整介绍与标准 JSON 返回示例
淘宝商品评论 API(taobao.item.reviews.get)完整介绍与标准 JSON 返回示例
|
10月前
|
缓存 NoSQL Java
JAVA面试题速记-redis知识点
Redis核心简介(240字内): Redis提供5种基础数据结构:String、Hash、List、Set、ZSet,及Geospatial等扩展类型。支持RDB快照与AOF日志双持久化机制,兼顾性能与安全;通过过期策略(定期+惰性+LRU)管理内存。应对缓存击穿/雪崩,采用错峰过期;保障缓存-数据库一致性,推荐异步Binlog监听+可靠MQ删除。分布式锁推荐Redisson(自动续期、原子Lua脚本)。高可用支持哨兵(主从故障转移)与集群(16384槽分片、水平扩展)。BigKey需拆分、异步删除(UNLINK)、lazy-free优化。
642 131
|
5月前
|
存储 弹性计算 运维
阿里云账号:服务器轻量应用服务器和ECS有什么区别!
本文由阿里云官方授权代理商零度云撰写,用通俗比喻与五大维度对比,深度解析轻量应用服务器(精装公寓)与云服务器ECS(毛坯别墅)的本质区别:定位、带宽计费、网络隔离、存储可靠性及运维门槛。附真实场景选型指南与3步决策树,助开发者、创客及中小企业按需选择,避免踩坑。
|
自然语言处理 安全 API
2025阿里云双11特惠:文本短信低至 0.02 元/条,短信认证套餐包3.99元/年
2025年阿里云双11金秋云创季开启!短信认证套餐包新人专享7折,免资质签名即用即发;短信服务新客低至0.02元/条,老客最高减5850元,验证短信秒级触达率99%。
1498 1
|
8月前
|
JSON 前端开发 JavaScript
前端组件库 ——FormMaking 知识点大全(一)
教程来源 http://qcycj.cn FormMaking是一款基于Vue的可视化表单设计器,支持拖拽设计、JSON导出与动态渲染,实现“设计即代码”。具备丰富组件、灵活验证、多端适配及低代码扩展能力,广泛应用于OA、考试、流程系统等企业场景。
|
11月前
|
JSON API 数据格式
实战解析:如何高效调用采招网关键词搜索API获取招标信息
本文详解采招网关键词搜索API的调用方法:支持按关键词、地区、类型等参数检索招标/中标公告,返回JSON格式数据;提供Python调用示例、分页处理、错误应对及安全实践,助力开发者快速构建招投标监控、竞对分析等应用。(239字)
|
安全 Linux 网络安全
【Windows】搭建Emby媒体库服务器,实现无公网IP远程访问
【Windows】搭建Emby媒体库服务器,实现无公网IP远程访问
2274 0
|
存储 安全 JavaScript
【XSS】XSS漏洞详细指南
【XSS】XSS漏洞详细指南
1084 3

热门文章

最新文章