图像点运算(matlab)

1.简言

在MATLAB中,数字图像数据是以矩阵形式存放的,矩阵的每个元素对应着一个像素点的像素值。点运算又称对比增强、对比拉升或灰度变换,通过图像中的每一个像素值进行运算的图像处理方式,其运算结果不会改变图像内像素点之间的空间关系,运算的数学关系式为:
B(x,y) = f[A(x,y)]
A(x,y) —原图像;
B(x,y) —经过点运算处理后的图像;
f —点运算的关系函数;

2.图像点运算

2.1 线性灰度变换

假定原图像A(x,y)的灰度变换范围为[a,b],处理后的图像B(x,y)的灰度扩展为[c,d],线性灰度变换运算的数学表达式为:B(x,y) = (d-c)/(b-a)[(x,y)-a]+a。
J = imadjust(I,[low_in high_in],[low_out high_out]) ---- 对图像进行灰度变换, 将 I 中的强度值映射到 J 中的新值,以使 low_in 和 high_in 之间的值映射到 low_out 到 high_out 之间的值。

close all;clear all;clc;
gamma = 0.5;//设定调整线性度取值
I = imread('输入图片名称');
R = I;//赋值给R
R(:,:,2) = 0;//将原图像变成单色图像,保留红色
R(:,:,3) = 0;
R1 = imadjust(R,[0.5,0.8],[0 1],gamma);//调节R的灰度

G = I;//赋值给R
G(:,:,1) = 0;//将原图像变成单色图像,保留绿色
G(:,:,3) = 0;
G1 = imadjust(R,[0,0.3],[0 1],gamma);//调节G的灰度

B = I;//赋值给R
B(:,:,1) = 0;//将原图像变成单色图像,保留蓝色
B(:,:,2) = 0;
B1 = imadjust(R,[0.5,0.8],[0 1],gamma);//调节B的灰度

I1 = R1+G1+B1;//变换后的RGB图像
figure,
subplot(121),imshow(R);
subplot(122),imshow(R1);

figure,
subplot(121),imshow(G);
subplot(122),imshow(G1);

figure,
subplot(121),imshow(B);
subplot(122),imshow(B1);

figure,
subplot(121),imshow(I);
subplot(122),imshow(I1);

结果显示如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.2 分段线性灰度变换

为了突出图像中感兴趣的目标或者灰度区间,可以采用分段线性,需要将图像细节灰度拉伸、对比度增强。数学表达式g(x,y)如下:
当0<f(x,y)<a时:g(x,y) = (c/a)f(x,y);
当0<= f(x,y) <= b时:g(x,y) = [(d-c)/(b-a)]f(x,y) + c;
当b<=f(x,y)<=Fmax<=Gmax时:[(Gmax - d)(Fmax - b)]([f(x,y)-b+d]。

close all;clear all;clc;
R = imread('输入图片名称');
J = rgb2gray(R);
[M,N] = size(J);
x = 1;y = 1;
for x=1:M
	for y=1:N
		if(J(x,y)<=35);//对灰度图像J进行分段处理,处理后的结果返回给矩阵H
			H(x,y) = J(x,y)*10;
		elseif(J(x,y)>35&J(x,y)<=75);
			H(x,y) = (10/7)*[J(x,y)-5]+50;
		else(J(x,y)>75);
			H(x,y) = (105/180)*[J(x,y)-75]+150;
		end
	end
end
figure,
subplot(121),imshow(J);
subplot(122),imshow(H);

结果如下:
在这里插入图片描述

2.3非线性灰度变换

当输出图像的像素点灰度值和输入图像的像素点灰度值不满足线性关系时,这种灰度变换都称为非线性灰度变换,数学表达式如下:
g(x,y) = a+[ln(f(x,y)+1]/(b*lnc),其中a,b,c是为了调整曲线的位置和形状而引入的参数。图像可以通过对数变换可扩展低值灰度,压缩高值灰度。

close all;clear all;clc;
R = imread('输入图片名称');
G = rgb2gray(R);
J = double(G);
H = (log(J+1))/10;
figure,
subplot(121),imshow(G);
subplot(122),imshow(H);

结果所下:
在这里插入图片描述

3、图像代数运算

图像迭代运算是指将两幅或者多幅图片通过对像素之间的加、减、乘、除运算得到输出图像的方法,运算的数学表达式如下:
C(x,y) = A(x,y) + B(x,y)
C(x,y) = A(x,y) - B(x,y)
C(x,y) = A(x,y) * B(x,y)
C(x,y) = A(x,y) / B(x,y)

1. 图像的加法运算

将一幅图像的内容叠加到另一幅图像上,生成叠加效果,或给图像中的每个像素叠加常数改变图像的亮度。
(1) Z = imadd(X,Y) ------X,Y为大小相等的图像矩阵;
两幅图片叠加:

close all;clera all;clc;
I = imread('输入图片名称');
J = imread('输入图片名称');
K = imadd(I,J);
figure,
sunplot(131),imshow(I);
subplot(132),imshow(J);
subplot(133),imshow(K);

在这里插入图片描述
图像变亮:

close all;clear all;clc;
I = imread('输入图片名称');
J = imadd(I,30);
figure,
subplot(121),imshow(I);
subplot(122),imshow(J);

在这里插入图片描述
图像的加法运算的另一个重要应用是通过同一幅图像叠加取平均,消除原图像中的附加噪声。
(2) J = imnoise(I,type,Parameters) ----- I:原图像;type:噪声类型[高斯噪声(gaussian)、邻均值的高斯噪声(localvar)、泊松噪声(poisson)、椒盐噪声(salt&pepper)和乘性噪声(speckle)];Parameters:不同类型的噪声参数。

close all;clear all;clc;
RGB = imread('输入图片名称');
A = imnoise(RGB,'gaussian',0,0.05);
I = A;
M = 3;//叠加的次数
I = imtodouble(I);
RGB = imtodouble(RGB);
for i=1:M
	I = imadd(I,RGB);//对原图与带噪声的图进行多次叠加
end
avg_A = I/(M+1);//求叠加平均像
figure,
subplot(121),imshow(A);//椒盐噪声后的图像
subplot(122),imshow(avg_A);//乘性噪声后的图像

在这里插入图片描述

2. 图像的减法运算

也称为差分法,是一种检测图像变化及运动物体的图像处理方法,经常用作相同场景的差异,是检测同一场景下两幅图像之间的变化或是混合图像分离。
Z = imsubtract(X,Y)
(1) 图像取反

close all;clear all;clc;
A = imread('输入图片名称');
B = imread('输入图片名称');
C = imsubtract(A,B);
figure,
subplot(121),imshow(C);
subplot(122),imshow(255-C);

在这里插入图片描述
(2) 混合图分离
Z = imabsdiff(X,Y)

close all;clear all;clc;
A = imread('输入图片名称');
[m,n] = size(A);
B = imread('输入图片名称');
C = B
A = im2double(A);
B = im2double(B);
C = im2double(C);
for i=1:m//将图像B和图像A叠加,结果赋值给C
	for j=1:n
		C(i,j) = B(i,j)+A(i,j);
	end
end
D = imabsdiff(C,B);//求C和B的差异
figure,
subplot(121),imshow(A);
sunplot(122),imshow(B);
figure,
subplot(121),imshow(C);
subplot(122),imshow(D);

在这里插入图片描述
在这里插入图片描述

3 .图像的乘法运算

主要实现两个功能:
(1)、掩模操作,即屏蔽图像的某些部分。
(2)、一幅图片乘以一个常数因子,如果常数大于1,将增强图像的亮度,反之则会使图像变暗。

图片乘法运算
Z = immultiply(X,Y)

>> I = imread('输入图片名称');
>> J = imread('输入图片名称');
>> C = immultiply(I,J);
>> I1 = im2double(I);
>> J1 = im2double(J);
>> C1 = immultipy(I1,J1);
>> C1 = immultiply(I1,J1);
>> figure,
>> subplot(221),imshow(I),axis on;
>> subplot(222),imshow(J),axis on;
>> subplot(223),imshow(C),axis on;
>> subplot(223),imshow(C1),axis on;
>> subplot(223),imshow(C),axis on;
>> subplot(224),imshow(C1),axis on;

图片亮度控制

>> I = imread('E:\桌面\个人文件\matlab\images\ad1.tif');
>> I = imread('E:\桌面\个人文件\matlab\images\1.tif');
>> B = immultiply(I,1.5);
>> C = immultiply(I,0.5);
>> figure,
>> subplot(131),imshow(I);
>> subplot(132),imshow(B);
>> subplot(133),imshow(C);

在这里插入图片描述

4.图像的除法运算

两幅图像相应像素值的变化率,而不是每个像素的绝对差异,因而图像除法也称为比率变换,常运于校正成像设备的非线性变换。

Z = imdivide(X,Y)

I = imread('E:\桌面\个人文件\matlab\images\ad1.tif');
J = imread('E:\桌面\个人文件\matlab\images\ad1.tif');
K = imdivide(J,I);
G = imdivide(J,0.5);
figure,
subplot(221),imshow(I);
subplot(222),imshow(J);
subplot(223),imshow(K);
subplot(224),imshow(G);

在这里插入图片描述

5.图像的其他代数运算

(1)、绝对值差函数:Z = imabsdiff(X,Y)

(2)、图像求补函数:IM2 = imcomplement(IM) IM图像矩阵IM的所有元素求补

(3)、图像运算的线性组合函数:Z = imlincomb(K1,A1,K2,A2,…,Kn,An) A(计算图像矩阵) K(加权和)

6.图像的大小改变

(1)、B = imresize(A,m) ------ m 倍数;
(2)、B = imresize(A,m,method) ------ method 的几种可选值:‘nearest’(默认值)最近邻插值;'bilinear’双线性插值;'bicubic’双三次插值。

7.图像的逻辑运算

常用的逻辑运算有与、或、非、或非、与非和异或。
与、或、非和异或

>> I = imread('E:\桌面\个人文件\matlab\images\1.tif');
>> J = imread('E:\桌面\个人文件\matlab\images\2.tif');
>> I1 = im2bw(I);
>> J1 = im2bw(J);
>> K1 = I1 & J1;
>> K2 = I1 | J1;
>> K3 = ~I1;
>> K4 = xor(I1,J1);
>> figure,
>> subplot(321),imshow(I);
>> subplot(322),imshow(J);
>> subplot(323),imshow(K1);
>> subplot(324),imshow(K2);
>> subplot(325),imshow(K3);
>> subplot(326),imshow(K4);

在这里插入图片描述
或非、与非

>> I = imread('E:\桌面\个人文件\matlab\images\1.tif');
>> J = imread('E:\桌面\个人文件\matlab\images\2.tif');
>> I1 = im2bw(I);
>> J1 = im2bw(J);
>> H = ~(I1 | J1);
>> G = ~(I1 & J1);
>> figure,
>> subplot(221),imshow(I1);
>> subplot(222),imshow(J1);
>> subplot(223),imshow(H);
>> subplot(224),imshow(G);

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_56260304/article/details/127168091