Matlab simple image operation example

1. The purpose of the experiment

1. Understand the meaning and means of image operation;
2. Master the algebraic operation and geometric operation method
of the image ; 3. Master the method of adding and removing noise of the
image ; 4. Compare the changes between the images after each operation ;

Second, the experimental task

The experiment requires designing a program to complete the following requirements:
1. Read two images, complete basic algebra operations, and display the results of each operation;
2. Add salt and pepper noise to an image and display the same as the previous image In the image dialog box;
3. Use a for loop to add 100 images with noise and average them, and display the averaged images.
4. Enlarge the image by 1.5 times and shrink by 0.8 times, respectively. The interpolation method uses bilinear interpolation. Rotate the image 45 degrees clockwise to display the rotated image.
5. Design the experiment steps and complete the experiment report.

3. Experimental content

Matlab source program is as follows:

clear
clc
I1=imread('rice.png');
I2=imread('testpat1.png');
I1=im2double(I1);
I2=im2double(I2);
Add=imadd(I1,I2);         %图像加运算
Sub=imsubtract(I1,I2);    %图像减运算
Mul=immultiply(I1,I2);    %图像乘运算
Div=imdivide(I1,I2);      %图像除运算
Abs=imabsdiff(I1,I2);     %图像差的绝对值运算
Com=imcomplement(I1);     %图像补运算
figure(1);
subplot(2,4,1);imshow(I1);title('原图1');
subplot(2,4,2);imshow(I2);title('原图2');
subplot(2,4,3);imshow(Add,[]);title('图像加运算');
subplot(2,4,4);imshow(Sub,[]);title('图像减运算');
subplot(2,4,5);imshow(Mul,[]);title('图像乘运算');
subplot(2,4,6);imshow(Div,[]);title('图像除运算');
subplot(2,4,7);imshow(Abs,[]);title('图像差的绝对值运算');
subplot(2,4,8);imshow(Com,[]);title('图像1的补运算');
Noi=imnoise(I1,'salt & pepper');
figure(2);
subplot(1,3,1);imshow(I1);title('原图1');
subplot(1,3,2);imshow(Noi);title('加入椒盐噪声后的图1');
N1=zeros(256,256);
for i=1:100    %将图像加噪声再通过多次相加求平均的方法祛除噪声
    Noi1=imnoise(I1,'salt & pepper');
    N2=im2double(Noi1);
    N1=N1+N2;
end
N3=N1/100;
subplot(1,3,3);imshow(N3);title('相加求平均的图像');
M1=imresize(I2,1.5,'bilinear');
M2=imresize(I2,0.8,'bilinear');
M11=imrotate(M1,-45,'bilinear');
M22=imrotate(M2,-45,'bilinear');
figure(3);
subplot(1,3,1);imshow(I2);title('原图');
subplot(1,3,2);imshow(M11);title('放大1.5倍并顺时针旋转45°');
subplot(1,3,3);imshow(M22);title('缩小0.8倍并顺时针旋转45°');

4. Experimental results

There are 8 images shown in Figure1, where the first line from left to right is: the original image 1, the original image 2, the image after the two images are added, the image after the two images are subtracted; the second line is from left to On the right are: the image after the two images are multiplied, the image after the two images are divided, the absolute value image of the difference between the two images, and the complementary image of the original image 1.
Three images are shown in Figure2, from left to right: the original image, the image after adding salt and pepper noise to the original image, and adding 100 images with noise and averaging.
Three images are shown in Figure3, from left to right: the original image, 1.5 times zoom and rotate 45 ° clockwise, 0.8 times zoom out and rotate 45 ° clockwise.
Insert picture description here
Figure 1 Image of the program running result 1
Insert picture description here
Figure 2 Image of the program running result 2

Insert picture description here
Figure 3 program running result image 3

V. Thinking of the experiment process

1. About image algebraic operation:
The algebraic operation of an image is essentially an operation between pixels of an image, so the type and size of the two images are required to be the same. When selecting an image, you cannot just look at its size, and sometimes you may encounter a binary image. At this time, you can also use the im2double function to type the two images into the same.
2. Regarding adding noise to the image and then eliminating the noise by adding the average multiple times:
In the program, a for loop is used 100 times to add pepper and salt noise to the image, and then the noise is removed by the average method. If you put the function that adds noise outside the loop, it will not produce such an effect. At this time, it is equivalent to adding 100 times to the same array of data and then averaging, the array data will not change. At the same time, it should be noted that after adding noise, im2double is used to matrix the image into a double type, because the common algebraic operation requires a double precision type.

For more function information, please refer to the MATLAB website MATLAB-function

Published 13 original articles · Like 32 · Visits 10,000+

Guess you like

Origin blog.csdn.net/weixin_43637490/article/details/89320784