头文件:

/*
* Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 2 or any later version.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details. A copy of the GNU General Public License is available at:
* http://www.fsf.org/licensing/licenses
*/ /*****************************************************************************
* convolution.h
*
* Linear convolution and polynomial multiplication.
*
* The convolution routine "conv" is implemented by it's definition in time
* domain. If the sequence to be convoluted are long, you should use the
* fast convolution algorithm "fastConv", which is implemented in frequency
* domain by usin FFT.
*
* Zhang Ming, 2010-01, Xi'an Jiaotong University.
*****************************************************************************/ #ifndef CONVOLUTION_H
#define CONVOLUTION_H #include <vector.h>
#include <fft.h>
#include <utilities.h> namespace splab
{ template<typename Type> Vector<Type> conv( const Vector<Type>&,
const Vector<Type>& );
template<typename Type> Vector<Type> convolution( const Vector<Type>&,
const Vector<Type>& ); template<typename Type> Vector<Type> fastConv( const Vector<Type>&,
const Vector<Type>& ); #include <convolution-impl.h> }
// namespace splab #endif
// CONVOLUTION_H

实现文件:

/*
* Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 2 or any later version.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details. A copy of the GNU General Public License is available at:
* http://www.fsf.org/licensing/licenses
*/ /*****************************************************************************
* convolution-impl.h
*
* Implementation for linear convolution.
*
* Zhang Ming, 2010-01, Xi'an Jiaotong University.
*****************************************************************************/ /**
* convolution and ploynonal multiplication.
*/
template <typename Type>
Vector<Type> conv( const Vector<Type> &signal, const Vector<Type> &filter )
{
if( signal.dim() < filter.dim() )
return convolution( filter, signal );
else
return convolution( signal, filter );
} template <typename Type>
Vector<Type> convolution( const Vector<Type> &signal, const Vector<Type> &filter )
{
int sigLength = signal.dim();
int filLength = filter.dim();
assert( sigLength >= filLength ); int length = sigLength + filLength - 1;
Vector<Type> x(length); for( int i=1; i<=length; ++i )
{
x(i) = 0;
if( i < filLength )
for( int j=1; j<=i; ++j )
x(i) += filter(j) * signal(i-j+1);
else if( i <= sigLength )
for( int j=1; j<=filLength; ++j )
x(i) += filter(j) * signal(i-j+1);
else
for( int j=i-sigLength+1; j<=filLength; ++j )
x(i) += filter(j) * signal(i-j+1);
}
return x;
} /**
* Fast convolution by FFT.
*/
template<typename Type>
Vector<Type> fastConv( const Vector<Type> &xn, const Vector<Type> &yn )
{
int M = xn.dim(),
N = yn.dim(); Vector<Type> xnPadded = wextend( xn, N-1, "right", "zpd" ),
ynPadded = wextend( yn, M-1, "right", "zpd" );
return ifftc2r( fft(xnPadded) * fft(ynPadded) ); // Vector< complex<Type> > Zk = fft(xnPadded) * fft(ynPadded);
// return ifftc2r(Zk); // return ifftc2r( fft(wextend(xn,N-1,"right","zpd")) * fft(wextend(yn,M-1,"right","zpd")) );
}

测试代码:

/*****************************************************************************
* convolution.cpp
*
* Convolution testing.
*
* Zhang Ming, 2010-01, Xi'an Jiaotong University.
*****************************************************************************/ #define BOUNDS_CHECK #include <iostream>
#include <convolution.h> using namespace std;
using namespace splab; typedef double Type;
const int M = 3;
const int N = 5; int main()
{
Vector<Type> xn( M ), yn( N );
Vector<Type> zn; for( int i=0; i<M; ++i )
xn[i] = i;
for( int i=0; i<N; ++i )
yn[i] = i-N/2; // convolution
zn = conv( xn, yn );
cout << "xn: " << xn << endl << "yn: " << yn << endl;
cout << "convolution of xn and yn: " << zn << endl;
zn = fastConv( xn, yn );
cout << "fast convolution of xn and yn: " << zn << endl; return 0;
}

运行结果:

xn:  size: 3 by 1
0
1
2 yn: size: 5 by 1
-2
-1
0
1
2 convolution of xn and yn: size: 7 by 1
0
-2
-5
-2
1
4
4 fast convolution of xn and yn: size: 7 by 1
-2.53765e-016
-2
-5
-2
1
4
4 Process returned 0 (0x0) execution time : 0.078 s
Press any key to continue. http://my.oschina.net/zmjerry/blog/3671http://v.youku.com/v_show/id_XMTMwNDI1NDAw.html

图像处理之基础---卷积及其快速算法的C++实现的更多相关文章

  1. 图像处理之基础---彩色转灰度算法优化rgb to yuv

    File:      StudyRGB2Gray.txtName:      彩色转灰度算法彻底学习Author:    zyl910Version:   V1.0Updata:    2006-5- ...

  2. Op-level的快速算法

    十岁的小男孩 本文为终端移植的一个小章节. 目录 引言 FFT Conv2d (7x7, 9x9) Winograd Conv2d (3x3, 5x5) 引言 本节针对CNN进行加速计算的,主要有以下 ...

  3. Atitit 图像处理之理解卷积attilax总结

    Atitit 图像处理之理解卷积attilax总结 卷积的运算可以分为反转.平移,相乘,求和.        在图像处理中,图像是一个大矩阵,卷积模板是一个小矩阵.按照上述过程,就是先把小矩阵反转,然 ...

  4. MinFilter(MaxFilter)快速算法C++实现

    目录 1.算法简述 1.1.MinFilter(MaxFilter) 算法简述 1.2.MinFilter(MaxFilter) 快速算法简述 2.实现代码 2.1.MinFilterOneRow 单 ...

  5. web前端基础知识及快速入门指南

    web前端基础知识及快速入门指南 做前端开发有几个月了,虽然说是几个月,但是中间断断续续的上课.考试以及其它杂七杂八的事情,到现在居然一直感觉自己虽然很多前端的知识很眼熟,却也感觉自己貌似也知识在门口 ...

  6. 从大整数乘法的实现到 Karatsuba 快速算法

    Karatsuba 快速乘积算法是具有独特合并过程(combine/merge)的分治算法(Karatsuba 是俄罗斯人).此算法主要是对两个整数进行相乘,并不适用于低位数(如 int 的 32 位 ...

  7. Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法

    原文:Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法 前面章节中介绍了二值图像的形态学算法,这里讲一下灰度图的形态学算法,主要是公式,代码略. 1,膨胀算法 2,腐蚀算法 3 ...

  8. 图像处理之基础---二维卷积c实现

    http://wenku.baidu.com/link?url=4RzdmvP9sdaaUbnVEW4OyBD-g67wIOiJjKFF3Le_bu7hIiBS7I6hMcDmCXrQwsHvrsPv ...

  9. 图像处理之基础---很好的一个快速比较两副图片是否相同的code 可用于公安鉴别

    转自Codeproject http://www.codeproject.com/dotnet/comparingimages.asp Public Enum CompareResult ciComp ...

随机推荐

  1. 四、Ubuntu 一些常用命令

    1.锁定root用户 :sudo passwd -l root 2.解锁root用户 :sudo passwd -u root 3.切换身份:su root  或者  su 其他用户名,然后输入密码, ...

  2. Codeforces 509E(思维)

                                                                                                         ...

  3. HDFS写文件过程分析

    转自http://shiyanjun.cn/archives/942.html HDFS是一个分布式文件系统,在HDFS上写文件的过程与我们平时使用的单机文件系统非常不同,从宏观上来看,在HDFS文件 ...

  4. SecureCRT使用Ctrl+D快速关闭Tab

  5. UIView的任意圆角

    今天在做项目的时候,遇到一个问题,grouped类型的tableview 怎么样才能让他们的一个view 其中一个角圆角? 如上图所示,其实我是用UILabel,但是箭头的位置总是尖的不太好看.设置l ...

  6. 避免在block中循环引用(Retain Cycle in Block)

    让我们长话短说.请参阅如下代码: - (IBAction)didTapUploadButton:(id)sender { NSString *clientID = @"YOUR_CLIENT ...

  7. 检查iOS app 是否升级为新版本

    之前我帮某公司做的一个iOS app,升级的时候发现闪退问题.后来检查是因为升级的时候数据库出现一点小问题导致对象为空. 下面这个代码可以检测程序是否更新了,从而进行相关处理: 1 2 3 4 5 6 ...

  8. MATLAB逻辑函数

    %%逻辑函数 %%all:判断是否有元素非0,A是多维矩阵,all(A)是以列为单位来处理的,当前列的逻辑 %值为1,当且仅当当前列的每一个元素都非0 A=[1,2,3;0,2,1;5,0,2]; % ...

  9. LCD设备驱动程序

    LCD是Liquid  Crystal  Display的简称,也就是经常所说的液晶显示器 LCD能够支持彩色图像的显示和视频的播放,是一种非常重要的输出设备 Framebuffer 是Linux系统 ...

  10. angular js 使用$location问题整理

    angular js 自带的$location方法十分强大,通过使用$location方法.我们能够获取到server的port.杂乱连接中的path()部分(/所包括的部分). 例: // give ...