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.convolvendconv = 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.correlatendcorr = 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.filter2Dcvfilter = 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探究的更多相关文章

  1. (原)使用intel的ipp库计算卷积及相关

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5462631.html 参考网址: https://software.intel.com/zh-cn/n ...

  2. 图像卷积、相关以及在MATLAB中的操作

    图像卷积.相关以及在MATLAB中的操作 2016年7月11日 20:34:35, By ChrisZZ 区分卷积和相关 图像处理中常常需要用一个滤波器做空间滤波操作.空间滤波操作有时候也被叫做卷积滤 ...

  3. [CLPR] 卷积神经网络的结构

    本文翻译自: http://www.codeproject.com/Articles/16650/Neural-Network-for-Recognition-of-Handwritten-Digi ...

  4. 相关与卷积(数字信号处理)的数学原理及 Python 实现

    数学原理 在数字信号处理中,相关(correlation)可以分为互相关(cross correlation)和自相关(auto-correlation). 互相关是两个数字序列之间的运算:自相关是单 ...

  5. opencv:图像卷积

    卷积基本概念 C++代码实现卷积 #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; u ...

  6. 13、OpenCV实现图像的空间滤波——图像平滑

    1.空间滤波基础概念 1.空间滤波基础 空间滤波一词中滤波取自数字信号处理,指接受或拒绝一定的频率成分,但是空间滤波学习内容实际上和通过傅里叶变换实现的频域的滤波是等效的,故而也称为滤波.空间滤波主要 ...

  7. OpenCV图像处理与视频分析详解

    1.OpenCV4环境搭建 VS2017新建一个控制台项目 配置包含目录 配置库目录 配置链接器 配置环境变量 重新启动VS2017 2.第一个图像显示程序 main.cpp #include< ...

  8. opencv笔记4:模板运算和常见滤波操作

    time:2015年10月04日 星期日 00时00分27秒 # opencv笔记4:模板运算和常见滤波操作 这一篇主要是学习模板运算,了解各种模板运算的运算过程和分类,理论方面主要参考<图像工 ...

  9. SSE图像算法优化系列十一:使用FFT变换实现图像卷积。

    本文重点主要不在于FFT的SSE优化,而在于使用FFT实现快速卷积的相关技巧和过程. 关于FFT变换,有很多参考的代码,特别是对于长度为2的整数次幂的序列,实现起来也是非常简易的,而对于非2次幂的序列 ...

随机推荐

  1. CentOS查看分区的方式

    看ls /dev然后挂载 df -T  只可以查看已经挂载的分区和文件系统类型 fdisk -l 可以显示出所有挂载和未挂载的分区,但不显示文件系统类型 parted -l 可以查看未挂载的文件系统类 ...

  2. 『PyTorch』第四弹_通过LeNet初识pytorch神经网络_下

    『PyTorch』第四弹_通过LeNet初识pytorch神经网络_上 # Author : Hellcat # Time : 2018/2/11 import torch as t import t ...

  3. LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)

    题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazi ...

  4. EBS OAF开发中的错误/异常处理(ErrorHandling) (转)

    原文地址 EBS OAF开发中的错误/异常处理(ErrorHandling) EBS OAF开发中的错误/异常处理(ErrorHandling) (版权声明,本人原创或者翻译的文章如需转载,如转载用于 ...

  5. @ConditionalOnProperty 详解

    @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) @Documented @Con ...

  6. golang模拟动态高优先权优先调度算法

    实验二  动态高优先权优先调度 实验内容 模拟实现动态高优先权优先(若数值越大优先权越高,每运行一个时间单位优先权-n,若数值越小优先权越高,没运行一个时间单位优先权+n),具体如下: 设置进程体:进 ...

  7. Python实现简单的爬虫获取某刀网的更新数据

    昨天晚上无聊时,想着练习一下Python所以写了一个小爬虫获取小刀娱乐网里的更新数据 #!/usr/bin/python # coding: utf-8 import urllib.request i ...

  8. FR报表 FileName

    在设计或者打印预览时,如果设置了FileName,可能反而出错. procedure TfrxReport.ShowPreparedReport; var WndExStyles: Integer; ...

  9. 玩转X-CTR100 l STM32F4 l DS18B20单总线温度传感器

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器 扩展DS1 ...

  10. 关于junk jack

    旨在利用一切物品打造你自己的世界.你需要砍树获得木头,挖各种矿石用火炉炼成不同东西,顺便得到不同颜色材质的石头建房子,打各种家禽获得吃的甚至烘焙.养起来繁殖,天上掉下的星星.捡到圣诞礼包中的袜子都可以 ...