当前位置:文档之家› 图像算术运算及逻辑运算

图像算术运算及逻辑运算

图像算术运算
代数运算函数:
Imabsdiff 两幅图像的绝对差值
Imadd 两幅图像的加法
Imcomplement 补足一幅图像(反像-余数)
Imdivide 两幅图像的除法
Imlincomb 两幅图像的线性组合
Immultiply 两幅图像的乘法
Imsubtract 两幅图像的减法
Z = IMLINCOMB(K1,A1,K2,A2, ..., Kn,An) 即:计算Z=K1*A1 + K2*A2 + ... +Kn*An 例子:
%%f3.2(线性点运算)
rice=imread('rice.png');
I=double(rice);
J=I*0.43+60;
rice2=uint8(J);
subplot(1,2,1),imshow(rice);
subplot(1,2,2),imshow(rice2);
%%f3.7_8(加法运算:两图像相加)
i=imread('rice.png');
j=imread('cameraman.tif');
k=imadd(i,j);
subplot(1,3,1),imshow(i);
subplot(1,3,2),imshow(j);
subplot(1,3,3),imshow(k);
%%imshow(k);
%%f3.9(加法运算:增加亮度值)
RGB=imread('cameraman.tif');
RGB2=imadd(RGB,50);
RGB3=imadd(RGB,100);
subplot(1,3,1),imshow(RGB);
subplot(1,3,2),imshow(RGB2);
subplot(1,3,3),imshow(RGB3);
%%f3.10(减法运算—减背景)
rice=imread('rice.png');
background=imopen(rice,strel('disk',15));
rice2=imsubtract(rice,background);
subplot(1,2,1),imshow(rice);
subplot(1,2,2),imshow(rice2);
%%f3.10(减法运算—图像相减)
%要求图像尺寸相同
rice=imread('rice.png');
cameraman=imread('cameraman.tif');
c=imsubtract(rice,cameraman);
subplot(1,3,1),imshow(rice); subplot(1,3,2),imshow(cameraman); subplot(1,3,3),imshow(c);
%采用线性组合函数实现加减运算
rice=imread('rice.png');
cameraman=imread('cameraman.tif');
c=imlincomb(1.0, rice, -1.0,cameraman);
subplot(1,3,1),imshow(rice); subplot(1,3,2),imshow(cameraman); subplot(1,3,3),imshow(c);
%%f3.11(乘法运算:j,k取值不同)
i=imread('moon.tif');
j=immultiply(i,1.2);
k=immultiply(i,2);
subplot(1,3,1),imshow(i);
subplot(1,3,2),imshow(j);
subplot(1,3,3),imshow(k);
%%f3.12(除法运算:j,k,l取值不同)
rice=imread('rice.png');i=double(rice);
j=i*0.43+90;k=i*0.1+90;l=i*0.01+90;
rice2=uint8(j);
rice3=uint8(k);
rice4=uint8(l);
ip=imdivide(rice,rice2);
ik=imdivide(rice,rice3);
il=imdivide(rice,rice4);
subplot(3,3,1);imshow(rice2);
subplot(3,3,2);imshow(rice3);
subplot(3,3,5);imshow(rice4);
subplot(3,3,6);imshow(il,[]);
subplot(3,3,7);imshow(ip,[]);
subplot(3,3,8);imshow(ik,[]);
subplot(3,3,9);imshow(il,[]);
%%f3.13(四则运算1:imadd_imdivide混合用法)i=imread('rice.png');
i2=imread('cameraman.tif');
l=imadd(i,i2);
k=imdivide(imadd(i,i2),6);
imshow(l);
figure,imshow(k,[]);
%%f3.13(四则运算2:imlincomb用法)
X=imread('rice.png');
Y=imread('rice.png');
A=0.5;B=2.1;C=2.3;
z1=imlincomb(A,X,C);
z2=imlincomb(A,X,B,Y);
figure,imshow(X);
figure,imshow(z1,[]);
figure,imshow(z2,[]);
%逻辑运算
i=zeros(280,280);
j=zeros(280,280);
%产生图像i
for m=1:280
for n=1:280
if sqrt((m-80)^2+(n-80)^2)<=40
i(m,n)=1;
end;
if sqrt((m-80)^2+(n-200)^2)<=40 i(m,n)=1;
end;
if sqrt((m-200)^2+(n-80)^2)<=40 i(m,n)=1;
end;
if sqrt((m-200)^2+(n-200)^2)<=40 i(m,n)=1;
end;
end
end
%产生图像j
j(100:180,100:180)=1;
for m=1:280
for n=1:280
if sqrt((m-140)^2+(n-140)^2)<=25 j(m,n)=0;
end
end
end
imshow(i,[]);
figure
imshow(j);
%逻辑或
k=i|j;
figure
imshow(k);
%逻辑与
k1=i&j;
figure
imshow(k1);
%逻辑异或
k2=xor(i,j);
figure
imshow(k2);。

相关主题