少女祈祷中...

数字图像的点运算

数字图像处理

(一)数字图像的点运算

原图:

b1f3ec0b1f11680d2dad1eca9cbce094.jpg

一、数字图像的点运算

1.灰度直方图

1
2
3
4
5
6
7
8
9
10
11
img=imread('C:\Users\24682\Desktop\lzy\face\lzy.jpg');
[m,n]=size(img);
img1=rgb2gray(img);
subplot(1,3,1);
imshow(img1);
subplot(1,3,2);
imhist(img1,32);
[counts,x]=imhist(img1,32);
counts=counts/m/n;
subplot(1,3,3);
stem(x,counts);

展示的效果如下图:

_6Q_VOU_`S__`VM_7_2___9.png

2.灰度线性变换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
img=imread('C:\Users\24682\Desktop\lzy\face\lzy.jpg'); 
img=rgb2gray(img);
img1=im2double(img);
img2=(img1.^5);
img3=(img1.^1.5);
img4=(img1.^0.5);
img5=(img1.^0.1);
img6=(img1.^0.04);
subplot(2,3,1),imshow(img);
subplot(2,3,2),imshow(img2);
subplot(2,3,3),imshow(img3);
subplot(2,3,4),imshow(img4);
subplot(2,3,5),imshow(img5);
subplot(2,3,6),imshow(img6);

展示的效果如下图:

9NQ_4OVWCVPCH57R37I6E_7.png

3.分段灰度线性变换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
I = imread('C:\Users\24682\Desktop\lzy\face\lzy.jpg'); 
I =rgb2gray(I);
[M,N] = size(I);
I = im2double(I);
J = zeros(M,N);
X1 = 0.3; Y1 = 0.15;
X2 = 0.7; Y2 = 0.85;
for i = 1:M
for j = 1:N
if I(i,j) < X1
J(i,j) = Y1*I(i,j)/X1;
elseif I(i,j) > X2
J(i,j) = (I(i,j)-X2)*(1-Y2)/(1-X2)+Y2;
else
J(i,j) = (I(i,j)-X1)*(Y2-Y1)/(X2-X1)+Y1;
end
end
end
subplot(2,2,1), imshow(I); subplot(2,2,2), imshow(J);
subplot(2,2,3), imhist(I); subplot(2,2,4), imhist(J

![OM7H5__E6`_9V~Q56X65B@W.png](https://s2.loli.net/2024/04/19/f9TDJeS8rGsbXCc.png)

4.灰度非线性变换

(1)对数变换

1
2
3
4
5
6
img=imread('C:\Users\24682\Desktop\lzy\face\lzy.jpg');
img=rgb2gray(img);
img1=im2double(img);
img2=2*log(img1+1);
subplot(1,2,1),imshow(img);title(‘原始图像');
subplot(1,2,2),imshow(img2);title(‘对数变换后的图像');

(2) 伽马变换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
img=imread('C:\Users\24682\Desktop\lzy\face\lzy.jpg');
img=rgb2gray(img);
img1=im2double(img);
img2=(img1.^5);
img3=(img1.^1.5);
img4=(img1.^0.5);
img5=(img1.^0.1);
img6=(img1.^0.04);
subplot(2,3,1),imshow(img);
subplot(2,3,2),imshow(img2);
subplot(2,3,3),imshow(img3);
subplot(2,3,4),imshow(img4);
subplot(2,3,5),imshow(img5);
subplot(2,3,6),imshow(img6);

5.灰度阈值变换

灰度阙值变换可以将一幅灰度图像转换成黑白的二值图像。用户指定一个起到分界线作用的灰度值,如果图像中某像素的灰度值小于该灰度值,则将该像素的灰度值设置为0,否则设置为255;起到分界线作用的灰度值称为阈值,灰度的阈值变换也常被称为阈值化、或二值化。

1
2
3
4
5
6
7
8
img=imread('C:\Users\24682\Desktop\lzy\face\lzy.jpg'); 
img=rgb2gray(img);
A=graythresh(img);
A1=im2bw(img,A);
A2=im2bw(img,50/255);
subplot(1,3,1),imshow(img);
subplot(1,3,2),imshow(A1);
subplot(1,3,3),imshow(A2);

(二)图像的几何运算

1.图像平移

1
2
3
4
5
6
7
8
9
10
11
12
13
function [J] = move(I,a,b)
[M,N]=size(I);
J=I;
for i=1:M
for j=1:N
if((i-a>0)&(i-a<M)&(j-b>0)&(j-b<N))
J(i,j)=I(i-a,j-b);
else
J(i,j)=0;
end
end
end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
A = imread('C:\Users\24682\Desktop\lzy\face\lzy.jpg');
I = rgb2gray(A);
a=70;b=80;
J1=imtranslate(I,[a,b]);
subplot(2,2,1);
imshow(J1);
J2=imtranslate(I,[-a,b]);
subplot(2,2,2);
imshow(J2);
J3=imtranslate(I,[a,-b]);
subplot(2,2,3);
imshow(J3);
J4=imtranslate(I,[-a,-b]);
subplot(2,2,4);
imshow(J4);

2.图像镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function [J] = mirror(I,n)
[M,N]=size(I);
J=I;
if(n==1)
for i=1:M;
for j=1:N;
J(i,j)=I(M-i+1,j);
end
end
elseif(n==2)
for i=1:M;
for j=1:N;
J(i,j)=I(i,N-j+1);
end
end
elseif(n==3)
for i=1:M;
for j=1:N;
J(i,j)=I(M-i+1,N-j+1);
end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
A = imread('C:\Users\24682\Desktop\lzy\face\lzy.jpg');
I = rgb2gray(A)
subplot(2,2,1);
imshow(I);
J1=mirror(I,1);
J2=mirror(I,2);
J3=mirror(I,3);
subplot(2,2,2);
imshow(J1);
subplot(2,2,3);
imshow(J2);
subplot(2,2,4);
imshow(J3);

3.图像转置

1
2
3
4
5
6
7
8
9
A = imread('C:\Users\24682\Desktop\lzy\face\lzy.jpg');
I = rgb2gray(A)
subplot(1,2,1);
imshow(I);
title((‘原图'));
J=I';
subplot(1,2,2);
imshow(J);
title(‘图像转置')

4.图像缩放

1
2
3
4
5
6
7
8
9
10
11
12
13
A = imread('C:\Users\24682\Desktop\lzy\face\lzy.jpg');
I = rgb2gray(A)
subplot(2,3,1);
imshow(I);
xlabel(‘原始图形')
J1=imresize(I,0.3);
subplot(2,3,2);
imshow(J1);
xlabel(‘缩小0.3倍')
J2=imresize(I,5);
subplot(2,3,3);
imshow(J2);
xlabel(‘放大5')
1
2
3
4
5
6
7
8
9
10
11
12
J3=imresize(I,0.5,'nearest');
subplot(2,3,4);
imshow(J3);
xlabel(‘临近插值缩小0.5')
J4=imresize(I,0.3,'bilinear');
subplot(2,3,5);
imshow(J4);
xlabel(‘双线性插值缩小0.3')
J5=imresize(I,0.3,'bicubic');
subplot(2,3,6);
imshow(J5);
xlabel(‘双立方插值缩小0.3')

5.图像旋转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
imread('C:\Users\24682\Desktop\lzy\face\lzy.jpg')
I=rgb2gray(A);
subplot(1,2,1);
imshow(I);
title(‘原图');
[M,N]=size(I);
ang=30;
J=I;
for i=1:M
for j=1:N
x=floor(i*cos(ang*pi/180)-j*sin(ang*pi/180));
y=floor(j*cos(ang*pi/180)+i*sin(ang*pi/180));
if((x<M) & (y<N)&(x>0) & (y>0))
J(i,j)=I(x,y);
else
J(i,j)=0;
end
end
end
subplot(1,2,2);
imshow(J);
title(‘旋转后图')