图像处理之基础---卷积及其快速算法的C++实现
头文件:
/*
* 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++实现的更多相关文章
- 图像处理之基础---彩色转灰度算法优化rgb to yuv
File: StudyRGB2Gray.txtName: 彩色转灰度算法彻底学习Author: zyl910Version: V1.0Updata: 2006-5- ...
- Op-level的快速算法
十岁的小男孩 本文为终端移植的一个小章节. 目录 引言 FFT Conv2d (7x7, 9x9) Winograd Conv2d (3x3, 5x5) 引言 本节针对CNN进行加速计算的,主要有以下 ...
- Atitit 图像处理之理解卷积attilax总结
Atitit 图像处理之理解卷积attilax总结 卷积的运算可以分为反转.平移,相乘,求和. 在图像处理中,图像是一个大矩阵,卷积模板是一个小矩阵.按照上述过程,就是先把小矩阵反转,然 ...
- MinFilter(MaxFilter)快速算法C++实现
目录 1.算法简述 1.1.MinFilter(MaxFilter) 算法简述 1.2.MinFilter(MaxFilter) 快速算法简述 2.实现代码 2.1.MinFilterOneRow 单 ...
- web前端基础知识及快速入门指南
web前端基础知识及快速入门指南 做前端开发有几个月了,虽然说是几个月,但是中间断断续续的上课.考试以及其它杂七杂八的事情,到现在居然一直感觉自己虽然很多前端的知识很眼熟,却也感觉自己貌似也知识在门口 ...
- 从大整数乘法的实现到 Karatsuba 快速算法
Karatsuba 快速乘积算法是具有独特合并过程(combine/merge)的分治算法(Karatsuba 是俄罗斯人).此算法主要是对两个整数进行相乘,并不适用于低位数(如 int 的 32 位 ...
- Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法
原文:Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法 前面章节中介绍了二值图像的形态学算法,这里讲一下灰度图的形态学算法,主要是公式,代码略. 1,膨胀算法 2,腐蚀算法 3 ...
- 图像处理之基础---二维卷积c实现
http://wenku.baidu.com/link?url=4RzdmvP9sdaaUbnVEW4OyBD-g67wIOiJjKFF3Le_bu7hIiBS7I6hMcDmCXrQwsHvrsPv ...
- 图像处理之基础---很好的一个快速比较两副图片是否相同的code 可用于公安鉴别
转自Codeproject http://www.codeproject.com/dotnet/comparingimages.asp Public Enum CompareResult ciComp ...
随机推荐
- 搞定linux的中文输入和vim
本篇是http://blog.csdn.net/guochaoxxl/article/details/53212090的姊妹篇,无论先操作哪一篇都可以: 1.一言不合先下载,链接: https://p ...
- 复制View对象
Mark一下 - (UIView*)duplicate:(UIView*)view { NSData * tempArchive = [NSKeyedArchiver archivedDataWit ...
- 354. Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- (8)ftp配置文档
1.vsftpd文件夹中的ftpusers文件的作用 它是一个黑名单,ftpusers不受任何配制项的影响 该文件存放的是一个禁止访问FTP的用户列表 管理员不希望一些拥有过大权限的帐号(比如root ...
- luogu P1608 路径统计
题目描述 “RP餐厅”的员工素质就是不一般,在齐刷刷的算出同一个电话号码之后,就准备让HZH,TZY去送快餐了,他们将自己居住的城市画了一张地图,已知在他们的地图上,有N个地方,而且他们目前处在标注为 ...
- 【JSOI2007】文本生成器
用AC自动机处理所有了解的单词 显然,不能直接算,直接算的话,我们需要大力容斥,复杂度不允许 我们不妨反过来做,我们根据AC自动机处理出所有的不可行解,然后用总数减去即可 计算所有不可行解用dp,\( ...
- Qt小结
(1)#include 报错fatal error: QHostInfo:No such file or directory, 解决办法 在.pro文件中添加 QT += core gui netwo ...
- 解决unknown import path "golang.org/x/sys/unix": unrecognized import path "golang.org/x/sys"
问题描述 当我们使用 go get.go install.go mod 等命令时,会自动下载相应的包或依赖包.但由于众所周知的原因,类似于 golang.org/x/... 的包会出现下载失败的情况. ...
- mac 配置sencha touch环境
1 安装 java 2 安装 node js 为使用npm作准备 3 用npm命令安装 cordova npm install -g cordova
- 【2048小游戏】——原生js爬坑之封装行的移动算法&事件
引言:2048小游戏的核心玩法是移动行,包括横行和纵行,玩家可以选择4个方向,然后所有行内的数字就会随着行的移动而向特定的方向移动.这个行的移动是一个需要重复调用的算法,所以这里就要将一行的移动算法封 ...