使用MATLAB中的一階導數運算元進行邊緣檢測


在數字影像處理中,邊緣檢測是用於識別數字影像中物體邊界的過程。我們有多種影像處理技術可以檢測影像中的邊緣,但在本教程中,我們將學習使用MATLAB中的一階導數運算元進行邊緣檢測。

什麼是MATLAB中的邊緣檢測?

MATLAB是一個執行復雜影像處理任務的高效工具。其中一項任務是邊緣檢測,它只不過是檢測影像中物體邊界的過程。

  • 在影像中,物體的邊緣通常是強度發生突然變化的區域。

  • 邊緣檢測是一個至關重要的過程,因為它在工程和技術領域的各個方面發揮著至關重要的作用,例如影像分割、特徵提取、影像質量增強、自動駕駛汽車和機器人技術等等。

現在,讓我們討論使用MATLAB中的一階導數運算元進行邊緣檢測的過程。

使用MATLAB中的一階導數運算元進行邊緣檢測

在MATLAB中,我們可以按照以下步驟使用一階導數運算元進行邊緣檢測

步驟(1)——使用“imread”函式讀取輸入影像。為此,請使用以下語法

img = imread('Image.jpg');

步驟(2)——將輸入影像轉換為灰度影像以使用MATLAB等數字工具進行處理。為此,請使用“rgb2gray”函式。

gray_img = rgb2gray(img);

步驟(3)——將灰度影像轉換為雙精度以進行計算。按如下方式執行此操作

gray_img = double(gray_img);

步驟(4)——定義一階導數運算元“D”以執行邊緣檢測。它可以是“前向運算元”、“中心運算元”或“後向運算元”。

步驟(5)——將影像與一階導數運算元進行卷積以檢測影像中物體的邊緣。為此,請按如下方式使用“conv2”函式

edge_img = conv2(gray_img, D);

步驟(6)——調整邊緣檢測影像,使其適合視覺化。為此,可以使用“abs”函式取卷積影像的絕對值,然後可以將生成的影像轉換為8位無符號整型格式進行顯示。按如下方式執行此操作

edge_img = abs(edge_img);
edge_img = uint8(edge_img);

步驟(7)——最後,使用“imshow”函式顯示生成的邊緣檢測影像,如下所示

imshow(edge_img);

現在,讓我們考慮一些MATLAB中的示例程式,以便實際瞭解如何使用一階導數運算元進行邊緣檢測。

示例(1)——使用前向運算元進行邊緣檢測

% MATLAB code to perform edge detection using first derivative forward operator
% Read the input image
img = imread('https://tutorialspoint.tw/assets/questions/media/14304-1687425236.jpg');

% Convert the input image to grayscale for processing
gray_img = rgb2gray(img);

% Convert the grayscale image to double data type for calculation
gray_img = double(gray_img);

% Create the first derivative operator
D = [1 -1 0];	% Forward operator

% Convolve the image with forward operator to detect edges
Cx = conv2(gray_img, D, 'same');	% Convolve along x-axis
Cy = conv2(gray_img, D, 'same');	%Convolve along y-axis

% Compute the edge magnitude (edge image)
edge_img = sqrt(Cx.^2 + Cy.^2);

% Display the original and edge detected images
figure;
subplot(2, 1, 1);
imshow(img);
title('Original Image');

subplot(2, 1, 2);
imshow(edge_img, []);
title('Edge Image');

輸出

它將生成以下輸出影像

程式碼解釋

在此MATLAB程式碼中,我們首先使用“imread”函式讀取輸入影像。然後,我們將輸入影像從RGB顏色比例轉換為灰度影像進行處理,並將其轉換為雙精度資料型別進行計算。

之後,定義前向一階導數運算元“D”,並使用“conv2”函式將灰度影像沿x軸和y軸與該運算元進行卷積以檢測邊緣。之後我們計算邊緣幅度以獲得完整的邊緣檢測影像。

最後,我們使用“imshow”函式顯示原始影像和邊緣檢測影像。

示例(2)——使用中心運算元進行邊緣檢測

% MATLAB code to perform edge detection using first derivative central operator
% Read the input image
img = imread('https://tutorialspoint.tw/assets/questions/media/14304-1687425236.jpg');

% Convert the input image to grayscale for processing
gray_img = rgb2gray(img);

% Convert the grayscale image to double data type for calculation
gray_img = double(gray_img);

% Create the first derivative operator
D = [1 0 -1];	% Central operator

% Convolve the image with forward operator to detect edges
Cx = conv2(gray_img, D, 'same');	% Convolve along x-axis
Cy = conv2(gray_img, D, 'same');	%Convolve along y-axis

% Compute the edge magnitude (edge image)
edge_img = sqrt(Cx.^2 + Cy.^2);

% Display the original and edge detected images
figure;
subplot(2, 1, 1);
imshow(img);
title('Original Image');

subplot(2, 1, 2);
imshow(edge_img, []);
title('Edge Image');

輸出

它將生成以下輸出影像

程式碼解釋:此MATLAB程式碼的實現和執行與之前的程式碼相同。唯一的區別在於此處使用一階導數中心運算元進行邊緣檢測。

示例(3)——使用後向運算元進行邊緣檢測

% Read the input image
img = imread('https://tutorialspoint.tw/assets/questions/media/14304-1687425236.jpg');

% Convert the input image to grayscale for processing
gray_img = rgb2gray(img);

% Convert the grayscale image to double data type for calculation
gray_img = double(gray_img);

% Create the first derivative operator
D = [0 1 -1];	% Backward operator

% Convolve the image with forward operator to detect edges
Cx = conv2(gray_img, D, 'same');	% Convolve along x-axis
Cy = conv2(gray_img, D, 'same');	%Convolve along y-axis

% Compute the edge magnitude (edge image)
edge_img = sqrt(Cx.^2 + Cy.^2);

% Display the original and edge detected images
figure;
subplot(2, 1, 1);
imshow(img);
title('Original Image');

subplot(2, 1, 2);
imshow(edge_img, []);
title('Edge Image');

輸出

它將生成以下輸出影像

程式碼解釋:此MATLAB程式碼的實現和執行與前兩個程式碼相同。唯一的區別在於此處使用一階導數後向運算元進行邊緣檢測。

結論

總之,邊緣檢測是突出影像中物體邊界的過程。可以使用MATLAB進行邊緣檢測。在本教程中,我藉助MATLAB中的示例程式解釋了使用一階導數運算元進行邊緣檢測的過程。您可以使用您自己的影像嘗試所有這些程式碼。

更新於:2023年10月10日

140 次檢視

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告