MATLAB [Digital Image Processing] Experiment 1: Basic operations of image processing (translation, amplification, reduction, rotation, interpolation)

1. Experimental purpose

1. Be familiar with and master the use of MATLAB tools; 
2. Able to read, display, store, translate, mirror, enlarge, reduce and rotate images;
3. Master commonly used interpolation methods and understand their advantages and disadvantages.

2. Experimental environment

Matlab 2020B

3. Experimental content

topic

1. Read an RGB image, convert it into a grayscale image and a binary image, display the RGB image and grayscale image respectively in the same window, add a text title, and save the result in file form Save to disk.
2. Perform translation, mirroring (horizontal mirroring, vertical mirroring) enlargement, reduction and rotation operations on the image. The enlargement and rotation operations are implemented using nearest neighbor interpolation and bilinear interpolation methods respectively. Requirements Write your own code to implement the algorithm and analyze the advantages and disadvantages of the two interpolation methods.

related information

In the matlab environment, the image can be read in by calling the imread() function. The default storage method is a matrix, which is divided into three channels: RGB. The value type is uint8, that is, the value range is 0~255.

The rgb2gray() function eliminates the hue and saturation of the image stored in the three RGB channels, retains the brightness, and converts the RGB image into a grayscale image.
The im2bw(I,level) function converts the image into a binary image based on a threshold. The input image can be an RGB image or a grayscale image. The level parameter is a decimal value ranging from 0 to 1. The pixels greater than the threshold are turned into white (1), and other pixels are turned into black (0).

When the image is translated, let the coordinate of a certain pixel be ( x 0 , y 0 ) (x_0,y_0) (x0,and0),经过平Shift ( t x , t y ) (t_x,t_y) (tx,ty), 达到材标为 ( x 1 , y 1 ) (x_1,y_1) (x1,and1)。该关系可记为: { x 1 = x 0 + t x y 1 = y 0 + t y \begin{cases}x_1=x_0+t_x\\y_1=y_0+t_y\end{cases} { x1=x0+txand1=and0+ty, define the following: ( x 1 y 1 1 ) = ( x 0 y 0 1 ) ( 1 0 0 0 1 0 t x t y 1 ) \begin{ pmatrix}x_1&y_1&1\end{pmatrix}=\begin{pmatrix}x_0&y_0&1\end{pmatrix}\begin{pmatrix}1&0&0\\0&1&0\\t_x&t_y& ;1\end{pmatrix}(x1and11)=(x0and01) 10tx01ty001 . When implementing, remember the height of the image as h h h,宽为 w w w,需要判断 x 1 , y 1 x_1,y_1 x1,and1give h , w h,w h,The size relationship of w cannot cause the subscript to cross the boundary.

When the image is mirrored horizontally, the width of the image is recorded as w w w. Original image raw point ( x 0 , y 0 ) (x_0,y_0) (x0,and0)变为 ( w − x 0 , y 0 ) (w-x_0,y_0) (wx0,and0),即: { x 1 = w − x 0 y 1 = y 0 \begin{cases}x_1=w-x_0\\y_1=y_0\end{cases} { x1=Inx0and1=and0. Record it in matrix form: { x 1 = x 0 + t x y 1 = y 0 + t y \begin{cases}x_1=x_0+t_x\\y_1=y_0+t_y\end{cases } { x1=x0+txand1=and0+ty can be written in matrix form as: ( x 1 y 1 1 ) = ( x 0 y 0 1 ) ( − 1 0 0 0 1 0 w 0 1 ) \ begin{pmatrix}x_1&y_1&1\end{pmatrix}=\begin{pmatrix}x_0&y_0&1\end{pmatrix}\begin{pmatrix}-1&0&0\\0&1&0\\ w&0&1\end{pmatrix} (x1and11)=(x0and01) 10w010001

When the image is mirrored vertically, record the image height as h h h. Original image raw point ( x 0 , y 0 ) (x_0,y_0) (x0,and0)变为 ( x 0 , h − y 0 ) (x_0,h-y_0) (x0,hand0),即: { x 1 = x 0 y 1 = h − y 0 \begin{cases}x_1=x_0\\y_1=h-y_0\end{cases} { x1=x0and1=hand0. Written in matrix form: ( x 1 y 1 1 ) = ( x 0 y 0 1 ) ( 1 0 0 0 − 1 0 0 h 1 ) \begin{pmatrix}x_1&y_1& ;1\end{pmatrix}=\begin{pmatrix}x_0&y_0&1\end{pmatrix}\begin{pmatrix}1&0&0\\0&-1&0\\0&h&1\end {pmatrix} (x1and11)=(x0and01) 10001h001

Image enlargement is divided into two steps: 1. Create new pixels. 2. Assign a value to the corresponding position of the new pixel.

When using the nearest neighbor interpolation method, the nearest pixel in the original image is directly used to assign a value to the new image.

When using bilinear interpolation method, remember the original image size m ∗ n m*n mn,New image size a ∗ b a*b ab,则新图的材标 ( x 0 ′ , y 0 ′ ) (x_0',y_0') (x0,and0)对应原图 ( x 0 , y 0 ) = ( x 0 ′ ∗ m / a , y 0 ′ ∗ n / b ) (x_0,y_0)=(x_0'*m/a,y_0'*n/b) (x0,and0)=(x0m/a,and0n/b).找arrival ( x 0 , y 0 ) (x_0,y_0) (x0,and0)The nearest four integer points are recorded as ( x 1 , y 1 ) , ( x 1 , y 2 ) , ( x 2 , y 1 ) , ( x 2 , y 2 ) (x_1,y_1),(x_1,y_2),(x_2,y_1),(x_2,y_2) (x1,and1),(x1,and2),(x2,and1),(x2,and2), the gray value at the corresponding point is recorded as P 11 , P 12 , P 21 , P 22 P_{11 },P_{12},P_{21},P_{22} P11,P12,P21,P22
( x 0 ′ , y 0 ′ ) (x_0',y_0') (x0,and0)的灰度值为
y 2 − y 0 y 2 − y 1 ( x 2 − x 0 x 2 − x 1 P 11 + x 0 − x 1 x 2 − x 1 P 21 ) + y 0 − y 1 y 2 − y 1 ( x 2 − x 0 x 2 − x 1 P 12 + x 0 − x 1 x 2 − x 1 P 22 ) \frac{y_2-y_0}{y_2-y_1}(\frac{x_2-x_0}{x_2-x_1}P_{11}+\frac{x_0-x_1}{x_2-x_1}P_{21})+\frac{y_0-y_1}{y_2-y_1}(\frac{x_2-x_0}{x_2-x_1}P_{12}+\frac{x_0-x_1}{x_2-x_1}P_{22}) and2y1and2y0(x2x1x2x0P11+x2x1x0x1P21)+and2y1and0y1(x2x1x2x0P12+x2x1x0x1P22)

During implementation, it may be necessary to remove the pixel values ​​on the border to prevent the subscript from crossing the boundary. If ( x 0 , y 0 ) = ( x 0 ′ ∗ m / a , y 0 ′ ∗ n / b ) (x_0,y_0)=(x_0'*m/a ,y_0'*n/b) (x0,and0)=(x0m/a,and0n/b)integer, direct Take-up point gray level order ( x 0 ′ , y 0 ′ ) (x_0',y_0') (x0,and0) without bilinear interpolation.

The image reduction algorithm is similar to that of enlargement, as long as the new image size is a ∗ b a*b abSmaller than the original image size m ∗ n m*n mnAvailable now.

When rotating an image, in order to rotate the center of the image, the coordinate system needs to be translated to the center first. Remember the original center point ( x ′ , y ′ ) = ( 0 , 0 ) (x',y')=(0,0) (x,and)=(0,0), then the transformed center point ( x , y ) (x,y ) (x,y)One of the independent variables ( x y 1 ) = ( x ′ y ′ . 1 ) ( 1 0 0 0 − 1 0 − 0.5 w 0.5 h 1 ) \begin{pmatrix}x&y&1\end{pmatrix}=\begin{pmatrix}x'&y'& 1\end{pmatrix}\begin{pmatrix}1&0&0\\0&-1&0\\-0.5w&0.5h&1\end{pmatrix} (xy1)=(xand1) 100.5w010.5h001 . Then the rotation is divided into three steps: x ′ o ′ y x'o'y xoy变生 x o y xoy xoy, turn the point clockwise α \alpha αdegree, the last commander x o y xoy xoychange x ′ o ′ y ′ x'o' y'xoy is the same as:
( x y 1 ) = ( x ′ y ′ 1 ) = ( 1 0 0 0 − 1 0 − 0.5 would 0.5 hold 1 ) ( cos ⁡ α − sin ⁡ α 0 sin ⁡ α cos ⁡ α 0 0 0 1 ) ( 1 0 0 0 − 1 0 − 0.5 w new 0.5 h new 1 ) \begin{pmatrix}x& ;y&1\end{pmatrix}=\begin{pmatrix}x'&y'&1\end{pmatrix}=\begin{pmatrix}1&0&0\\0&-1& ;0\\-0.5w_{old}&0.5h_{old}&1\end{pmatrix}\begin{pmatrix}\cos\alpha&-\sin\alpha&0\\\sin\alpha&\ cos\alpha&0\\0&0&1\end{pmatrix}\begin{pmatrix}1&0&0\\0&-1&0\\-0.5w_{new}&0.5h_{new} &1\end{pmatrix}(xy1)=(xand1)= 100.5wold010.5hold001 cosasina0sinacosa0001 100.5wnew010.5hnew001
Because the size of the front and rear images are not necessarily equal. Therefore, interpolation processing is required, and the two interpolation algorithms mentioned above can be used. The new image width is w ′ = ∣ cos ⁡ α ∣ ∗ w + ∣ sin ⁡ α ∣ ∣ h w'=|\cos\alpha|*w+|\sin\alpha| *h In=cosαIn+sinαh. The height of the new image is h ′ = ∣ sin ⁡ α ∣ ∗ w + ∣ cos ⁡ α ∣ ∗ h h'=|\sin\alpha|*w+|\cos\alpha |*h h=sinαIn+cosαh

Part of the core code

I_0=imread("test.jpg");
I_1=rgb2gray(I_0);
I_2=im2bw(I_0,0.5);
%figure('P1','P2','P3','P4')
title("实验1-1")
subplot(1,3,1);imshow(I_0);title('原图')
subplot(1,3,2);imshow(I_1);title('灰度图')
subplot(1,3,3);imshow(I_2);title('二值化')
savefig("lab1_1.fig")

It has completed reading an RGB image, converting it into a grayscale image and a binary image, displaying the RGB image and grayscale image respectively in the same window, adding a text title, and saving the result to the disk in the form of a file.

I=imread("test.jpg");
[R,C,color]=size(I);
res=zeros(R,C,color);
for color=1:3
    for i=1:R
        for j=1:C
            newx=i+60;
            newy=j+60;
            if ((newx<=R) && (newy<=C))
                res(newx,newy,color)=I(i,j,color);
            end
        end
    end
end
imshow(uint8(res)) 

The translation operation of the image is completed, and the image is translated 60 pixels to the lower right. For each pixel, first translate, then determine whether the subscript is out of bounds, and finally fill in the pixel.

I=imread("test.jpg");
[R,C,ch]=size(I);
res=zeros(R,C,ch);
for color=1:3
    for i=1:R
        for j=1:C-1
            res(i,j,color)=I(i,C-j,color);
        end
    end
end
imshow(uint8(res))

Completed the vertical flipping of the image. No matrix operations are used.

I=imread("test.jpg");
[R,C,ch]=size(I);
res=zeros(R,C,ch);
for color=1:3
    for i=1:R-1
        for j=1:C
            res(i,j,color)=I(R-i,j,color);
        end
    end
end
imshow(uint8(res))

The horizontal flip operation of the image is completed. No matrix operations are used.

I=imread("test2.jpg");
[h,w,ch]=size(I);
height=1500;
width=1500;
newimg=zeros(height,width);
multh=height/h;
multw=width/w;
for i=1:height
    for j=1:width
        for c=1:3
            oldi=round(i/multh);
            oldj=round(j/multw);
            if (oldi>=1&&oldi<=h&&oldj>=1&&oldj<=w)
                newimg(i,j,c)=I(oldi,oldj,c);
            end
            
         end
    end
end
imshow(uint8(newimg))

The image enlargement/reduction operation is completed. Use nearest neighbor interpolation. First, divide the pixels in the new image by the magnification map to obtain its position in the original image, round the position value to find the nearest neighbor, and directly fill in the value of the nearest neighbor.

I=imread('test2.jpg');
[m,n,ch]=size(I);
width=100;
height=100;
newimg = uint8(zeros(width,height,3));
% 计算放大/缩小倍数
widthScale = m/width;
heightScale = n/height;  
for ch=1:3
    for x = 1:width - 1
        for y = 1:height - 1 
            xx = x * widthScale;%新图像位置对应原图像位置 
            yy = y * heightScale;%新图像位置对应原图像位置 
            if (xx/double(uint16(xx)) == 1.0) && (yy/double(uint16(yy)) == 1.0)
                newimg(x,y,ch) = I(int16(xx),int16(yy),ch);%若正好对应到整数点,则直接赋值,不插值
            else 
                a = double(uint16(xx));%找整数点
                b = double(uint16(yy));
                if a==0||b==0||a>=m||b>=n%下标不能越界
                    continue;
                end
                x11 = double(I(a,b,ch)); % x11 = I(a,b)
                x12 = double(I(a,b+1,ch)); % x12 = I(a,b+1)
                x21 = double(I(a+1,b,ch)); % x21 =I(a+1,b)
                x22 = double(I(a+1,b+1,ch)); % x22 = I(a+1,b+1)
                newimg(x,y,ch) = uint8( (b+1-yy) * ((xx-a)*x21 + (a+1-xx)*x11) + (yy-b) * ((xx-a)*x22 +(a+1-xx) * x12));
            end
        end
    end
end
imshow(newimg);

The image enlargement/reduction operation is completed. Use bilinear interpolation. First, divide the pixels in the new image by the magnification map to get its position in the original image. If it is an integer, fill it in directly. If it is not an integer, find the four adjacent points and interpolate according to the formula above. Fill in the value.

I=imread("test2.jpg");
angle=30;
[h,w,d]=size(I);
theta=angle/180*pi;
cos_val = cos(theta);
sin_val = sin(theta);
 
w2=round(abs(cos_val)*w+h*abs(sin_val));
h2=round(abs(cos_val)*h+w*abs(sin_val));
img_rotate  = uint8(zeros(h2,w2,3));    %像素是整数
 
 
for x=1:w2
    for y=1:h2
        x0 = uint32(x*cos_val + y*sin_val -0.5*w2*cos_val-0.5*h2*sin_val+0.5*w);%展开矩阵乘法
        y0 = uint32(y*cos_val - x*sin_val +0.5*w2*sin_val-0.5*h2*cos_val+0.5*h);%展开矩阵乘法
        
        x0=round(x0);         %最近邻
        y0=round(y0);         %最近邻
        if x0>=1 && y0>=1&&  x0<=w && y0<=h %检测下标不能越界
            img_rotate(y,x,:) = I(y0,x0,:);
        end
    end
end
imshow(img_rotate)

The image rotation operation is completed. For situations where the image size changes, nearest neighbor interpolation is used to handle scaling. In the same way, find the nearest neighbor by rounding.

angle=45;                   
I=imread('test2.jpg');     
imshow(I);                   
[h,w,d]=size(I);
theta=angle/180*pi;
M=[cos(theta) -sin(theta) 0;sin(theta) cos(theta) 0;0 0 1];
leftup=[1 1 1]*M;               %新图左上角
rightup=[1 w 1]*M;               %新图右上角
leftdown=[h 1 1]*M;               %新图左下角
rightdown=[h w 1]*M;               %新图右下角
cos_val = cos(theta);
sin_val = sin(theta);
 
w2=round(abs(cos_val)*w+h*abs(sin_val));%新图宽度
h2=round(abs(cos_val)*h+w*abs(sin_val));%新图高度
img_rotate=uint8(zeros(h2,w2,3));
dy=abs(min([leftup(1) rightup(1) leftdown(1) rightdown(1)]));            %取得y方向的负轴超出的偏移量
dx=abs(min([leftup(2) rightup(2) leftdown(2) rightdown(2)]));            %取得x方向的负轴超出的偏移量
 
for i=1-dy:h2-dy
    for j=1-dx:w2-dx
        pix=[i j 1]/M;                                %用变换后图像的点的坐标去寻找原图像点的坐标,
        yy=pix(1)-floor(pix(1));
        xx=pix(2)-floor(pix(2));
        if pix(1)>=1 && pix(2)>=1 && pix(1) <= h && pix(2) <= w
            x11=floor(pix(1));        %四个相邻的点
            y11=floor(pix(2));
            x12=floor(pix(1));
            y12=ceil(pix(2));
            x21=ceil(pix(1));
            y21=floor(pix(2));
            x22=ceil(pix(1));
            y22=ceil(pix(2));
 
            value_leftup=(1-xx)*(1-yy);              %计算临近四个点在双线性插值中的权重
            value_rightup=xx*(1-yy);
            value_leftdown=(1-xx)*yy;
            value_rightdown=xx*yy;
 
            img_rotate(i+dy,j+dx,:)=value_leftup*I(x11,y11,:)+value_rightup*I(x12,y12,:)+ value_leftdown*I(x21,y21,:)+value_rightdown*I(x22,y22,:);
        end
    end
end
 
imshow(uint8(img_rotate))

The image rotation operation is completed. For situations where the image size changes, scaling is handled using bilinear interpolation. In the same way, first find the coordinates of a certain point in the new image corresponding to the position of the original image (using matrix division), find its four adjacent points, and perform interpolation according to the formula above.

Experimental results

Figure 1 Experimental results of Question 1
For question 1, the reading, grayscale and binary operations of the image were completed. A .fig file that can be opened by matlab is also generated.
Figure 2 Translation experimental results
For translation, the image is correctly translated and the remaining parts are filled with black borders.
Figure 3 Left and right flipping experimental results
For left and right mirror flipping, the left and right mirror flipping of the image is correctly implemented.
Figure 4 Experimental results of flipping up and down
For the upper and lower mirror flipping, the upper and lower mirror flipping of the image is correctly implemented.
Figure 5 Zoom in on experimental results
Successfully enlarged the image using nearest neighbor interpolation. It can be seen that there are certain jagged and mosaic phenomena in the enlarged result.
Figure 6 Zoom out experimental results
Successfully reduced the image using nearest neighbor interpolation.
Figure 7 Zoom in on experimental results 2
Successfully enlarged the image using bilinear interpolation. It can be seen that the enlarged result is smoother than nearest neighbor interpolation, and there is no serious aliasing phenomenon.
Figure 8 Zoom out experiment results 2
Successfully reduced the image using bilinear interpolation. It can be seen that there is no distortion when reducing using bilinear interpolation.
Insert image description here
Figure 9 and Figure 10 Rotation experiment results 1 and 2
Use nearest neighbor interpolation and bilinear interpolation to rotate the image 45°, and fill the remaining areas with black.

Analysis: Advantages and Disadvantages of Two Interpolation Methods

Advantages of nearest neighbor interpolation method: simple implementation and fast running speed. Disadvantages: jaggedness may appear when enlarging the image, and the edges of some curves in the image are not smooth. There will also be a certain amount of distortion when zooming out after zooming in, making the image look blurry and like a mosaic.
Advantages of bilinear interpolation: It alleviates the shortcomings of nearest neighbor interpolation to a certain extent. After enlarging the image, some curves in the image look smoother. After repeatedly enlarging and reducing an image, the distortion is not serious and the processing effect is better. Disadvantages: The processing speed is slow. In the matlab2020B experimental environment, it often takes about 3 seconds to process an image. It is also difficult to process the outermost edges of the image.

4. Experimental experience

Guess you like

Origin blog.csdn.net/qq_46640863/article/details/125354403