头文件:

/*
* 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. Android使用圆角

    圆角Button 效果图 绿色Button 定义button_green.xml资源文件位于drawable文件夹下,可用作button的background属性 button_green.xml: ...

  2. VIM使用技巧5

    查找并手动替换.例如有如下一段文字: We' re waiting for content before the site can go live If you are content with th ...

  3. 00.mp4v2工具的用法

    1.交叉编译mp4v2库# ./configure --prefix=/usr/local/mp4v2-2.0.0 --host=arm-hisiv300-linux  CC=arm-hisiv300 ...

  4. paramiko模块及ssh远程登陆

    ssh实现远程登陆一般有两种方式,一种就是用户密码登陆,另一种是密钥登陆(当然默认是要服务端打开ssh服务). 我这里使用这两种方法操作一下远程登陆,测试客户端是本机的root与jeff用户,远程连接 ...

  5. Codeforces 761E Dasha and Puzzle(构造)

    题目链接 Dasha and Puzzle 对于无解的情况:若存在一个点入度大于4,那么直接判断无解. 从根结点出发(假设根结点的深度为0), 深度为0的节点到深度为1的节点的这些边长度为2^30, ...

  6. ListView设置某一项item的文本居中

    使用ListView和volley写了一个使用网络获取天气的demo ListView中Item的文本模式都是左侧对齐 我这边需要一些标题文本居中对齐 网上也找不到示例,不过找到了getView这个函 ...

  7. 基于WPF系统框架设计(9)-多值绑定之IMultiValueConverter

    应用场景 我想把View层的一个布局控件和功能按钮传到ViewModel层,达到动态变更布局,同时灵活获取功能按钮的属性,让View和ViewModel完全分离,而不受View层影响. 最后我想到使用 ...

  8. 【Java】List转化为数组

    List转化为数组的两种方式: 第一种: List<String> list = new ArrayList<>(); String [] arr = list.toArray ...

  9. 解决官网下载jdk只有5k大小的错误

    问题现象 官网 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 我选择linu ...

  10. 解决 java.sql.SQLException: Before start of result set

    java中使用如下代码做数据库连接,用以查询数据 *******************我是分割线************************************* try { Class.f ...