参考此书:数字图像处理与机器视觉,第二版,张铮
—————————————————————————————————————
%图像预处理
RGBIm = imread(‘lena.jpg’);%读入图片
GRAYImage = rgb2gray(RGBIm);%图像灰度转换
threshold = graythresh(GRAYImage);%阈值
BW = im2bw(RGBIm,threshold);%二值化图像
imshow(BW );
—————————————————————————————————————
%%
%图像反转
RGBIm = imread(‘lena.jpg’);%读入图片
doubleimage = double(RGBIm);
doubleimage = -doubleimage +256-1;
H = uint8(doubleimage);
subplot(1,2,1);imshow(doubleimage);
subplot(1,2,2);imshow(H);
—————————————————————————————————————
%%
%直方图均衡
RGBIm = imread(‘lena.jpg’);%读入图片
gray = rgb2gray(RGBIm);
figure;
subplot(2,2,1);imshow(gray);
subplot(2,2,2);imhist(gray);%获得直方图
histIm = histeq(gray);%直方图均衡后的图片
subplot(2,2,3);imshow(histIm);
subplot(2,2,4);imhist(histIm);%获得直方图
—————————————————————————————————————
%%
%线性平滑滤波器
RGBIm = imread(‘lena.jpg’);%读入图片
gray = rgb2gray(RGBIm);
noiseIm = imnoise(gray,‘salt & pepper’,0.02);%添加椒盐噪声
K1 = filter2(fspecial(‘average’,3),noiseIm)/255; %进行33模板平滑滤波
K2 = filter2(fspecial(‘average’,5),noiseIm)/255; %进行55模板平滑滤波
%K3 = filter2(fspecial(‘average’,7),noiseIm)/255;%进行77模板平滑滤波
%K4 = filter2(fspecial(‘average’,9),noiseIm)/255;%进行99模板平滑滤波
K3 = filter2(fspecial(‘gaussian’),noiseIm)/255; %进行gaussian滤波
K4 = filter2(fspecial(‘prewitt’),noiseIm)/255; %用于边缘增强,大小为【3 3】,无参数
figure;
subplot(2,3,1);imshow(RGBIm);title(‘原始图像’);
subplot(232);imshow(noiseIm);title(‘添加椒盐噪声’);
subplot(233),imshow(K1);title(‘33模板平滑滤波’);
subplot(234),imshow(K2);title('55模板平滑滤波’);
subplot(235),imshow(K3);title(‘高斯滤波’);
subplot(236),imshow(K4);title(‘prewitt滤波’);
—————————————————————————————————————
%%
%中值滤波器
RGBIm = imread(‘lena.jpg’);%读入图片
gray = rgb2gray(RGBIm);
noiseIm &