当前位置:网站首页>【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
2022-07-04 12:50:00 【Nirvana;】
【Matlab】conv、filter、conv2、filter2和imfilter函数总结
1. conv函数
作用:
1.计算一维向量卷积
u = [1 1 1];
v = [1 1 0 0 0 1 1];
w = conv(u,v)
2.通过卷积计算多项式乘法
u = [1 0 1];
v = [2 7];
w = conv(u,v)
2. filter函数
作用:一维数字滤波器
y = filter(b,a,x) 使用由分子和分母系数 b 和 a 定义的有理传递函数 对输入数据 x 进行滤波。
// 移动平均滤波器是用于对含噪数据进行平滑处理的常用方法。
t = linspace(-pi,pi,100);
rng default %initialize random number generator
x = sin(t) + 0.25*rand(size(t));
windowSize = 5;
b = (1/windowSize)*ones(1,windowSize);
a = 1;
y = filter(b,a,x);
plot(t,x)
hold on
plot(t,y)
legend('Input Data','Filtered Data')
3. conv2函数
作用:二维卷积
A = rand(3);
B = rand(4);
C = conv2(A,B)
conv2函数
1、用法
C=conv2(A,B,shape); %卷积滤波
A:输入图像,B:卷积核
假设输入图像A大小为ma x na,卷积核B大小为mb x nb,则
当shape=full时,返回全部二维卷积结果,即返回C的大小为(ma+mb-1)x(na+nb-1)
shape=same时,返回与A同样大小的卷积中心部分
shape=valid时,不考虑边界补零,即只要有边界补出的零参与运算的都舍去,返回C的大小为(ma-mb+1)x(na-nb+1)
2、实现步骤
假设输入图像A大小为ma x na,卷积核大小为mb x nb,则MATLAB的conv2函数实现流程如下:
a、对输入图像补零,第一行之前和最后一行之后都补mb-1行,第一列之前和最后一列之后都补nb-1列(注意conv2不支持其他的边界补充选项,函数内部对输入总是补零)。
b、关于卷积核的中心,旋转卷积核180度。
c、滑动卷积核,将卷积核的中心位于图像矩阵的每一个元素。
d、将旋转后的卷积核乘以对应的矩阵元素再求和。
4. filter2函数
1、用法
B = filter2(h,A,shape) ; %相关(correlation)滤波
- A:输入图像,h:相关核
- 假设输入图像A大小为ma x na,相关核h大小为mb x nb,则
当shape=full时,返回全部二维卷积结果,即返回B的大小为(ma+mb-1)x(na+nb-1) - shape=same时,返回与A同样大小的卷积中心部分
- shape=valid时,不考虑边界补零,即只要有边界补出的零参与运算的都舍去,返回B的大小为(ma-mb+1)x(na-nb+1)
2、实现步骤
假设输入图像A大小为ma x na,相关核h大小为mb x nb,MATLAB的filter2的实现流程如下:
- a、对输入图像补零,第一行之前和最后一行之后都补mb-1行,第一列之前和最后一列之后都补nb-1列(注意filter2不支持其他的边界补充选项,函数内部对输入总是补零)。
- b、滑动相关核,将相关核的中心位于图像矩阵的每一个元素。
- c、将相关核乘以对应的矩阵元素再求和
注意filter2不对核进行180°旋转,直接对应相乘再相加,这一点与filter2不同。
5. imfilter函数
1、用法
B=imfilter(A,H,option1,option2,option3);
A:输入图像,H:滤波核
- option1:边界选项,可选的有:补充固定的值X(默认都补零),symmetric,replicate,circular
- option2:输出图像大小选项,可选的有same(默认),full
- option3:决定采用与filter2相同的相关滤波还是与conv2相同的卷积滤波
2、优势:
Padding Options 填充选项
1)默认补0
2)symmetric 对称:数组边界之外的输入数组值是通过沿数组边界对数组进行镜面反射得到
3)replicate 复制:数组边界之外的输入数组值假定为等于最近的数组边界值.
4)circular 循环:数组边界之外的输入数组值是通过隐式假设输入数组具有周期性来计算的。
Output Size 输出大小
same:输出数组与输入数组大小相同。这是未指定输出大小选项时的默认行为。
full:输出数组是完全滤波后的结果,因此比输入数组大。
6. fspecial函数
作用:构造卷积核,可以与filter2、conv2和imfilter配合使用
h = fspecial(type)
h = fspecial('average',hsize)
h = fspecial('disk',radius)
h = fspecial('gaussian',hsize,sigma)
h = fspecial('laplacian',alpha)
h = fspecial('log',hsize,sigma)
h = fspecial('motion',len,theta)
h = fspecial('prewitt')
h = fspecial('sobel')
7. 总结
filter2、conv2将输入转换为double类型,输出也是double的,输入总是补零(zero padded), 不支持其他的边界补充选项。
imfilter:不将输入转换为double,输出只与输入同类型,有灵活的边界补充选项。建议使用~
8. 代码演示
MATLAB代码:
clear;
close all;
clc;
%% fspecial函数
value = 5;
h = fspecial('gaussian',[5 5],value);
srcImage = imread('lena.jpg');
srcImage = rgb2gray(srcImage);
srcImage_double = double(srcImage);
%% conv2函数 默认:'full',只能补零
image_conv2 = conv2(srcImage_double,h);
%% filter2函数 默认:'same',只能补零
image_filter2 = filter2(h,srcImage_double);
%% imfilter函数 默认:'same'
image_imfilter = imfilter(srcImage,h,'replicate');
%% 显示图像
figure(1);
subplot(221);imshow(srcImage,[]); title('原图');
subplot(222);imshow(image_conv2,[]); title('conv2');
subplot(223);imshow(image_filter2,[]); title('filter2');
subplot(224);imshow(image_imfilter,[]); title('imfilter');
效果图:
边栏推荐
- Programmer anxiety
- Interviewer: what is the internal implementation of hash data type in redis?
- Read excel table data
- 苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?
- Introduction to XML II
- 2022 Shandong Province safety officer C certificate examination question bank and online simulation examination
- 2022危险化学品经营单位主要负责人练习题及模拟考试
- 美国土安全部部长警告移民“不要踏上危险的旅程”
- 结合案例:Flink框架中的最底层API(ProcessFunction)用法
- 基于PaddleX的智能零售柜商品识别
猜你喜欢
MySQL8版本免安装步骤教程
Getting started with the go language is simple: go implements the Caesar password
JVM series - stack and heap, method area day1-2
2022年山东省安全员C证考试题库及在线模拟考试
Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)
英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
嵌入式编程中五个必探的“潜在错误”
MySQL5免安装修改
吃透Chisel语言.09.Chisel项目构建、运行和测试(一)——用sbt构建Chisel项目并运行
苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?
随机推荐
205. 同构字符串
程序员转方向
Haproxy high availability solution
.Net之延迟队列
30: Chapter 3: develop Passport Service: 13: develop [change / improve user information, interface]; (use * * * Bo class to accept parameters, and use parameter verification)
unity不识别rider的其中一种解决方法
JVM series - stack and heap, method area day1-2
Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
Automatic filling of database public fields
吃透Chisel语言.04.Chisel基础(一)——信号类型和常量
markdown 语法之字体标红
一次 Keepalived 高可用的事故,让我重学了一遍它
苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?
基于PaddleX的智能零售柜商品识别
C foundation in-depth study I
[C question set] of VII
Interviewer: what is the internal implementation of hash data type in redis?
. Net delay queue
中邮科技冲刺科创板:年营收20.58亿 邮政集团是大股东