灰度变换函数
线性点运算
alpha=linspace(0,2*pi,7);
bata=linspace(0,2*pi,13);
x1=cos(alpha);y1=sin(alpha);
x2=cos(bata);y2=sin(bata);
plot(x1,y1,x2,y2)
负像(y=ax+b)
flower=imread('/MATLAB Drive/shiyan.jpg');
I=double(flower);
% J=-1*I;
J1=-3*I+70;
% subplot(1,2,1),imshow(I,[]);
% subplot(1,2,2),imshow(J,[]);
subplot(1,2,1),imshow(I,[]);
subplot(1,2,2),imshow(J1,[]);
灰度分段变换
clf
X1=imread('C:\Users\admin\Desktop/shiyan.jpg');
subplot(221),imshow(X1);
f0=0;g0=0;
f1=70;g1=30;
f2=180;g2=230;
f3=255;g3=255;
subplot(222),plot([f0 f1 f2 f3],[g0 g1 g2 g3]);
axis tight,xlabel('f'),ylabel('g'),title('灰度变换曲线');
r1=(g1-g0)/(f2-f1);
b1=g0-r1*f0;
r2=(g2-g1)/(f2-f1);
b2=g1-r2*f0;
r3=(g3-g2)/(f3-f2);
b3=g2-r3*f2;
[m n]=size(X1);
X2=double(X1);
for I=1:m
for J=1:n
f=X2(I,J);
g(I,J)=0;
if (f>=0)&(f<=f1)
g(I,J)=r1*f+b1;
elseif(f>f2)&(f<=f3)
g(I,J)=r3*f+b3;
end
end
end
subplot(223),imshow(mat2gray(g));
对比度和对数拉伸变换
clf
I=imread('/MATLAB Drive/shiyan.jpg');
g=im2uint8(mat2gray(log(1+double(I))));
subplot(121),imshow(I);title('原图');
subplot(122),imshow(g);title('对比变换图');
直方图处理与函数绘制
生成绘制图像
clf
I=imread('/MATLAB Drive/shiyan.jpg');
subplot(121);
imshow(I);
subplot(122);imshow(I);
直方图均衡化
clf
I=imread('/MATLAB Drive/shiyan.jpg');
J=imadjust(I,[0.3 0.7],[]);
subplot(221),imshow(I);title('输入图像');
subplot(222),imshow(J);title('均衡图像');
subplot(223),imhist(I);title('输入图像直方图');
subplot(224),imhist(J);title('均衡图像直方图');
直方图规定化
clf
I=imread('/MATLAB Drive/shiyan.jpg');
J=histeq(I,50);
subplot(221);imshow(I);title('输入图像');
subplot(222);imhist(I,45);title('规定直方图');
subplot(223);imshow(J);title('规定匹配图像');
subplot(224);imhist(J,64);title('匹配增强直方图');
索引图像直方图
clf;
load trees;%matlab本地图
subplot(121);imshow(X,map);
subplot(122);imhist(X,map);
空间滤波
线性空间滤波
clf;
I=imread('tire.tif');
I2=imfilter(I,[3 3],'replicate');
subplot(121);imshow(I,[]);
subplot(122);imshow(I2,[]);
非线性空间滤波
clf;
I=imread('/MATLAB Drive/shiyan.jpg');
I2=nlfilter(I,[54 54],'mean2');
subplot(121);imshow(I,[]);
subplot(122);imshow(I2,[]);
学习函数colfilt():
%它的主要作用是对图像的每个列向量进行平滑处理,以消除噪声和细节。
%C = colfilt(A, M):
%其中,A是一个二维矩阵,表示输入图像;M是一个正整数,表示滤波器的大小。
%函数的输出C是一个与输入A大小相同的二维矩阵,表示经过列滤波后的图像。
clf;
I=imread('tire.tif');
I2=colfilt(I,[5 5],'sliding','mean');
subplot(121);imshow(I,[]);
subplot(122);imshow(I2,[]);
图像的加减乘除
图像相加
clf
I=imread('rice.png');
J=imread('cameraman.tif');
K=imadd(I,J);
subplot(2,2,1);imshow(I);title('原始图像');
subplot(2,2,2);imshow(J);title('原始图像');
subplot(2,2,3);imshow(K);title('相加图像');
图像减法
I1 = imread('rice.png');
I2 = imread('cameraman.tif');
diff =imsubtract(I1, I2);
subplot(1, 2, 1);
imshow(I1);
title('原始图像');
subplot(1, 2, 2);
imshow(diff);
title('相减后的图像');
图像乘法
I1 = imread('rice.png');
I2 = imread('cameraman.tif');
diff =immultiply(I1, I2);
subplot(1, 2, 1);
imshow(I1);
title('原始图像');
subplot(1, 2, 2);
imshow(diff);
title('相乘后的图像');
图像相除
I1 = imread('rice.png');
I2 = imread('cameraman.tif');
diff =imdivide(I1, I2);
subplot(1, 2, 1);
imshow(I1);
title('原始图像');
subplot(1, 2, 2);
imshow(diff);
title('相除后的图像');