图像拉普拉斯金字塔融合(Laplacian Pyramid Blending)

转摘的,修改了下程序,图像融合看不太懂 。。。。http://blog.csdn.net/abcjennifer/article/details/7628655#comments


// 转摘的别人的程序
// #include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <opencv2/opencv.hpp>using namespace cv;// __func__ 和 __FUNCTION__ 一样的意思,描述当前所在的函数
#define ENABLE_DEBUG 1   //else, comment this line
#ifdef ENABLE_DEBUG
#define DEBUG_LOG(fmt,...) fprintf(stderr, "%s:%d: " fmt "\n",   \__FUNCTION__ , __LINE__, ## __VA_ARGS__)
#else
#define DEBUG_LOG(fmt, ...) do {} while (0)
#endif//
// disable stupid warning 4018 and etc...
#pragma warning(push)
#pragma warning(disable:4018)/************************************************************************/
/* 说明:
*金字塔从下到上依次为 [0,1,...,level-1] 层
*blendMask 为图像的掩模
*maskGaussianPyramid为金字塔每一层的掩模
*resultLapPyr 存放每层金字塔中直接用左右两图Laplacian变换拼成的图像
*/
/************************************************************************/
class LaplacianBlending {
private:Mat_<Vec3f> left;Mat_<Vec3f> right;Mat_<float> blendMask;vector<Mat_<Vec3f> > leftLapPyr,rightLapPyr,resultLapPyr;//Laplacian PyramidsMat leftHighestLevel, rightHighestLevel, resultHighestLevel;vector<Mat_<Vec3f> > maskGaussianPyramid; //masks are 3-channels for easier multiplication with RGBint levels;void buildPyramids() {buildLaplacianPyramid(left,leftLapPyr,leftHighestLevel);buildLaplacianPyramid(right,rightLapPyr,rightHighestLevel);buildGaussianPyramid();}void buildGaussianPyramid() {//金字塔内容为每一层的掩模assert(leftLapPyr.size()>0);maskGaussianPyramid.clear();Mat currentImg;cvtColor(blendMask, currentImg, CV_GRAY2BGR);//store color img of blend mask into maskGaussianPyramidmaskGaussianPyramid.push_back(currentImg); //0-levelcurrentImg = blendMask;for (int l=1; l<levels+1; l++) {Mat _down;if (leftLapPyr.size() > l)pyrDown(currentImg, _down, leftLapPyr[l].size());elsepyrDown(currentImg, _down, leftHighestLevel.size()); //lowest levelMat down;cvtColor(_down, down, CV_GRAY2BGR);maskGaussianPyramid.push_back(down);//add color blend mask into mask PyramidcurrentImg = _down;}}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;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变换拼成的图像resultLapPyrresultHighestLevel = leftHighestLevel.mul(maskGaussianPyramid.back()) +rightHighestLevel.mul(Scalar(1.0,1.0,1.0) - maskGaussianPyramid.back());for (int l=0; l<levels; l++) {Mat A = leftLapPyr[l].mul(maskGaussianPyramid[l]);Mat antiMask = Scalar(1.0,1.0,1.0) - maskGaussianPyramid[l];Mat B = rightLapPyr[l].mul(antiMask);Mat_<Vec3f> blendedLevel = A + B;resultLapPyr.push_back(blendedLevel);}}public:LaplacianBlending(const Mat_<Vec3f>& _left, const Mat_<Vec3f>& _right, const Mat_<float>& _blendMask, int _levels)://construct function, used in LaplacianBlending lb(l,r,m,4);left(_left),right(_right),blendMask(_blendMask),levels(_levels){assert(_left.size() == _right.size());assert(_left.size() == _blendMask.size());buildPyramids();	//construct Laplacian Pyramid and Gaussian PyramidblendLapPyrs();	//blend left & right Pyramids into one Pyramid};Mat_<Vec3f> blend() {return reconstructImgFromLapPyramid();//reconstruct Image from Laplacian Pyramid}
};Mat_<Vec3f> LaplacianBlend(const Mat_<Vec3f>& l, const Mat_<Vec3f>& r, const Mat_<float>& m) {LaplacianBlending lb(l,r,m,4);return lb.blend();
}// 图像融合处理
void process(char *sleft, char *sright, char *sresult/*结果保存*/)
{if (!sleft || !*sleft || !sright || !*sright){return;}Mat l8u = imread(sleft);Mat r8u = imread(sright);// check file sizeSize sz1 = l8u.size(),sz2 = r8u.size();if ( sz1 != sz2){DEBUG_LOG("left and the right image must be the same size");exit(0);}//imshow("left",l8u); imshow("right",r8u);Mat_<Vec3f> l; l8u.convertTo(l,CV_32F,1.0/255.0);//Vec3f表示有三个通道,即 l[row][column][depth]Mat_<Vec3f> r; r8u.convertTo(r,CV_32F,1.0/255.0);/*****************    void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;******************//* Performs linear transformation on every source array element:dst(x,y,c) = scale*src(x,y,alpha)+beta.Arbitrary combination of input and output array depths are allowed(number of channels must be the same), thus the function can be usedfor type conversion *///create blend mask matrix mMat_<float> m(l.rows,l.cols,0.0);					//将m全部赋值为0m(Range::all(),Range(0,m.cols/2)) = 1.0;	//取m全部行&[0,m.cols/2]列,赋值为1.0Mat_<Vec3f> blend = LaplacianBlend(l, r, m);imshow("blended",blend);// save result to png filestd::vector<int> qualityType;qualityType.push_back(CV_IMWRITE_JPEG_QUALITY);qualityType.push_back(90);    //png格式下,默认的参数为3.if(sresult != NULL){//build file namechar filename[255] = {0};sprintf(filename, "%s.jpg", sresult);try {imwrite(filename, cv::Mat(blend), qualityType);}catch (std::runtime_error& ex) {DEBUG_LOG("Exception converting image to PNG format: %s\n", ex.what());exit(1);}DEBUG_LOG("Saved PNG file with alpha data...");}waitKey(0);
}/*******************************************************************************主函数*******************************************************************************/
int main( int argc, char * argv[] )
{// 调试程序,选择菜单->项目->属性->调试->命令参数, if ( argc < 3 ) {printf( "Usage:\n" );printf( "LaplacianBlending LeftImage RightImage\n" );exit( 0 );}DEBUG_LOG("\nleft image = %s\nright image=%s\n\n", argv[1], argv[2]);if ( argc == 3 )process( argv[1], argv[2], NULL );else  // argc >= 4process( argv[1], argv[2],argv[3]);return 0;
}//
#pragma warning(pop)



  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/243314.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ASA 9.21 in Vmware Workstation 10

There is old post “ASA 8.02 in Vmware Workstation “ in this blog posted on Dec 2011. Anothe post “How to Make your own ASA 8.42 in VMware”. Here are all related posts in this blog: ASA 8.02 in Vmware WorkstationASA 8.42 in VMware WorkstationASA 9.21 i…

状态机在VHDL中的实现

状态机在VHDL中的实现 1、Moore状态机的VHDL描述 输出仅取决于所处的状态 LIBRARY IEEE; --库、程序包的说明调用 USE IEEE.STD_LOGIC_1164.ALL;ENTITY Moore IS PORT (RESET,CLOCK,DIN : IN STD_LOGIC;DOUT : OUT STD_LOGIC_VECTOR(2 DOWNTO 0) ); END;ARCHITECTURE Mo…

VHDL仿真

VHDL仿真 仿真(Simulation也称模拟),不接触具体的硬件系统利用计算机对电路设计的逻辑行为和运行功能进行模拟检测,较大规模的VHDL系统设计的最后完成必须经历多层次的仿真测试过程,包括针对系统的VHDL行为仿真、分模块的时序仿真和硬件仿真,直至最后系统级的硬件仿真测…

关于结构体的内容

关于结构体的内容 结构体使用类似于C语言的语法来定义 结构体使用struct关键字声明。结构体内的成员可以是任何数据类型&#xff0c;包括用户自定义类型和其他的结构体类型。 struct{int a,b; //32位变量opcode_t opcode;//用户定义类型logic [23:0] adress;//24位变量bit er…

Pushing Policy Failed because Checkpoint Firewall “Load on module failed – no memory”

One day when pushing firewall policy from Checkpoint management server to UTM 272 cluster gateways, it failed and I got error message “Load on module failed – no memory” on one of cluster members. “Network Security Policy ‘Montreal_DMZ’ was prepared …

Point-BERT:一种基于Transformer架构的点云深度网络

目录 1. 前言 2. Point Tokenization 3. Transformer Backbone 4. Masked Point Modeling 5. Experiments Reference 1. 前言 从PointNet [1] 开始&#xff0c;点云深度网络逐渐成为解决点云特征提取与语义分析的主要研究方向。尤其在OpenAI的GPT模型获得了突破性成果后&#…

GNS3 VoIP Lab (Cisco 3725 and CME 4.3)

Here is a simple VoIP Lab in GNS3 environment. It is only used for my lab test and recorded here for future reference. 1. Topology: GNS3 Topology:Logic Topology:xp(192.168.2.60)——–C3725 Router(192.168.2.10) 2. Enviroment: ESXi 5.5 (or Vmware Workstation…

关于数组的内容

关于数组的内容 Verilog数组声明的基本语法 <data_type><vector_size><array_name><array_dimension> 例如&#xff1a; reg[15:0] RAM [0:4095];//储存器数组SystemVerilog允许任何数据类型的非压缩数组 SystemVerilog将非压缩数组的声明进行了扩展…

BreadCrumb控件

BreadCrumb控件&#xff0c;如上图所示&#xff0c;即面包屑导航控件&#xff0c;类似于TreeCtrl&#xff0c;但不是一次显示所有的Item&#xff0c;VC 2010可以编译通过&#xff0c;稍微修改一下其他的也可以编译&#xff0c;源代码下载&#xff1a; http://download.csdn.net…

Android简介

最近Android很火&#xff0c;小弟也想了解一下它的结构。跟CE或者Mobile比起来&#xff0c;它的结构是有点凌乱&#xff0c;也难怪&#xff0c;毕竟是基于别人的内核在上层开发了一些应用 ---------------------------------------------------------------------------------…

说不尽的刘恒

认识刘恒快三十年了&#xff0c;作为曾经的同事和他小说的责任编辑&#xff0c;我只写过他一篇文章&#xff0c;还是在二十多年前。有时候特别熟悉的人反而不知道从何写起&#xff0c;因为一想起往事&#xff0c;各种记忆像开闸的水一样涌满眼前&#xff0c;让人很难落笔。1985…

外行看Flash的存储原理

突然在网上看到别人两年前写的一篇关于nor和nand的好文章&#xff0c;做为csdn的合法公民&#xff0c;有必要转 一、存储数据的原理 两种闪存都是用三端器件作为存储单元&#xff0c;分别为源极、漏极和栅极&#xff0c;与场效应管的工作原理相同&#xff0c;主要是利用电场的…

快速计算整数的二进制表示法中1的个数

快速计算整数的二进制表示法中1的个数 题目&#xff1a;给定一个无符号32位整数x&#xff0c;求x的二进制表示法中含1的个数&#xff1f; 第一种算法&#xff1a; int OneCount(unsigned int x){ for(int count0; x>0; count) x&x-1;//把最后面的1变0 return …

在C/C++代码中使用SSE等指令集的指令(1)介绍

http://blog.csdn.net/gengshenghong/article/details/7007100我们知道&#xff0c;在C/C代码中&#xff0c;可以插入汇编代码提高性能。现在的指令集有了很多的高级指令&#xff0c;如果我们希望使用这些高级指令来实现一些高效的算法&#xff0c;就可以在代码中嵌入汇编&…

赋值操作符

赋值操作符 和其他赋值操作符 out in;//将out和in相加并将结果赋值给out //out out in;注意&#xff1a;这些操作符都是阻塞赋值packdage definitions;typedef enum logic[2:0] {ADD,SUB,MULT,DIV,SL.SR} opcode_t;typedef enum logic{UNSIGNED,SIGNED}operand_type_t;typ…

有无关通配符的相等操作符

有无关通配符的相等操作符 逻辑相等操作符 条件相等操作 SystemVerilog通配符相等操作符允许屏蔽某些位 SystemVerilog还增加了两个新的比较操作符&#xff1a; &#xff1f; 和&#xff01;&#xff1f;。这两个操作符允许在比较中屏蔽无关位。 操作符 &#xff1f;&#x…

cab文件介绍及制作方法

转自&#xff1a;http://bbs.pcpop.com/091030/5945399.html 1. 什么是cab文件 CAB在电脑上是一种压缩文件&#xff0c;微软出品的东西&#xff0c;用WinRAR可以解压缩&#xff0c;在DOS启动盘里面可以看见一些CAB压缩文件。其实Windows里面已经带了CAB压缩程序&#xff0c;但…

CVE-2015-0235: GHOST – A Critical Vulnerability in the Glibc Library

GHOST is a ‘buffer overflow’ bug affecting the gethostbyname() and gethostbyname2() function calls in the glibc library. If a remote attacker can make an application call to gethostbyname() or gethostbyname2(), this vulnerability allows the remote attack…

通过ap运行cab安装程序的方法及Sample Code

1. 第一部分&#xff1a;Sample Code 这部分转自&#xff1a;http://blog.csdn.net/hhygcy/archive/2009/05/04/4147870.aspx 最近这个东西很多被问及&#xff0c;软件动态升级的时候可能可以用到&#xff0c;在这里做一下记录。 就知道的方法有2个&#xff0c; 一个是通过Sh…

MFC透明桌面flash金鱼

代码&#xff1a; http://download.csdn.net/detail/hemmingway/6823935 使用方法&#xff1a; //0,添加left.png&#xff0c;right.png资源文件 //1, 头文件定义变量 GoldFish pet; //2, 在CPP文件创建 // Create a goldfishif (!pet.Create(NULL, NULL, WS_CHILD | WS_VISIB…