The scaling of digital images is a very interesting problem, but also a seemingly simple, but some complex problems. Many friends after having a certain basic knowledge of computer graphics programming, they can design some simple bitmap scaling algorithm. In the field of computer graphics and digital image processing, the problem of digital image scaling has been studied in detail, and a mature algorithm has been developed. Some friends have not studied computer graphics and digital image processing, so with their own ideas to design a bitmap scaling algorithm has many flaws. In this article, I will work with you to study this problem, and learn the algorithms that the predecessors have summed up.
The concept of the image is easy to understand, and you open your eyes and all you see is an image. A picture, a picture, is the real life of the means and carrier of recording images. In science, we need to set up mathematical model for our research object, so it is necessary to set up the mathematical model of the image. The mathematical model of an image can be simply defined as follows:
+-----------------------------------------------------+
Image = f (x, y);
where x, y is the real number on [0, 1]
For grayscale images also real numbers on [0, 1]
For color images The image is composed of R, G, B three components
+-----------------------------------------------------+
Because both the definition field and the domain value are on [0, 1], they are called continuous image models.
The continuous image model can accurately and completely depict the image to be described, however, in the real world, most of the images cannot be described by this mathematical model, because the real-world images are not possible through the function of analytic methods to describe. More often than not, we can only use the camera to save part of the real-world image on film, or use a brush to draw images on paper. It is precisely because of this feature of the image, we have established a continuous image model, for the study of images, and there is no use, and the traditional method of mathematical research can not be used in the image.
In order to study and process images more effectively, we use the theory of discrete mathematics to build a digital image model for realistic images, and use this powerful tool to help us research and process images. The digital image model is defined as follows:
+-----------------------------------------------------+
Image = Array (i, j);
Normally I is an integer greater than or equal to 0, less than W
And j is an integer greater than or equal to 0, less than H
Array can be understood as a matrix
The value of Image is greater than or equal to 0, and is less than or equal to 255 integers
For grayscale images, the image represents the luminance value of a point
For color images The image is composed of R, G, B three components
+-----------------------------------------------------+
where W, H is often referred to as the width and height of a digital image. Here, the width and height of the digital image, and a digital image on the display of the actual width and height, has a certain correspondence, this should be easy to understand. The problem with scaling the digital image we're going to discuss is to change the width and height of an image, and make the data in the matrix (I, j) change Accordingly, so that the image scales proportionally.
In Digital image processing, the scaling of digital image is also called resampling filter, which is abstract but deeply reveals the essence of digital image scaling. When you understand the concept of resampling filtering, you will find that bitmap scaling is so straightforward and surprising that resampling filtering is so magical and profound.
Now for the convenience of the narrative, we refer to the real life of the image is called the real image, the picture is called the physical image, the continuous image model as a continuous image, the digital image model into a digital image or bitmap. So, let's take a look at the process of producing a digital image.
First, you need to use a camera to take pictures of real-world images, producing photos, or physical images. In the physical image, only part of the information in the real image is recorded. The photo is then placed on the scanner for scanning, where the image is transformed from a physical image to a digital image, resulting in a commonly said bitmap in the computer. In the process of transforming images from physical images to digital images, the most critical place is sampling and quantification, which may be familiar to both of us, but we still need to understand and think deeply.
If said, in the process of scanning, we will scan the resolution of a large, that is, the sampling rate setting is large, the scanned digital image resolution is also larger, that is, the width and height of the image is larger, conversely, it is smaller. Let's take a deep understanding of the concept of resampling, the so-called resampling filter, which refers to the reconstruction of a physical image in some way based on a digital image, and re-sampling the reconstructed physical image at the desired sampling rate.
Let's imagine a process where we have a wide width of bitmap A that is high (W1, H1), and we want to scale it to a bitmap B with a width height of (w2, H2). We can do this by doing the scaling, that is, spending about 1000 bucks, buying a Canon color laser printer, and printing bitmap A with a printer Come out. Then buy an HP scanner to scan the image to the computer (W2, H2) at the scanning resolution. This allows us to successfully zoom in on the bitmap. Of course, the cost of such an approach is too high to spend more than 1000 of the ocean, and time-consuming and laborious, is a theoretically feasible but not practical approach. However, this approach is sufficient to vividly and clearly illustrate the nature and method of digital image scaling.
Let's take a look at the depth of the resampling filter, which is to reconstruct the physical image and resample it at the resolution you want. In the computer world, digital images are easy to describe, and a two-dimensional array can be used to describe a digital image simply. However, how can we reconstruct the physical image of an existing digital image? Think about it, just hurry up and get the 2000 bucks to buy a printer and a scanner. Not Big brother, I really want to buy a printer, quickly reveal the answer.
Okay, no kidding, now answer. In the computer, reconstructing the physical image is actually a computational model, the physical image in the computer world is not really rebuilt, after all, the computer is a discrete system, and the physical image is a continuous thing, the computer can not be complete and accurate description. However, we can find a variety of computational models to describe the physical images we need to reconstruct. It is important to note that we are looking at a computational model and then we are going to resample it based on this computational model.
From real image to physical image to digital Image transformation process, is an irreversible transformation process, in each transformation process, will lose a lot of information, is irreversible. If we want to reconstruct the physical image from digital image, it is a kind of approximation to the physical image based on the existing digital image data. For the existing digital image, we have a number of computational models to reconstruct the physical image, and this reconstructed physical image is generally described by the previous continuous image model. First, the simplest computational model, the nearest neighbor algorithm, is introduced.
Please consider some of these questions:
1. I have a 24bit color BMP image, for the image of a point (I, J) we can know its RGB color value, but if I want to know (101.3, 98.6) The color value of the point, what should we do?
2. I want to be a BMP * * 24bit color image, scaled to 1005 * 754 * 24bit color bmp picture, how to resample it?
The first problem, the first step in digital image scaling, is to reconstruct the physical image. If we use the nearest neighbor algorithm, for a non-integer coordinate point, we select the color value of the nearest integer coordinate point from this point, as its color value. That is, we simply take the color value (101.3, 98.6) as the color value of the point (101, 99). To solve this problem, we actually put a continuous image in our brain, and we can calculate the color value for any pixel on this continuous image.
The second question is how to resample on a reconstructed physical image. The so-called sampling, is to obtain each need to sample the color value, that is, to use a double cycle, processing 1005 * 754 points, and calculate the color value of each point. The simple code is as follows:
+-------------------------------------------------------------------------+
DWORD color;
int i;
Int J;
for (j=0; j<754; j + +)
{
for (i=0; i<1005; i++)
{
color = Resample (i, j); Calculate the color value of a resampling point
Putpixel (Destbmp, I, j, color); Draw a sample point onto the destination bitmap
}
}
+-------------------------------------------------------------------------+
The method for calculating the color value of each resampling point is determined by the original digital image, the calculation model you selected, and the physical image reconstructed by the computational model. For the above example, we can calculate the width and height scaling ratio, and then establish the original image and the target image of the pixel in the corresponding relationship and transformation formula, according to the transformation formula, the target image (I, j) point in the original image of the corresponding point coordinates (x, y), according to the reconstructed physical image of the calculation model, Calculates the color value C of the point in the original image (X, y), and then draws C as a result onto (I, j) in the destination image. When you're done with each pixel in the destination image, you're done zooming in on the digital image.
For our example, we can write the following code:
+-------------------------------------------------------------------------+
int w1 = 320;
int h1 = 240;
int w2 = 1005;
int h2 = 754;
float xratio = (float) w1/w2;
float yratio = (float) h1/h2;
int i;
Int J;
float x;
Float y;
for (j=0; j<754; j + +)
{
for (i=0; i<1005; i++)
{
x = i * xratio;
y = J * yratio;
color = GetPixel (srcbmp, (int) (x + 0.5), (int) (y + 0.5));
Putpixel (Destbmp, I, j, color);
}
}
+-------------------------------------------------------------------------+
Among them | x = i * xratio; y = J * Yratio; | The two lines of code reflect the location of the sample, that is, where the reconstructed image is sampled, and | color = GetPixel (srcbmp, (int) (x + 0.5), (int) (y + 0.5)); | This code shows the result of the sample, that is, the number of the sampled color values in the reconstructed image (x, y).
Please seriously understand the profound significance of the so-called reconstruction of physical images, actually can not really reconstruct, that is, this is just a computational model, a method. The resampling filter is a resampling based on the computational model. The subtlety and depth of digital image scaling is briefly and profoundly summed up by resampling filtering.
Through the above introduction, we should understand the principle of digital image scaling and implementation methods, and can use the nearest neighbor method of the calculation model, write a general bitmap scaling program. As you can see, the algorithm framework for bitmap scaling is actually very simple, and the key is to understand the principles and methods. Bitmap scaling of the nearest neighbor algorithm, in my graphics library has been implemented, you can find in my graphics library code for reference.
The next step is to introduce more computational models for reconstructing physical images. In addition to the nearest neighbor algorithm, there are bilinear interpolation algorithms and surface interpolation algorithms. bilinear interpolation algorithm is simple and easy to calculate, so it has been widely used in engineering. And the surface interpolation can get better image effect, but its interpolation principle and calculation method are more complicated, this article is no longer introduced.
The so-called linear interpolation, we should not be unfamiliar, give a simple example to illustrate the problem. F (a) = 100, f () = 65, the value of f (99.3) is calculated by the linear interpolation method. Here's how:
F (+)-F (+) f (+)-F (99.3)
----------------- = --------------------
100-99 100-99.3
The value of f (99.3) can be calculated based on the above equation. This is called linearity because three points are located on a straight line. Extend the above linear interpolation to the two-dimensional case, which is what we usually call bilinear interpolation.
Also use an example to illustrate the problem:
F (123, 221) = 231, F (124, 221) = 35
F (123, 222) = 213, F (124, 222) = 86
The color value of f (123.8, 221.2).
Solution, the use of one-dimensional linear interpolation, according to F (123, 221) and F (123, 222) to obtain F (123, 221.2)
Using one-dimensional linear interpolation, F (124, 221.2) is calculated according to F (124, 221) and F (124, 222)
Using one-dimensional linear interpolation, F (123.8, 221.2) is calculated according to F (123, 221.2) and F (124, 221.2)
The problem was solved.
In bilinear interpolation, we can prove that the first calculation along the x-coordinate and first the y-coordinate calculation, the final result is the same. Of course, in the practical application, we also need to calculate a general formula, here is just an example to illustrate the problem.
Finally, let's sum it up.
The fundamentals of Digital Image scaling:
1. Rebuilding physical images based on existing digital images
2. Re-sampling the reconstructed physical image at the desired resolution
The basic method of digital image scaling:
1. Build up a computational model of reconstructed physical images
2. Establish the coordinate transformation relationship between the target image and the original image
3. Calculate the midpoint of the destination image (I, j) coordinates of the corresponding point in the original image (X, y)
4. Calculates the color value C at the midpoint (x, y) of the original image based on the calculation model
5. Set the color value of the midpoint (I, j) of the destination image to the color value C
6. Complete the zoom with each pixel on the destination image
Speaking so much, should be the digital image scaling the problem is more clearly introduced. It is critical to understand the nature of digital images, the concept of sampling and quantification, and the reconstruction of physical images and resampling based on digital images, that is, resampling filtering. Finally, we will tell you two concepts, that is, the reduction of digital image, known as the de-sampling filter, and amplification is called oversampling filter (this does not know if this is called, even if I invented the name of it). I hope that after reading this article, we can gain some understanding of the principles and methods of digital image scaling, and implement your own bitmap scaling program.
Original: http://bbs.bccn.net/thread-198397-1-2.html
Talking about the scaling algorithm of digital Image [go]