“This is the 13th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

preface

Hello! Friend!!!

Thank you very much for reading haihong’s article, if there are any mistakes in the article, please point out ~

 

Self-introduction ଘ(੭, ᵕ)੭

Nickname: Haihong

Tag: programmer monkey | C++ contestant | student

Introduction: because of C language to get acquainted with programming, then transferred to the computer major, had the honor to get some national awards, provincial awards… Has been confirmed. Currently learning C++/Linux/Python

Learning experience: solid foundation + more notes + more code + more thinking + learn English well!

Contraction of image

Definition: the shrinkage of the image is commonly referred to as amplification and abbreviation. In digital image processing, it refers to the increase and decrease of pixels. For example, an image of 22 with 4 pixels, magnified twice, would have 44=16 pixels.

So how do you shrink the image?

Here is an example of magnification: the image magnified by two times means that the width and height of the image are enlarged by two times as the original, that is, the total number of pixels is four times as original. So how do you add pixels to an image without changing the content? This is also simple, for example, a red circle originally represented by 4 pixels (4 identical pixels), and a double circle is represented by 16 pixels (also identical). In general, it means that more pixels are used to represent the same shape. We used to need 4, so now we need 16.

Of course, a fragment of a random image is represented by multiple different pixels, so how can you increase the number of pixels without changing the shape? This is going to be interpolated. For example, if you want to increase the slope (in the middle), you must fill in the middle of the slope with the same height as the two sides, so that the original slope is also continuous, maintaining the original continuity. So the purpose of interpolation is to keep the original image consistent by filling in pixels, similar to the continuity after changes in high numbers. Below is an illustration of the interpolation method: Pseudocode of the algorithm:

For example, the size of the original picture is 300400, enlarge it twice, become 600800; The point (1,2) on the original image is the point (2,4) on the enlarged image. This should make a little bit of sense.

When programming, we first construct an empty matrix to enlarge the image, and then fill in the values. This requires that we should be able to know the coordinates of a point in the enlarged image, calculate the coordinates of the original picture, and then assign the coordinates of the original picture to the new point.

Using the idea of proportion, for example, on the X-axis, if the enlarged image 2 takes up 2/600 of the total length of 600, then the x-coordinate of the original image also takes up 2/600 of the total length of 600, which is calculated as 1. Suppose (x1,y1) is the value of the enlarged image, (x0, y0) is the value of the original image, m,n are the horizontal and vertical lengths of the original image, rm, Rn are the horizontal and vertical lengths of the enlarged image.

There are the following relationships:

x1/rm=x0/m y1/rn=y0/n

It can be deduced that:

x0=x1m/rm y0=y1n/rn

Finally, we can use bilinear interpolation. Here the code is (in fact, it is the first 4 points around, using different value functions, then calculate) :

rt(i,j,:)=t(floor(x),floor(y),:)*(1-u)*(1-v)+t(floor(x),ceil(y),:)*(1-u)*v+t(ceil(x),ceil(y),:)*(u)*(1-v)+t(ceil(x),ceil (y),:)*u*v;Copy the code

Full code:

t=imread('a1.jpg'); [m,n,dep]=size(t); % Custom length and width rm=1400; rn=822; % Construct a new matrix to store the shrunk image rt= Zeros (rm, Rn,dep); For I =1:rm for j=1: RN % conversion x= I *m/rm; y=j*n/rn; % find the offset u=x-floor(x); v=y-floor(y); If x<1 x=1; end if y<1 y=1; End % bilinear interpolation rt(i,j,:)=t(floor(x),floor(y),:)*(1-u)*(1-v)+t(floor(x),ceil(y),:)*(1-u)*v+t(ceil(x),ceil(y),:)*(u)*(1-v)+t(ceil(x),ceil (y),:)*u*v; end end imshow(t); The title (' artwork '); figure; imshow(uint8(rt)); Title (' doubled image ');Copy the code