Texture mapping __OPENCV

Source: Internet
Author: User
(Image mosaic of the project to use texture mapping, with OpenCV cvwarpperspective too slow, consider using look-up table mapping to improve)Baidu to the following content, spare: Turn from: http://www.cnblogs.com/wangguchangqing/p/4039095.html 1. Basic concepts of geometric transformations 1.1 Coordinate mapping relationship

The geometric transformation of the image changes the spatial position of the pixel, a mapping relationship between pixel of original image and pixel of transformed image is established, through which the following two kinds of calculation can be realized: any pixel of the original image computes the coordinate position of any pixel in the original image after the transformation of the pixel in the coordinate position of the transformed image.

For the first calculation, as long as any pixel coordinates on the original image are given, the coordinate position of the pixel in the transformed image can be obtained through the corresponding mapping relation. The process of mapping this input image coordinate to the output is called "forward mapping." In turn, we know the pixel coordinates on the image after any transformation, and compute its pixel coordinates in the original image, and the process of mapping the output image to the input is called "backward mapping". However, there are some deficiencies in using forward mapping to handle geometric transformations, which usually result in two problems: incomplete mappings, incomplete mapping overlapping mappings
The total number of pixels in the input image is less than the output image, so some pixels in the output image cannot find a mapping in the original image.

The above figure has only (0,0), (0,2), (2,0), (2,2) four coordinates found the corresponding pixel in the original image according to the mapping relationship, and the remaining 12 coordinates have no valid values. Mappings overlap
Depending on the mapping relationship, multiple pixels of the input image are mapped to the same pixel on the output image.

The four pixels (0,0), (0,1), (1,0), (1,1) in the upper-left corner of the upper figure are mapped to the output image (0,0), so (0,0) what is the pixel value.

To solve the above two problems, you can use "backward mapping" to use the coordinates of the output image to calculate the coordinate position in the original image. Thus, each pixel of the output image can find the unique corresponding pixel in the original image through the mapping relationship, without the mapping incomplete and mapping overlapping. Therefore, it is common to use backward mapping to deal with geometric transformations of images. It can also be seen from above that the problem with forward mapping is mainly due to the change in the total number of pixels in the image, that is, the size of the image changed. Forward mapping is also effective in transformations where the image size does not change. 1.2. Interpolation algorithm

For a digital image, the coordinate of a pixel is a discrete nonnegative integer, but it is possible to produce floating-point coordinate values during the transformation. For example, the original image coordinate (9,9) becomes (4.5,4.5) when it is reduced by one time, which is obviously an invalid coordinate. Interpolation algorithms are used to handle these floating-point coordinates. The common interpolation algorithms include nearest interpolation method, bilinear interpolation method, two cubic interpolation method, three cubic interpolation method and so on. This paper mainly introduces the nearest interpolation and bilinear interpolation, some other higher order interpolation algorithms, and then do research later. Nearest neighbor interpolation
Also known as the 0-order interpolation method, the simplest interpolation algorithm, of course, the effect is the worst. Its idea is quite simple, that is rounding, the pixel value of the floating-point coordinates equals the pixel value of the input image closest to the point.

The above code can obtain the nearest neighbor interpolation coordinate (x,y) (u,v).
The nearest neighbor interpolation has almost no extra operation, which is very fast. But this method of adjacent value is very rough, it will cause the image mosaic, sawtooth and other phenomena. bilinear interpolation
Its interpolation effect is much better than the nearest neighbor interpolation, and the corresponding calculation speed is much slower. The main idea of bilinear interpolation is to calculate the pixel approximation of floating-point coordinates. So how do you calculate the approximate value of a floating-point coordinate? A floating-point coordinate is bound to be surrounded by four integer coordinates, and the pixel value of the floating-point coordinates can be obtained by mixing the pixels of the four integer coordinates in a certain proportion. The blending ratio is the distance from the floating-point coordinates.
Suppose that the pixel value p is required for coordinates (2.4,3), which is between (2,3) and (3,3), as shown below
The U and V are the proportions of the nearest two integer coordinates pixel in the floating-point coordinates pixel, respectively, from the floating-point coordinates
P (2.4,3) = U * P (2,3) + V * p (3,3), the mixing ratio is based on distance, then U = 0.4,v = 0.6.
Above is the interpolation of only one line, called linear interpolation. bilinear interpolation is a linear interpolation operation on the x axis and the y axis respectively.
The following three-time linear interpolation is used to double trust interpolation operation

(2.4,3) pixel value F1 = m * T1 + (1–m) * T2
(2.4,4) pixel value F2 = m * T3 + (1–m) * T4
(2.4,3.5) pixel value F = n * F1 + (1–n) * F2
This allows you to get the pixel values of the floating-point coordinates (2.4,3.5).
To find the floating point coordinate pixel F, the 4 pixel values around the floating-point coordinates are T1,T2,T3,T4, and the difference between the horizontal axis of the floating point coordinate distance from its upper left corner is M, and the difference between the ordinate is N.
F1 = m * T1 + (1–m) * T2
F2 = m * T3 + (1–m) *t4
F = n * F1 + (1–n) * F2
The above is the basic formula of bilinear interpolation, it can be seen that the calculation of each pixel pixel value requires 6 times floating-point operation. Moreover, because the floating-point coordinates have 4 coordinates approximate, if this four coordinates the pixel value difference is big, after interpolation, will make the image in the color demarcation more obvious place becomes more blurred.

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.