matlab圖像濾波基礎(chǔ)

2017-02-14  by:CAE仿真在線  來源:互聯(lián)網(wǎng)

%% 基礎(chǔ)濾波

addpath('F:\A');

file = imread('linker_9.jpg');
if isempty( file )
error('file is empty !');
end
if size(file,3) == 3
img = rgb2gray( file );
else
img = file;
end

%% 均值濾波

filter_avg = fspecial('average',[5 5]); %% 定義一個均值濾波器
filter_gau = fspecial('gaussian', [5 5], 1.0); %% 定義一個高斯濾波器

img_avg = imfilter(img, filter_avg, 'replicate' ); %%讓圖像通過濾波器
img_gau = imfilter(img, filter_gau, 'replicate' ); %%讓圖像通過濾波器

% figure;
% imshow( img_avg );
% title( 'img_avg' );
% figure;
% imshow( img_gau );
% title( 'img_gau' );

imwrite(img_avg, 'linker_9_avg.jpg'); %% 默認(rèn)保存在當(dāng)前文件夾下
imwrite(img_gau, 'linker_9_gau.jpg'); %% 默認(rèn)保存在當(dāng)前文件夾下

%% 中值濾波

% axes(handles.axes2);
% x=(handles.img);


img_salt = imnoise(img, 'salt & pepper', 0.04); %% 加椒鹽噪聲

img_mid = medfilt2( img_salt(:,:), [5 5], 'symmetric'); %% 圖像中值濾波

figure;
imshow( img_mid );
title('img_mid');

imwrite(img_mid, 'lingker_9_mid.jpg');

%% 雙邊濾波

img1 = im2double( img );

img1_bil = bilateralFilter(img1, 5, [3, 0.1]);

img_bil = im2uint8( img1_bil );

% figure;
% imshow( img_bil );
% title('img_bil');

imwrite(img_bil, 'linker_9_bil.jpg');

===========================================================

%% 雙邊濾波函數(shù)
% A為歸一化到[0,1]的圖像矩陣
% W為雙邊濾波器(核)的邊長/2
% 定義域方差σd記為SIGMA(1)
% 值域的方差σr記為SIGMA(2)

% 問題: 如何選取合適的方差SIGMA ???

% Pre-process input and select appropriate filter.
function B = bilateralFilter(A, w, sigma)
% Verify that the input image exists and is valid.
if ~exist('A','var') || isempty(A)
error('Input image A is undefined or empty.');
end
if ~isfloat(A) || ~sum([1,3] == size(A,3)) || ...
min(A(:)) < 0 || max(A(:)) > 1
error(['Input image A must be a double precision ',...
'matrix of size NxMx1 or NxMx3 on the closed ',...
'interval [0,1].']);
end
% Verify bilateral filter window size.
if ~exist('w','var') || isempty(w) || ...
numel(w) ~= 1 || w < 1
w = 5;
end
% w 向上取整
w = ceil(w);
% Verify bilateral filter standard deviations.
if ~exist('sigma','var') || isempty(sigma) || ...
numel(sigma) ~= 2 || sigma(1) <= 0 || sigma(2) <= 0
sigma = [3 0.1];
end
% Apply either grayscale or color bilateral filtering.
if size(A,3) == 1
B = bfltGray(A, w, sigma(1), sigma(2));
else
B = bfltColor(A, w, sigma(1), sigma(2));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Implements bilateral filtering for grayscale images.
function B = bfltGray(A,w,sigma_d,sigma_r)
%創(chuàng)建核距離矩陣
[X,Y] = meshgrid(-w:w,-w:w);

% 基于距離差的高斯卷積核(固定的)
G = exp(-(X.^2+Y.^2)/(2*sigma_d^2));
[rows, cols] = size(A);
B = zeros(rows, cols);
for i = 1:rows
for j = 1:cols
% 提取當(dāng)前核所作用的區(qū)域:
% (iMin:iMax,jMin:jMax)
iMin = max(i-w,1);
iMax = min(i+w,rows);
jMin = max(j-w,1);
jMax = min(j+w,cols);
I = A(iMin:iMax,jMin:jMax);
% 基于像素值差的高斯卷積核(變動的)
H = exp(-(I-A(i,j)).^2/(2*sigma_r^2));
% 兩核乘積并歸一化濾波
F = H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1);
B(i,j) = sum(F(:).*I(:))/sum(F(:));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Implements bilateral filter for color images.
function B = bfltColor(A,w,sigma_d,sigma_r)
% Convert input sRGB image to CIELab color space.
if exist('applycform','file')
A = applycform(A,makecform('srgb2lab'));
else
A = colorspace('Lab<-RGB',A);
end
% Pre-compute Gaussian domain weights.
[X,Y] = meshgrid(-w:w,-w:w);
G = exp(-(X.^2+Y.^2)/(2*sigma_d^2));
% Rescale range variance (using maximum luminance).
sigma_r = 100*sigma_r;
% Create waitbar.
h = waitbar(0,'Applying bilateral filter...');
set(h,'Name','Bilateral Filter Progress');
% Apply bilateral filter.
dim = size(A);
B = zeros(dim);
for i = 1:dim(1)
for j = 1:dim(2)
% Extract local region.
iMin = max(i-w,1);
iMax = min(i+w,dim(1));
jMin = max(j-w,1);
jMax = min(j+w,dim(2));
I = A(iMin:iMax,jMin:jMax,:);
% Compute Gaussian range weights.
dL = I(:,:,1)-A(i,j,1);
da = I(:,:,2)-A(i,j,2);
db = I(:,:,3)-A(i,j,3);
H = exp(-(dL.^2+da.^2+db.^2)/(2*sigma_r^2));
% Calculate bilateral filter response.
F = H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1);
norm_F = sum(F(:));
B(i,j,1) = sum(sum(F.*I(:,:,1)))/norm_F;
B(i,j,2) = sum(sum(F.*I(:,:,2)))/norm_F;
B(i,j,3) = sum(sum(F.*I(:,:,3)))/norm_F;
end
waitbar(i/dim(1));
end
% Convert filtered image back to sRGB color space.
if exist('applycform','file')
B = applycform(B,makecform('lab2srgb'));
else
B = colorspace('RGB<-Lab',B); A
end
% Close waitbar.
close(h);


開放分享:優(yōu)質(zhì)有限元技術(shù)文章,助你自學(xué)成才

相關(guān)標(biāo)簽搜索:matlab圖像濾波基礎(chǔ) MatLab培訓(xùn) MatLab培訓(xùn)課程 MatLab在線視頻教程 MatLab技術(shù)學(xué)習(xí)教程 MatLab軟件教程 MatLab資料下載 MatLab代做 MatLab基礎(chǔ)知識 Fluent、CFX流體分析 HFSS電磁分析 Ansys培訓(xùn) Abaqus培訓(xùn) 

編輯
在線報名:
  • 客服在線請直接聯(lián)系我們的客服,您也可以通過下面的方式進(jìn)行在線報名,我們會及時給您回復(fù)電話,謝謝!
驗證碼

全國服務(wù)熱線

1358-032-9919

廣州公司:
廣州市環(huán)市中路306號金鷹大廈3800
電話:13580329919
          135-8032-9919
培訓(xùn)QQ咨詢:點(diǎn)擊咨詢 點(diǎn)擊咨詢
項目QQ咨詢:點(diǎn)擊咨詢
email:kf@1cae.com