[CLPR] 卷积还是相关? - Opencv之filter2D探究
I am doing something about convolving images in Python and for sake of speed I chose opencv 2.4.9.
Opencv offers a way called filter2D to do this and here's its docs:http://docs.opencv.org/modules/imgproc/doc/filtering.html?highlight=filter2d#filter2d
In docs, it says:
Convolves an image with the kernel.
But I have doubts(caused by something else) so I make some experiments on it:
First, I make a normal 3x3 matrix a using numpy as:
[[ 1., 5., 0.],
[ 7., 2., 9.],
[ 2., 3., 4.]]
Then, I make a 2x2 matrix b as the cornel as:
>>> b
[[ 1., 2.],
[ 3., 4.]]
Finally, in order to make it clear to see difference between convolve and correlate, rotate b by 180 degree and b will look like:
[[ 4., 3.],
[ 2., 1.]]
Now, All pre-work is done. We could begin the experiment.
Step 1. Use scipy.ndimage.convolve: ndconv = ndimage.convolve(a, b, mode = 'constant')
and ndconv is:
[[ 35., 33., 18.],
[ 41., 45., 44.],
[ 17., 24., 16.]]
Convolution op will rotate b by 180 degree and do correlation using b on a. So ndconv[0][0] = 4*1+3*5+2*7+1*2 = 35, and ndconv[2][2] = 4*4+3*0+2*0+1*0 = 16
This result is correct.
Step 2. Use scipy.ndimage.correlate: ndcorr = ndimage.correlate(a, b, mode = 'constant')
and ndcorr is:
[[ 4., 23., 15.],
[ 30., 40., 47.],
[ 22., 29., 45.]]
According to correlation's definition, ndcorr[0][0] = 1*0+2*0+3*0+4*1 = 4 because the border will expand by 0.
(Someone may be confused by the expandation's difference between conv and corr. It seems convolveexpand image in directions right and down while correlate in directions left and up.)
But this is not the point.
Step 3. Use cv2.filter2D: cvfilter = cv2.filter2D(a, -1, b)
and cvfilter is:
[[ 35., 34., 35.],
[ 41., 40., 47.],
[ 33., 29., 45.]]
If we ignore the border cases, we will find that what cv2.filter2D did is actually a correlation other than aconvolution! How could I say that?
because cvfilter[1..2][1..2] == ndcorr[1..2][1..2].
WEIRD, isn't it?
Could anyone be able to tell the real thing that cv2.filter2D do? Thanks a lot.
[CLPR] 卷积还是相关? - Opencv之filter2D探究的更多相关文章
- (原)使用intel的ipp库计算卷积及相关
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5462631.html 参考网址: https://software.intel.com/zh-cn/n ...
- 图像卷积、相关以及在MATLAB中的操作
图像卷积.相关以及在MATLAB中的操作 2016年7月11日 20:34:35, By ChrisZZ 区分卷积和相关 图像处理中常常需要用一个滤波器做空间滤波操作.空间滤波操作有时候也被叫做卷积滤 ...
- [CLPR] 卷积神经网络的结构
本文翻译自: http://www.codeproject.com/Articles/16650/Neural-Network-for-Recognition-of-Handwritten-Digi ...
- 相关与卷积(数字信号处理)的数学原理及 Python 实现
数学原理 在数字信号处理中,相关(correlation)可以分为互相关(cross correlation)和自相关(auto-correlation). 互相关是两个数字序列之间的运算:自相关是单 ...
- opencv:图像卷积
卷积基本概念 C++代码实现卷积 #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; u ...
- 13、OpenCV实现图像的空间滤波——图像平滑
1.空间滤波基础概念 1.空间滤波基础 空间滤波一词中滤波取自数字信号处理,指接受或拒绝一定的频率成分,但是空间滤波学习内容实际上和通过傅里叶变换实现的频域的滤波是等效的,故而也称为滤波.空间滤波主要 ...
- OpenCV图像处理与视频分析详解
1.OpenCV4环境搭建 VS2017新建一个控制台项目 配置包含目录 配置库目录 配置链接器 配置环境变量 重新启动VS2017 2.第一个图像显示程序 main.cpp #include< ...
- opencv笔记4:模板运算和常见滤波操作
time:2015年10月04日 星期日 00时00分27秒 # opencv笔记4:模板运算和常见滤波操作 这一篇主要是学习模板运算,了解各种模板运算的运算过程和分类,理论方面主要参考<图像工 ...
- SSE图像算法优化系列十一:使用FFT变换实现图像卷积。
本文重点主要不在于FFT的SSE优化,而在于使用FFT实现快速卷积的相关技巧和过程. 关于FFT变换,有很多参考的代码,特别是对于长度为2的整数次幂的序列,实现起来也是非常简易的,而对于非2次幂的序列 ...
随机推荐
- zzuli 1432(二进制特点)
1432: 背包again Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 222 Solved: 65 SubmitStatusWeb Board ...
- OC 类的本质和分类
一.分类 (一)分类的基本知识 概念:Category 分类是OC特有的语言,依赖于类. 分类的作用:在不改变原来的类内容的基础上,为类增加一些方法. 添加一个分类: 文件结构图: 在分类中添加一 ...
- The Architecture of Open Source Applications——阅读笔记part 1
Architects look at thousands of buildings during their training, and study critiques of those buildi ...
- RabbitMQ整合spring
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 快速切题 sgu113 Nearly prime numbers 难度:0
113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...
- (C/C++学习笔记) 十八. 继承和多态
十八. 继承和多态 ● 继承的概念 继承(inheritance): 以旧类为基础创建新类, 新类包含了旧类的数据成员和成员函数(除了构造函数和析构函数), 并且可以派生类中定义新成员. 形式: cl ...
- Double H5.0
Alpha阶段 - 博客链接合集 项目Github地址 Github 敏捷冲刺日志 Alpha冲刺! Day1 - 磨刀 Alpha冲刺! Day2 - 砍柴 Alpha冲刺! Day3 - 砍柴 A ...
- L1-052 2018我们要赢
2018年天梯赛的注册邀请码是“2018wmyy”,意思就是“2018我们要赢”.本题就请你用汉语拼音输出这句话. 输入格式: 本题没有输入. 输出格式: 在第一行中输出:“2018”:第二行中输出: ...
- webservice之wsdl
最近项目中需要将原来的通信协议改成webservice,由于业务需要,我们需要向server端传送数据,故server方提供给我们一个.wsdl文件,内容如下: WSDL即Web Services D ...
- Intellij Idea2016.3 svn服务器拉取代码
1.修改idea的默认配置,取消SVN设置里的两个勾 2.拉取代码 3.输入SVN仓库的地址,然后checkout 即可