Matalb-图像均值滤波,中值滤波,梯度锐化(sobel算子)的实现
clc;clear all;
img = imread('C:\Users\lihuanyu\Desktop\opencv\image\lenaNoise.png');
figure;
imshow(img),title("原图像");
img_R = img(:,:,1);
img_G = img(:,:,2);
img_B = img(:,:,3);
%纵,横,通道数
[ROW,COL,DIM] = size(img);
%构造(3,3)的矩阵
R = zeros(ROW,COL);
G = zeros(ROW,COL);
B = zeros(ROW,COL);
funBox = zeros(3,3);
for i = 1:ROW - 2
for j = 1:COL - 2
funBox = img_R(i:i+2,j:j+2);
s = sum(funBox(:))/9;
R(i+1,j+1) = s;
funBox = img_G(i:i+2,j:j+2);
s = sum(funBox(:))/9;
G(i+1,j+1) = s;
funBox = img_B(i:i+2,j:j+2);
s = sum(funBox(:))/9;
B(i+1,j+1) = s;
end;
end;
img_filter(:,:,1) = R/255;
img_filter(:,:,2) = G/255;
img_filter(:,:,3) = B/255;
figure(2);
imshow(img_filter),title("均值滤波后的图像");
clc;clear all;
img = imread('C:\Users\lihuanyu\Desktop\opencv\image\lenaNoise.png');
figure;
imshow(img),title("原图像");
img_R = img(:,:,1);
img_G = img(:,:,2);
img_B = img(:,:,3);
%纵,横,通道数
[ROW,COL,DIM] = size(img);
%构造(3,3)的矩阵
R = zeros(ROW,COL);
G = zeros(ROW,COL);
B = zeros(ROW,COL);
funBox = zeros(3,3);
for i = 1:ROW - 2
for j = 1:COL - 2
funBox = img_R(i:i+2,j:j+2);
s = sort(funBox(:));
R(i+1,j+1) = s(5);
funBox = img_G(i:i+2,j:j+2);
s = sort(funBox(:));
G(i+1,j+1) = s(5);
funBox = img_B(i:i+2,j:j+2);
s = sort(funBox(:));
B(i+1,j+1) = s(5);
end;
end;
img_filter(:,:,1) = R/255;
img_filter(:,:,2) = G/255;
img_filter(:,:,3) = B/255;
figure(2);
imshow(img_filter),title("中值滤波后的图像");
clc;clear all;
img = imread('C:\Users\lihuanyu\Desktop\opencv\image\lena256.bmp');
figure;
imshow(img),title("原图像");
[ROW,COL] = size(img);
img = double(img);
new_img = zeros(ROW,COL); %新建画布
sobel_x = [-1,0,1;-2,0,2;-1,0,1];
sobel_y = [-1,-2,-1;0,0,0;1,2,1];
for i = 1:ROW - 2
for j = 1:COL - 2
funBox = img(i:i+2,j:j+2);
G_x = sobel_x .* funBox;
G_x = abs(sum(G_x(:)));
G_y = sobel_y .* funBox;
G_y = abs(sum(G_y(:)));
sobelxy = G_x * 0.5 + G_y * 0.5;
%带有截距的锐化(二值化)
% if (sobelxy > 150)
% sobelxy = 255;
% else
% sobelxy = 0;
% end
new_img(i+1,j+1) = sobelxy;
end
end
figure(2);
imshow(new_img/255),title("梯度运算的图像");
Python+OpenCV计算机视觉 - 李立宗