Digital Image Processing: Experiment 1

digital image processing

Use Matlab to process the image and record the experiment;
references: Fourier transform , image enhancement , histogram, equalization

Experiment 1: Commonly used mathematical transformations

1. Matlab uses addition to superimpose two different images (custom)

  1. Matlab program: (version 2016a)
1.	clear;  
2.	clc;  
3.	I= imread('OIP-C.jpg');%读入图片1  
4.	J=imread('OIP-D.jpg');%读入图片2  
5.	Is = stretchlim(I);%计算图片1的灰度图像的最佳输入区间  
6.	Js = stretchlim(J);%同理计算图片2  
7.	Ix=imadjust(I,Is,[0;1]);%调整图片1灰度图像的灰度范围  
8.	Jx=imadjust(J,Js,[0;1]);%同理调整图片2  
9.	P=imadd(Ix,Jx);%实现2副图相加运算  
10.	subplot(1,3,1);imshow(I);%创建子图、显示图像1  
11.	title('图像1');  
12.	subplot(1,3,2),imshow(J);%创建子图、显示图像2  
13.	title('图像2');  
14.	subplot(1,3,3),imshow(P);%创建子图、显示图像3  
15.	title('两幅图像进行相加操作后效果');  

  1. Original image, processed image:
    insert image description here
  2. Description of the process and results of the processing:

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-rOzwNcJ4-1667490230485)(file:///c:/user/default/AppData/Local/Temp/msohtmlclip1 /01/clip_image003.gif)]

As shown in the algorithm flow chart above, after the program initializes and clears the data stored in the workspace, when superimposing two different images, you first need to read two different images through the imread function in Matlab and assign values ​​to the two images respectively Give a receiving variable I, J;

Considering that two images will be directly superimposed and there will be double exposure, we need to perform grayscale processing on the image, that is, first calculate the best input interval of the grayscale image of the two images through the stretchlim function, and assign the interval value to Is and Js variables. Then use the imadjust function to adjust the grayscale range of the corresponding grayscale image on the two images, and obtain two grayscale processed images Ix and Jx; finally, use the imadd function to superimpose Ix and Jx to obtain the two images after adding The picture of P. Before the end of the program, two original pictures I and J and the processed picture P are displayed respectively; from the effect picture, we can see that the two pictures are superimposed successfully.

2. Matlab uses subtraction to subtract two images (customized) of the same scene but with differences;

  1. Matlab program: (version 2016a)
1.	clear;  
2.	clc;  
3.	I= imread('OIP-C.jpg');%读入图片1  
4.	J=imread('OIP-D.jpg');%读入图片2  
5.	P=imadd(I,J);%得到未进行灰度处理的加法图  
6.	imwrite(P,'OIP-E.jpg');%把处理过的图片写出保存  
7.	%图像相减  
8.	%实现2副存在差异的图相减运算
9.	Px=imsubtract(P,I);
10.	%实现2副相同图做减法运算 
11.	%Px=imsubtract(P,P);
12.	subplot(1,3,1),imshow(P),xlabel('A');  
13.	subplot(1,3,2),imshow(I),xlabel('B');  
14.	subplot(1,3,3),imshow(Px),xlabel('A-B');  

  1. Original image, processed image;

Scenario 1: There are differences in the same scene Image subtraction:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-laCs3SvJ-1667490230486)(file:///c:/user/default/AppData/Local/Temp/msohtmlclip1 /01/clip_image002.jpg)]

Scenario 2: Subtraction of the same two images:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-Q60Dqwhd-1667490230487)(file:///c:/user/default/AppData/Local/Temp/msohtmlclip1 /01/clip_image004.jpg)]

  1. Description of the process and results of the processing:

insert image description here

As shown in the algorithm flow chart above, after the program initializes and clears the workspace to store data, it first needs to obtain two images of the same scene but with differences. Therefore, it is hoped to combine the above-mentioned addition algorithm and calculate the obtained addition image as the other two images. There are differences in image usage in the same scene;

To obtain image 3 without grayscale operation, you need to read in images 1 and 2 first, and use the imwrite function to save image 3. After obtaining two images with differences, use the imsubtract function to subtract the two images, and finally display the processed image;

​ There are two kinds of subtraction operations. Two kinds of images are obtained by modifying the code. One is to subtract two images with differences in the same scene. The effect is as follows, removing the same elements in image A and image B; the other is The situation is that two identical images are subtracted, and it can be seen that the entire image after the subtraction process is completely black, which means that the two subtracted images are the same;

3. Use Matlab to calculate the power spectrum and phase spectrum of an image (customized, it is recommended to have obvious texture features).

  1. Matlab program: (version 2016a)
1.	clc;  
2.	clear;  
3.	img = imread('hua.jpg');  
4.	imggray = rgb2gray(img);%灰度处理  
5.	imgf = fft2(imggray);%傅里叶变换二维快速傅里叶变换函数  
6.	imgfshift = fftshift(imgf);%频谱图中零频率成分移动至频谱图中心  
7.	%获得傅里叶变换的幅度谱  
8.	imgA = log(1+abs(imgfshift));%对数变换,压缩动态范围  
9.	%由幅度谱求出功率谱 对信号傅里叶变换--幅度谱--取模求平方--功率谱  
10.	imgB=abs(imgfshift).^2;  
11.	imgB=10 * log10(imgB);  
12.	%获得傅里叶变换的相位谱  
13.	imgPhase = log(angle(imgfshift)*180/pi);  
14.	%变换后图像矩阵大多是复试矩阵,包含实部虚部  
15.	%需要对数组元素进行绝对值处理求其复数矩阵的模,在进行对数变换进行显示  
16.	imgPhase=log(abs(imgPhase)+1);  
17.	subplot(2,2,1);  
18.	imshow(img);  
19.	title('原图像');  
20.	subplot(2,2,2);  
21.	imshow(imggray);  
22.	title('灰度处理后的原图像');  
23.	subplot(2,2,3);  
24.	imshow(imgB,[]); %显示图像的功率谱,参数’[]'是为了将其值线性拉伸  
25.	title('图像功率谱');  
26.	subplot(2,2,4);  
27.	imshow(imgPhase,[]);  
28.	title('图像相位谱');  

  1. Original image, processed image;
    insert image description here

  2. A description of the process and results of the processing;

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-BHhb2NlV-1667490230492)(file:///c:/user/default/AppData/Local/Temp/msohtmlclip1 /01/clip_image001.gif)]

As shown in the algorithm flow chart above, after the program initializes and clears the workspace to store data, first read in a picture 1, and use the rgb2gray function to obtain its grayscale image; after obtaining the grayscale image, obtain the spectrum diagram according to Fourier transform, Select the two-dimensional fast Fourier transform function fft2 to process the grayscale image, and use the fftshift function on the spectrogram to move the 0 frequency component in the spectrogram to the center of the spectrogram; then respectively from the spectrogram to the power map, and from the spectrogram to the phase map The formula to calculate the power diagram and phase diagram of the picture;

When running the program for the first time, a prompt message appears: "Warning: Display the real part of the complex input item". After querying relevant information, it is known that if the image is directly displayed by the imshow function after Fourier transform, the above error will be prompted. Therefore, it is necessary to perform secondary processing on the processed phase map and power map, that is, to use the abs function to obtain the modulus of its complex matrix, and to optimize the image display with logarithmic transformation.

Guess you like

Origin blog.csdn.net/TianHW103/article/details/127680724