用循环实现拉普拉斯锐化Matlab代码
时间: 2024-11-16 12:20:48 浏览: 7
在MATLAB中,拉普拉斯锐化是一种常用的技术,用于增强图像的边缘细节。这里是一个简单的循环实现拉普拉斯锐化的示例代码:
```matlab
function sharpened_img = laplacian_sharpening(original_img)
% 图像尺寸
[rows, cols] = size(original_img);
% 初始化拉普拉斯算子矩阵
laplacian_matrix = [-1 -1; -1 8; -1 -1]; % 3x3 Laplacian kernel
% 矩阵转置以便卷积
laplacian_matrix = laplacian_matrix';
% 初始化结果图像
sharpened_img = zeros(rows, cols);
% 循环遍历图像的每个像素
for i = 1:rows
for j = 1:cols
% 计算当前像素及其邻居的加权平均
weighted_sum = original_img(i-1,j-1) * laplacian_matrix(1,1) + ...
original_img(i-1,j+1) * laplacian_matrix(1,3) + ...
original_img(i,j-1) * laplacian_matrix(2,1) + ...
original_img(i,j) * laplacian_matrix(2,2) + ...
original_img(i,j+1) * laplacian_matrix(2,3) + ...
original_img(i+1,j-1) * laplacian_matrix(3,1) + ...
original_img(i+1,j) * laplacian_matrix(3,2) + ...
original_img(i+1,j+1) * laplacian_matrix(3,3);
% 将计算结果存储到新图像中
sharpened_img(i,j) = original_img(i,j) + weighted_sum;
end
end
end
% 使用函数并展示结果
sharpened_img = laplacian_sharpening(original_image); % 假设original_image是你需要锐化的原始图像
imshow
阅读全文