MATLAB之微分处理图像前向差分
复杂些的滤波算子一般是先利用高斯滤波来平滑,然后计算其1阶和2阶微分。由于它们滤除高频和低频,因此称为带通滤波器(band-pass filters)。
先介绍必备的图像微分知识。
function dimg = mipforwarddiff(img,direction)
% MIPFORWARDDIFF Finite difference calculations
%
% DIMG = MIPFORWARDDIFF(IMG,DIRECTION)
%
% Calculates the forward-difference for a given direction
% IMG : input image
% DIRECTION : 'dx' or 'dy'
% DIMG : resultant image
%
imgPad = padarray(img,[1 1],'symmetric','both');%将原图像的边界扩展
[row,col] = size(imgPad);
dimg = zeros(row,col);
switch (direction)
case 'dx',
dimg(:,1:col-1) =imgPad(:,2:col)-imgPad(:,1:col-1);%x方向差分计算,
case 'dy',
dimg(1:row-1,:) =imgPad(2:row,:)-imgPad(1:row-1,:);
otherwise, disp('Direction is unknown');
end;
dimg = dimg(2:end-1,2:end-1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%主函数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I = imread('lena1.png');
figure; imshow(I);
forward_dx = mipforwarddiff(I,'dx'); figure, imshow(forward_dx);
forward_dy = mipforwarddiff(I,'dy'); figure, imshow(forward_dy);
原图
x方向
y方向
FPGA自习学院将不断更新和总结FPGA相关学习资料,书籍,实例工程和视频。
欢迎大家加入FPGA自习学院,一起学习一起成长。