五种算子对图像进行边缘提取(Matlab)

目的

  • 使用Roberts算子、Prewitt算子、Prewitt斜对角线算子、Laplacian算子(中心-4)、Laplacian算子(中心-20)提取图像边缘。

思路

  • 用矩阵表示算子。
  • 遍历图像,以每个像素点为中心的3*3/2*2矩阵为待处理数据,
  • 将算子矩阵与待处理矩阵点乘,求和结果矩阵,以其作为矩阵中心点的新值。
  • 设定阈值,遍历新图像,根据阈值进行图像二值化。

实验结果

  • 整体来说,Prewitt算子的表现更好,清晰提取边缘的同时不会引入噪声。
  • Roberts算子表现其次,引入了少量噪声,可能是因为其模板只关注了2*2矩阵的变化,不如Prewitt的3*3矩阵全面。
  • Laplacian算子则引入了很多噪声,可能是因为其模板只有一个,导致其对边缘的敏感性不如Prewitt算子与Roberts算子强,轻微变化就被当做边缘,因此引入了不少噪声

代码

clear;
ima = imread('lena_color.jpg');
grayIma = rgb2gray(ima);
[height, width] = size(grayIma);

%roberts
robertsOperatorX = [-1 0; 0 1];
robertsOperatorY = [0 -1; 1 0];
%prewitt算子
prewittOperatorX = ([-1 0 1;-1 0 1;-1 0 1;]);
prewittOperatorY = ([-1 -1 -1; 0 0 0; 1 1 1]);
%prewitt斜对角线算子
prewittOperatorX2 = [0 1 1; -1 0 1; -1 -1 0];
prewittOperatorY2 = [-1 -1 0; -1 0 1; 0 1 1];
%laplacian
laplacianOperator = [0 1 0; 1 -4 1; 0 1 0];
%laplacian2
laplacianOperator2 = [1 4 1; 4 -20 4; 1 4 1];

imaRoberts = grayIma;
imaPrewitt = grayIma;
imaPrewitt2 = grayIma;
imaLaplacian = grayIma;
imaLaplacian2 = grayIma;

for row = 1:height-1
    for col = 1:width-1
        temp = double(grayIma(row:row+1, col:col+1));
        tempX = robertsOperatorX .* temp;
        tempX = abs(sum(tempX(:)));
        tempY = robertsOperatorY .* temp;
        tempY = abs(sum(tempY(:)));
        imaRoberts(row, col) = tempX + tempY;
        if row~=1 && col~=1
            %prewitt算子
            temp = double(grayIma(row-1:row+1, col-1:col+1));
            tempX = prewittOperatorX .* temp;
            tempX = abs(sum(tempX(:)));
            tempY = prewittOperatorY .* temp;
            tempY = abs(sum(tempY(:)));
            imaPrewitt(row, col) = tempX + tempY;
            %prewitt斜对角线算子
            tempX = prewittOperatorX2 .* temp;
            tempX = abs(sum(tempX(:)));
            tempY = prewittOperatorY2 .* temp;
            tempY = abs(sum(tempY(:)));
            imaPrewitt2(row, col) = tempX + tempY;
            %laplacian
            tempX = laplacianOperator .* temp;
            tempX = abs(sum(tempX(:)));
            imaLaplacian(row, col) = tempX;
            %laplacian2
            tempX = laplacianOperator2 .* temp;
            tempX = abs(sum(tempX(:)));
            imaLaplacian2(row, col) = tempX;
        end
    end
end

robertsThresholdValue = 30;
prewittThresholdValue = 100;
prewittThresholdValue2 = 100;
laplacianThresholdValue = 30;
laplacianThresholdValue2 = 150;

for row = 1:height-1
    for col = 1:width-1
        %roberts
        if(imaRoberts(row, col) < robertsThresholdValue)
            imaRoberts(row, col) = 255;
        else
            imaRoberts(row, col) = 1;
        end
        if row~=1 && col~=1
            %prewitt
            if(imaPrewitt(row, col) < prewittThresholdValue)
                imaPrewitt(row, col) = 255;
            else
                imaPrewitt(row, col) = 1;
            end
            %prewitt斜对角线算子
            if(imaPrewitt2(row, col) < prewittThresholdValue2)
                imaPrewitt2(row, col) = 255;
            else
                imaPrewitt2(row, col) = 1;
            end
            %laplacian
            if(imaLaplacian(row, col) < laplacianThresholdValue)
                imaLaplacian(row, col) = 255;
            else
                imaLaplacian(row, col) = 1;
            end
            %laplacian2
            if(imaLaplacian2(row, col) < laplacianThresholdValue2)
                imaLaplacian2(row, col) = 255;
            else
                imaLaplacian2(row, col) = 1;
            end
        end
    end
end

subplot(2,3,1);
imshow(grayIma);
subplot(2,3,2);
imshow(imaRoberts);
subplot(2,3,3);
imshow(imaPrewitt);
subplot(2,3,4);
imshow(imaPrewitt2);
subplot(2,3,5);
imshow(imaLaplacian);
subplot(2,3,6);
imshow(imaLaplacian2);

You may also like...

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注