簡體   English   中英

如何在圖像上應用butterwoth過濾器

[英]how to apply butterwoth filter on an image

我想對照片應用butterworth過濾器 濾波器設計方法是雙線性的

我設計的過濾器是:

[N, Wn]=buttord(2, 4.83, -3, -15, 's');
[b,a]=butter(N,Wn,'s');
[num,den]=bilinear(b,a,1);

如何將濾鏡應用於2D圖像?

buttord用於一維信號處理。 您可以編寫一個簡單的代碼來自行設計2D butterworth濾波器。 下面是帶通butterworth濾波器的示例代碼。 您可以在高通濾波器上移除d1 ,或在低通濾波器上移除d0

filter1 = ones(2*nx-1,2*ny-1);
filter2 = ones(2*nx-1,2*ny-1);
filter3 = ones(2*nx-1,2*ny-1);

for i = 1:2*nx-1
    for j =1:2*ny-1
        d = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;

        filter1(i,j)= 1/(1 + (d/d1)^(2*n)); % d1:higher cutoff frequency
        filter2(i,j) = 1/(1 + (d/d0)^(2*n)); % d0:lower cutoff frequency
        filter3(i,j)= 1 - filter2(i,j);
        butterworthf(i,j) = filter1(i,j).*filter3(i,j); % Create Butterworth filter.

    end
end

您可以使用該鏈接上給出的代碼:

https://www.mathworks.com/matlabcentral/fileexchange/30946-butterworth-bandpass-filter-for-image-processing

代碼是

function filtered_image = butterworthbpf(I,d0,d1,n)
    % Butterworth Bandpass Filter
    % This simple  function was written for my Digital Image Processing course
    % at Eastern Mediterranean University taught by
    % Assoc. Prof. Dr. Hasan Demirel
    % for the 2010-2011 Spring Semester
    % for the complete report:
    % http://www.scribd.com/doc/51981950/HW4-Frequency-Domain-Bandpass-Filtering
    %
    % Written By:
    % Leonardo O. Iheme (leonardo.iheme@cc.emu.edu.tr)
    % 23rd of March 2011
    %
    %   I = The input grey scale image
    %   d0 = Lower cut off frequency
    %   d1 = Higher cut off frequency
    %   n = order of the filter
    %
    % The function makes use of the simple principle that a bandpass filter
    % can be obtained by multiplying a lowpass filter with a highpass filter
    % where the lowpass filter has a higher cut off frquency than the high pass filter.
    %
    % Usage BUTTERWORTHBPF(I,DO,D1,N)
    % Example
    % ima = imread('grass.jpg');
    % ima = rgb2gray(ima);
    % filtered_image = butterworthbpf(ima,30,120,4);
    f = double(I);
    [nx, ny] = size(f);
    f = uint8(f);
    fftI = fft2(f,2*nx-1,2*ny-1);
    fftI = fftshift(fftI);
    subplot(2,2,1)
    imshow(f,[]);
    title('Original Image')
    subplot(2,2,2)
    fftshow(fftI,'log')
    title('Image in Fourier Domain')
    % Initialize filter.
    filter1 = ones(2*nx-1,2*ny-1);
    filter2 = ones(2*nx-1,2*ny-1);
    filter3 = ones(2*nx-1,2*ny-1);
    for i = 1:2*nx-1
        for j =1:2*ny-1
            dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
            % Create Butterworth filter.
            filter1(i,j)= 1/(1 + (dist/d1)^(2*n));
            filter2(i,j) = 1/(1 + (dist/d0)^(2*n));
            filter3(i,j)= 1.0 - filter2(i,j);
            filter3(i,j) = filter1(i,j).*filter3(i,j);
        end
    end
    % Update image with passed frequencies.
    filtered_image = fftI + filter3.*fftI;
    subplot(2,2,3)
    fftshow(filter3,'log')
    title('Filter Image')
    filtered_image = ifftshift(filtered_image);
    filtered_image = ifft2(filtered_image,2*nx-1,2*ny-1);
    filtered_image = real(filtered_image(1:nx,1:ny));
    filtered_image = uint8(filtered_image);
    subplot(2,2,4)
    imshow(filtered_image,[])
    title('Filtered Image')
end

我不知道MATLAB內置函數,但我寫了一個可以用於Image的butterworth過濾器

function Filter = MyButterWorth(grade, cutoff_freq, img_name,type)

img=imread(img_name); %read image
[rows,cols] = size(img);
img_double=double(img);
FImg = fftshift(fft2(img_double)); %Fast Fourier transform 2D and shift it to Center 
%immagphase(FImg);

% compute distance to center with consider image size
x =  (ones(rows,1) * [1:cols]  - (fix(cols/2)+1))/cols;
y =  ([1:rows]' * ones(1,cols) - (fix(rows/2)+1))/rows;

radius = sqrt(x.^2 + y.^2); 
% create filter
Filter = 1 ./ (1.0 + (radius ./ cutoff_freq).^(2*grade));
% change filter type low pass or high
if(strcmp(type, 'hpf'))
    Filter = 1 - Filter;
end
figure;
surf([-1:2/(cols-1):1],[-1:2/(rows-1):1], Filter);
shading interp;
title('Butterworth filter');
xlabel('x');
ylabel('y');
zlabel('intensity');
grid on;

%applay filter
resultFImg = FImg .* Filter;

resultImg = real(ifft2(ifftshift(resultFImg)));

%show image
figure;
subplot(1,2,1); imshow(img); title('Orginal Image')
subplot(1,2,2); imshow(resultImg,[]); title('Filtered Image')

在此處此處查看更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM