Matlab图像处理教程系列(二)

更多资料添加微信lyj1575304183

单色图像 

在RGB色彩模式下就是指那单独的红色、绿色、蓝色部分。也就是说,一幅完整的图像,是由红色绿色蓝色三个通道组成的。他们共同作用产生了完整的图像。

close all;clear all;clc; %关闭当前所有图形窗口,清空工作空间变量,% 清除工作空间所有变量gamma=0.5; %设定调整线性度取值I=imread('peppers.png'); %读入要处理的图像,并赋值给 IR=I; %将图像数据赋值给 RR (:,:,2)=0; %将原图像变成单色图像,保留红色R(:,:,3)=0;R1=imadjust(R,[0.5 0.8],[0 1],gamma); %利用函数 imadjust 调整 R 的灰度,结果返回 R1G=I; %将图像数据赋值给 GG(:,:,1)=0; %将原图像变成单色图像,保留绿色G(:,:,3)=0;G1=imadjust(G,[0 0.3],[0 1],gamma); %利用函数 imadjust 调整 G 的灰度,结果返回 G1B=I; %将图像数据赋值给 BB(:,:,1)=0; %将原图像变成单色图像,保留蓝色B(:,:,2)=0;B1=imadjust(B,[0 0.3],[0 1],gamma); %利用函数 imadjust 调整 B 的灰度,结果返回 B1I1=R1+G1+B1; %求变换后的 RGB 图像 set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置figure(1)subplot(421),imshow(R); %绘制 R、R1、G、G1、B、B1 图像,% 观察线性灰度变换结果subplot(422),imshow(R1); subplot(423),imshow(G);subplot(424),imshow(G1);subplot(425),imshow(B);subplot(426),imshow(B1);subplot(427),imshow(I);subplot(428),imshow(I1);

代数运算 

图像的代数运算是指多幅图像的加、减、乘、除和一般的线性运算。图像的代数运算不但可以作为复杂图像处理的预处理步骤,还有其它许多用途,如:加法对同一场景的多幅图像求平均,可以有效地降低具有叠加性质的随机噪声等。

1.两个图像相加

=imread('flower.tif'); %读入 flower 图像J=imadd(I,30); %每个像素值增加 30set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置subplot(121),imshow(I); %显示原图像和加常数后的图像

2.图像叠加噪声

  • read('eight.tif'); %读入 eight 图像,赋值给 RGBA=imnoise(RGB,'gaussian',0,0.05); %加入高斯白噪声I=A; %将 A 赋值给 IM=3; %设置叠叠加次数 MI=im2double(I); %将 I 数据类型转换成双精度RGB=im2double(RGB);for i=1:M I=imadd(I,RGB); %对用原图像与带噪声图像进行多次叠加,结果返回给 Iendavg_A=I/(M+1); %求叠加的平均图像set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置subplot(121); imshow(A); %显示加入椒盐噪声后的图像subplot(122); imshow(avg_A); %显示加入乘性噪声后的图像

3.图像相减

A=imread('cameraman.tif');%B=imread('testpat1.png');C=imsubtract(A,B); %进行图像减法set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置figure, %显示原图像及差异图像subplot(121),imshow(C);subplot(122),imshow(255-C);

4.图像相乘

A=imread('rice.png'); %读入原始图像赋值给 A 和 BB=imread('testpat1.png');C=immultiply(A,B); %计算 A 和 B 的乘法,计算结果返回给 C A1=im2double(A); %将 A 和 B 转换成双精度类型,存为 A1 和 B1B1=im2double(B);C1=immultiply(A1,B1); %重新计算 A1 和 B1 的乘积,结果返回给 C1set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置figure(1), % 显示原图像 A 和 Bsubplot(121),imshow(A),axis on;subplot(122),imshow(B),axis on;figure(2), % 显示 uint8 和 double 图像数据格式下,乘积 C 和 C1subplot(121),imshow(C),axis on;;subplot(122),imshow(C1),axis on;;

5.图像相除

I=imread('office_1.jpg'); %读入图像 office_1 和 office_2,并赋值J=imread('office_2.jpg');Ip=imdivide(J,I); %两幅图像相除K=imdivide(J,0.5); %图像跟一个常数相除set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置figure(1); %依次显示四幅图像subplot(121); imshow(I);subplot(122); imshow(J);figure(2)subplot(121); imshow(Ip);subplot(122); imshow(K);

6.逻辑运算

I=imread('rice.png'); %读入图像,赋值给 I 和 JJ=imread('cameraman.tif');I1=im2bw(I); %转化为二值图像J1=im2bw(J);K1=I1 & J1; %实现图像的逻辑“与”运算K2=I1 | J1; %实现图像的逻辑“或”运算K3=~I1; %实现逻辑“非”运算K4=xor(I1,J1); %实现“异或”运算set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置figure, %显示逻辑运算图像subplot(221);imshow(K1),axis on; subplot(222);imshow(K2),axis on;subplot(223);imshow(K3),axis on;subplot(224);imshow(K4),axis on;

图像大小调整 

lose all;clear all;clc; %关闭当前所有图形窗口,清空工作空间变量,% 清除工作空间所有变量[X,map]=imread('trees.tif'); %读入图像J1=imresize(X, 0.25); %设置缩放比例,实现缩放图像并显示J2=imresize(X, 3.5);J3=imresize(X, [64 40]); %设置缩放后图像行列,实现缩放图像并显示J4=imresize(X, [64 NaN]);J5=imresize(X, 1.6, 'bilinear'); %设置图像插值方法,实现缩放图像并显示 J6=imresize(X, 1.6, 'triangle');[J7, newmap]=imresize(X,'Antialiasing',true,'Method','nearest',... 'Colormap','original','Scale', 0.15);%设置图像多个参数,实现缩放图像并显示set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置figure(1) %显示各种缩放效果图subplot(421),imshow(J1);subplot(422),imshow(J2);subplot(423),imshow(J3);subplot(424),imshow(J4);subplot(425),imshow(J5);subplot(426),imshow(J6);subplot(427),imshow(X); subplot(428),imshow(J7);

  • 图像旋转

close all;clear all;clc; %关闭当前所有图形窗口,清空工作空间变量,% 清除工作空间所有变量[A,map]=imread('trees.tif'); %读入图像J1=imrotate(A, 30); %设置旋转角度,实现旋转并显示J2=imrotate(A, -30);J3=imrotate(A,30,'bicubic','crop'); %设置输出图像大小,实现旋转图像并显示J4=imrotate(A,30, 'bicubic','loose'); set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置figure(1) %显示旋转处理结果subplot(221),imshow(J1);subplot(222),imshow(J2); subplot(223),imshow(J3);subplot(224),imshow(J4);

图像剪切 

  •  
[A,map]=imread('peppers.png'); %读入图像rect=[75 68 130 112]; %定义剪切区域X1=imcrop(A,rect); %进行图像剪切set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置subplot(121),imshow(A); %显示原图像rectangle('Position',rect,'LineWidth',2,'EdgeColor','r') %显示图像剪切区域subplot(122),imshow(X1); %显示剪切的图像

[I,map]=imread('peppers.png'); %读入图像Ta = maketform('affine', ...[cosd(30) -sind(30) 0; sind(30) cosd(30) 0; 0 0 1]');% 创建旋转参数结构体Ia = imtransform(I,Ta); %实现图像旋转 Tb = maketform('affine',[5 0 0; 0 10.5 0; 0 0 1]'); %创建缩放参数结构体Ib = imtransform(I,Tb); %实现图像缩放 xform = [1 0 55; 0 1 115; 0 0 1]'; %创建图像平移参数结构体Tc = maketform('affine',xform);Ic = imtransform(I,Tc, 'XData', ... %进行图像平移[1 (size(I,2)+xform(3,1))], 'YData', ...[1 (size(I,1)+xform(3,2))],'FillValues', 255 );Td = maketform('affine',[1 4 0; 2 1 0; 0 0 1]');% 创建图像整体切变的参数结构体Id = imtransform(I,Td,'FillValues', 255); %实现图像整体切变set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置figure %显示结果subplot(221),imshow(Ia),axis on;subplot(222),imshow(Ib),axis on;subplot(223),imshow(Ic),axis on;subplot(224),imshow(Id),axis on;

图像邻域操作 

 邻域操作是指将每个输入的像素值以及其某个邻域的像素值结合处理而得到对应的输出像素值的过程。邻域通常形状规则。如2*2,2*3之类。

  • A = imread('cameraman.tif'); %读取图像A1=im2double(A); %数值类型转换B1 = nlfilter(A1,[4 4],'std2'); %对图像 A 利用滑动邻域操作函数进行处理fun = @(x) max(x(:)); %对图像 A 利用滑动邻域操作函数进行处理B2 = nlfilter(A1,[3 3],fun);B3 = nlfilter(A1,[6 6],fun);set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置subplot(131),imshow(B1); %显示处理后图像subplot(132),imshow(B2); subplot(133),imshow(B3);
I = imread('peppers.png');fun = @(block_struct) imrotate(block_struct.data,30);%获取分离块操作的函数句柄I1 = blockproc(I,[64 64],fun); %进行分离块操作fun = @(block_struct) std2(block_struct.data) ; %获取获取分离块操作的函数句柄I2 = blockproc(I,[32 32],fun); %进行分离块操作fun = @(block_struct) block_struct.data(:,:,[3 1 2]); %获取分离块操作的函数句柄blockproc(I,[100 100],fun,'Destination','brg_peppers.tif');%进行分离块操作set(0,'defaultFigurePosition',[100,100,1000,500]); %修改图形图像位置的默认设置set(0,'defaultFigureColor',[1 1 1]) %修改图形背景颜色的设置figure %显示处理后结果subplot(131),imshow(I1);subplot(132),imshow(I2,[]);subplot(133),imshow('brg_peppers.tif')

​​​​​​​

发布了39 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_34763204/article/details/104248198
今日推荐