Opencv-Image Scaling

Source: Internet
Author: User

Image size conversion

Void cvresize (const cvarr * SRC, cvarr * DST, int interpolation = cv_inter_linear); SRC input image. dst output image. interpolation method:

  • Cv_inter_nn-nearest neighbor interpolation,
  • Cv_inter_linear-bilinear interpolation (used by default)
  • Cv_inter_area-use the pixel relationship for re-sampling. When the image is reduced, this method can avoid ripple. When the image is enlarged, it is similar to the cv_inter_nn method ..
  • Cv_inter_cubic-cube interpolation.

The cvresize function changes the size of the image SRC to the same size as that of DST. If you set the ROI, the function supports the ROI as usual.


Basic Principles of nearest neighbor interpolation and bilinear interpolation

Image Scaling is easy to understand, that is, image amplification and reduction. In traditional painting tools, there is a kind of painting tool called "scale-up". Painters often use it to enlarge pictures. Of course, on the computer, we no longer need to use a scale to zoom in or out the image. You just need to submit the work to the program. Next we will talk about how computers can zoom in and out images. In this article, all images refer to bitmap, that is, a pixel matrix to describe images, for another image: using functions to describe the vector image is not discussed in this article.

The simpler the model, the more suitable it is for example. Let's take a simple image: A 3x3 256 grayscale image, that is, a 3 pixel High, the width is also a three-pixel image. The value of each pixel can be 0-255, indicating the brightness of the pixel. 255 indicates the brightest, that is, white, and 0 indicates the darker, that is, black. The Pixel matrix of the dummy image is shown in (the source image is called the source image ):

234 38 22

67 44 12

89 65 63

In this matrix, the element coordinates (x, y) are determined in this way. x starts from left to right, starts from 0, and y starts from top to bottom, this is the most common coordinate system in image processing:

----------------------> X

|

|

|

|

|

∨ Y

If you want to enlarge this image to a 4x4 image, what should you do? The first step is to first draw the 4x4 matrix. After the matrix is ready, it is shown as follows. Of course, each pixel of the matrix is unknown, wait for us to fill (the target image, destination, of the graph to be filled ):

? ? ? ?

? ? ? ?

? ? ? ?

? ? ? ?



Then I want to fill in the value in this empty matrix. Where can I fill in the value? It is from the source graph. Well, first fill in the pixel at the top left corner of the target graph, and the coordinate is (). Then, the coordinates corresponding to the source graph can be obtained by the following formula:

Srcx = dstx * (srcwidth/dstwidth), srcy = dsty * (srcheight/dstheight)

Well, apply the formula to find the coordinates of the corresponding source image (0 * (3/4), 0 * (3/4) => (0*0.75, 0*0.75) => (0, 0)

, Find the corresponding coordinates of the source image, you can fill in the 234 pixel value at the coordinate (0, 0) of the source image to the position of the target image (0, 0.

Next, search for the coordinates in the source image corresponding to the pixels whose coordinates are (1, 0) in the target image, and apply the formula:

(1*0.75, 0*0.75) => (0.75, 0)

The result shows that there are decimal places in the obtained coordinates. What can I do? Images in a computer are digital images. pixels are the smallest unit. The coordinates of pixels are integers, and there are never decimal coordinates. At this time, a strategy is to use the rounding method (you can also directly remove decimal places) to convert non-integer coordinates into integers, then, the coordinates () are obtained by rounding the method. The complete calculation process is as follows:

(1*0.75, 0*0.75) => (0.75, 0) =>)

Then you can fill in another pixel in the target matrix. Similarly, you can fill in 38 pixels at the coordinate () in the source image with the coordinates in the target image.



After each pixel is filled in order, an enlarged image is generated. The Pixel matrix is shown as follows:

234 38 22 22

67 44 12 12

89 65 63 63

89 65 63 63

This method of enlarging an image is called the nearest interpolation algorithm. This is the most basic and simple image scaling algorithm, and the effect is also the worst. The enlarged image has a very serious mosaic, the reduced image has serious distortion. The root cause of poor effects is that its simple nearest interpolation method introduces serious image distortion, for example, when the coordinate of the source graph obtained by the coordinate of the target graph is a floating point number, the rounding method is used to directly use the pixel value closest to the floating point number, this method is not scientific. When we get the coordinate value of 0.75, we should not simply set it to 1. Since it is 0.75, It is 0.25 less than 1, it is 0.75 larger than 0, so the target pixel value should be calculated according to certain rules based on the four actual points around the virtual points in the source graph, in this way, the scaling effect can be better. The two-line inner interpolation algorithm is a good image scaling algorithm. It makes full use of the four real pixel values around the virtual points in the source graph to jointly determine a pixel value in the target graph, therefore, the scaling effect is much better than the simple nearest interpolation.

The bilinear interpolation algorithm is described as follows:

For a target pixel, set the floating point coordinates obtained through reverse transformation to (I + u, J + V) (where I and j are integral parts of the floating point coordinates, U and V are the decimal part of the floating point coordinate, which is the floating point number in the range of [0, 1). Then the pixel value f (I + u, J + V) the coordinates of the original image are (I, j), (I + 1, J), (I, j + 1), (I + 1, J + 1) the value of the surrounding four pixels is determined, namely:

F (I + u, J + V) = (1-u) (1-V) f (I, j) + (1-u) VF (I, j + 1) + U (1-V) f (I + 1, J) + UVF (I + 1, J + 1) Formula 1

F (I, j) indicates the pixel value at the source image (I, j), and so on.

For example, if the pixel coordinate of the target image is (0.75), the coordinates of the source image are (0.75 ), this is actually a conceptual virtual pixel. Actually, there is no such pixel in the source image, so the value of the target image's pixel () cannot be determined by this virtual pixel, it can only be determined by the four pixels of the source image: (0, 0) (0.75) (0.75) (), and because (,) is closer, then (0.75) plays a greater role, which is determined by the coefficient UV = x 0 in Formula 1. 75 can be reflected, and (0.75, 0.75) is the farthest from (), SO () plays a smaller role, the coefficient in the formula is (1-u) (1-V) = 0.25x0. 25 also reflected this feature.

Principle reference link: http://blog.csdn.net/andrew659/article/details/4818988

Opencv code: scale is the scale-down ratio

# Include "stdafx. H "# include <cv. h> # include <cxcore. h> # include 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.