已阅: 132
目的
思路
- 用矩阵表示算子。
- 遍历图像,以每个像素点为中心的3*3/2*2矩阵为待处理数据,
- 将算子矩阵与待处理矩阵点乘,求和结果矩阵,以其作为矩阵中心点的新值。
实验结果
- 从上到下,从左到右依次为:原图、梯度算子、索贝尔算子、拉普拉斯算子
代码
clear;
ima = imread('lena_color.jpg');
grayIma = rgb2gray(ima);
[height, width] = size(grayIma);
gradientOperatorX = ([-1 1; 0 0]);
gradientOperatorY = ([-1 0; 1 0]);
sobelOperatorX = [-1 0 1; -2 0 2; -1 0 1];
sobelOperatorY = [-1 -2 -1; 0 0 0; 1 2 1];
laplacianOperator = [0 1 0; 1 -4 1; 0 1 0];
imaGradient = grayIma;
imaSobel = grayIma;
imaLaplacian = grayIma;
for row = 1:height-1
for col = 1:width-1
%梯度
temp = double(grayIma(row:row+1, col:col+1));
tempX = gradientOperatorX .* temp;
tempX = abs(sum(tempX(:)));
tempY = gradientOperatorY .* temp;
tempY = abs(sum(tempY(:)));
imaGradient(row, col) = tempX + tempY;
if row~=1 && col~=1
%索贝尔
temp = double(grayIma(row-1:row+1, col-1:col+1));
tempX = sobelOperatorX .* temp;
tempX = abs(sum(tempX(:)));
tempY = sobelOperatorY .* temp;
tempY = abs(sum(tempY(:)));
imaSobel(row, col) = tempX + tempY;
%拉普拉斯
tempX = laplacianOperator .* temp;
tempX = abs(sum(tempX(:)));
imaLaplacian(row, col) = tempX;
end
end
end
subplot(2,2,1);
imshow(grayIma);
subplot(2,2,2);
imshow(imaGradient);
subplot(2,2,3);
imshow(imaSobel);
subplot(2,2,4);
imshow(imaLaplacian);
共有 0 条评论