MATLAB - 高斯-拉普拉斯濾波器



高斯濾波器是一種用於影像處理的線性濾波器,用於模糊或平滑影像。它以高斯函式命名,該函式用於定義濾波器的形狀。高斯濾波器通常用於減少影像中的噪聲和細節,使其更適合進一步處理或分析。

高斯-拉普拉斯 (LoG) 濾波器是一種流行的影像增強和邊緣檢測濾波器,用於影像處理。它是兩種濾波器的組合:高斯濾波器和拉普拉斯濾波器。高斯濾波器用於平滑影像並減少噪聲,而拉普拉斯濾波器用於檢測邊緣。

高斯-拉普拉斯濾波器可用於檢測影像中不同尺度的邊緣。透過改變高斯濾波器的標準差,可以控制檢測邊緣的尺度。較小的標準差檢測更精細的細節,而較大的標準差檢測更寬泛的特徵。

讓我們看看幾個高斯-拉普拉斯濾波器的示例。

示例 1:使用 fspecial() 函式

fspecial() 函式用於建立高斯濾波器,然後計算該高斯的拉普拉斯運算元以建立 LoG 濾波器。但是,拉普拉斯濾波器期望高斯濾波器為 double 型別。

我們的程式碼如下:

% Read the image
img = imread('peppers.jpg');

% Convert the image to grayscale
if size(img, 3) == 3
    img_gray = rgb2gray(img);
else
    img_gray = img;
end

% Create a Gaussian filter
sigma = 2; % Standard deviation of the Gaussian filter
hsize = 2 * ceil(3 * sigma) + 1; % Filter size
gaussian_filter = fspecial('gaussian', hsize, sigma);

% Create a Laplacian of Gaussian filter
log_filter = fspecial('log', hsize, sigma);

% Apply the LoG filter to the image
filtered_img = imfilter(double(img_gray), log_filter, 'conv', 'replicate');

% Display the original and filtered images
subplot(1, 2, 1);
imshow(img_gray);
title('Original Image');

subplot(1, 2, 2);
imshow(uint8(filtered_img));
title('Laplacian of Gaussian Filtered Image');

讓我們詳細瞭解程式碼:

img = imread('peppers.jpg');

此處,它從當前目錄讀取影像“peppers.jpg”並將其儲存在變數 img 中。

if size(img, 3) == 3
    img_gray = rgb2gray(img);
else
    img_gray = img;
end

如果影像為彩色(RGB 格式),則使用 rgb2gray 函式將其轉換為灰度。灰度影像儲存在變數 img_gray 中。如果影像已經是灰度影像,則按原樣儲存。

sigma = 2; % Standard deviation of the Gaussian filter
hsize = 2 * ceil(3 * sigma) + 1; % Filter size
gaussian_filter = fspecial('gaussian', hsize, sigma);

上面的程式碼建立了高斯濾波器。這裡,我們定義了高斯濾波器的標準差 sigma 並根據標準差計算濾波器大小 hsize。然後,我們使用 fspecial 函式建立高斯濾波器,其中“gaussian”作為濾波器型別。

log_filter = fspecial('log', hsize, sigma);

我們使用 fspecial 函式建立高斯-拉普拉斯濾波器,其中“log”作為濾波器型別。此濾波器表示高斯濾波器的拉普拉斯運算元,用於邊緣檢測。

filtered_img = imfilter(double(img_gray), log_filter, 'conv', 'replicate');

此處,將高斯-拉普拉斯濾波器應用於使用 imfilter 函式的灰度影像 img_gray。'conv' 選項指定應使用卷積應用濾波器,而“replicate”選項指定在濾波期間如何處理影像邊界。

subplot(1, 2, 1);
imshow(img_gray);
title('Original Image');

subplot(1, 2, 2);
imshow(uint8(filtered_img));
title('Laplacian of Gaussian Filtered Image');

最後,我們使用 subplot、imshow 和 title 函式並排顯示原始灰度影像和濾波後的影像。濾波後的影像在顯示之前轉換為 uint8 格式。

執行程式碼後,我們得到如下輸出:

示例 2:使用拉普拉斯和 LoG 濾波器進行影像濾波

此示例顯示了將兩種不同的濾波器(拉普拉斯濾波器和高斯-拉普拉斯 (LoG) 濾波器)應用於輸入影像“peppers.jpg”

我們的程式碼如下:

x=imread('peppers.jpg');
figure;
imshow(x);
title('Input Image');
figure;
h=fspecial('laplacian');
filtered_image=imfilter(x,h);
imshow(filtered_image);
title('Output of Laplacian Filter');
figure;
h=fspecial('log');
filtered_image=imfilter(x,h);
imshow(filtered_image);
title('Laplacian Gaussian Filter');

在上面的示例中:

x = imread('peppers.jpg');
figure;
imshow(x);
title('Input Image');

此程式碼讀取影像“peppers.jpg”並使用 imshow 函式顯示它。title 函式在影像圖形中新增標題。

h = fspecial('laplacian');
filtered_image = imfilter(x, h);

這裡,使用 fspecial 函式建立拉普拉斯濾波器,其中“laplacian”作為濾波器型別。然後使用 imfilter 函式將此濾波器應用於輸入影像 x,從而產生濾波後的影像 filtered_image。

figure;
imshow(filtered_image);
title('Output of Laplacian Filter');

此程式碼顯示從拉普拉斯濾波器獲得的濾波後的影像。title 函式在影像圖形中新增標題。

h = fspecial('log');
filtered_image = imfilter(x, h);

與拉普拉斯濾波器類似,使用 fspecial 函式建立 LoG 濾波器,其中“log”作為濾波器型別。然後使用 imfilter 函式將此濾波器應用於輸入影像 x,從而產生濾波後的影像 filtered_image。

imshow(filtered_image);
title('Laplacian of Gaussian Filter');

此程式碼顯示從 LoG 濾波器獲得的濾波後的影像。title 函式在影像圖形中新增標題。

執行程式碼後,我們得到如下輸出:

廣告
© . All rights reserved.