The bottom of the pyramid is a high-resolution representation of the image to be processed, while the top is a low-resolution approximation. We liken a layer of image to a pyramid, the higher the level, the smaller the image, the lower the resolution
One, two pyramids
- Gauss Pyramid (GAUSSIANPYRAMID): Used for downward sampling, the main image pyramid
- Laplace Pyramid (LAPLACIANPYRAMID): Used to reconstruct the upper layer of the image from the lower pyramid images, in the digital image processing is also predictive residuals, the image can be maximized to restore, with the use of Gaussian pyramid.
Gaussian pyramid different (DoG), also known as the Laplace Pyramid, before the calculation method, first strengthen the definition
Remember, on top of that, we defined g0,g1,g2.
G0 under sampling for G1
G1 is sampled on the Upsample (G1), note that upsample (G1) is not equal to G0, the upper and lower samples are not reversible, because the next sample loses the picture information
Here, the formula for calculating the Laplace pyramid (DOG) is given: L (i) = g (i) –upsample (g (i+1))
Second, sampling
- sample up to Image: Pyrup function
- <1> Gaussian kernel convolution for image g_i
- <2> Remove all even rows and columns
- sample down the image: Pyrdown function
- <1> expands the image by twice times in each direction, with new rows and columns filled with 0
- <2> using the same kernel (multiplied by 4) and enlarged image convolution to get the approximate value of "new pixel"
Note : The downward and upward sampling here is for the size of the image (opposite to the pyramid), which is doubled the image size and downward is half the image size. And if we understand the pyramid direction as shown in, the pyramid is actually shrinking, which is exactly the opposite.
Note : Pryup and Prydown are not reciprocal, that is, Pryup is not a de-sampled inverse operation. In this case, the image first expands to twice times the original on each dimension, and the new rows (even rows) are filled with 0. The specified filter is then convolution (actually a filter that expands to twice times the original in each dimension) to estimate the approximate value of the "lost" pixel.
Prydown () is a function that loses information. To restore images of higher resolution, we have to get the information lost by the drop-sampling operation, which is related to the Laplace pyramid.
Three, the function introduction
<span style= "FONT-SIZE:18PX;" >c++: void Pyrup (Inputarray src, outputarraydst, const size& dstsize=size (), int bordertype=border_default) </span>
- The first parameter, the Inputarray type SRC, the input image, namely the source image, fills the Mat class the object to be able.
- The second parameter, the Outputarray type of DST, the output image, and the source picture have the same size and type.
- The third parameter, the const size& type of dstsize, is the size of the output image, with the default value of size (), which is calculated by size (src.cols*2,src.rows*2) By default, and the following conditions are always required:
- The fourth parameter, the int type of the Bordertype, comes again, the boundary pattern, generally we don't have to do it.
C + +: void Pyrdown (Inputarray src,outputarray DST, const size& dstsize=size (), int bordertype=border_default)
- The first parameter, the Inputarray type SRC, the input image, namely the source image, fills the Mat class the object to be able.
- The second parameter, the Outputarray type of DST, the output image, and the source picture have the same size and type.
- The third parameter, the const size& type of dstsize, is the size of the output image; A default value of size (), which is calculated by default, by size size ((src.cols+1)/2, (src.rows+1)/2), And always need to meet the following conditions:
The Pyrdown function performs a downward sampling of the Gaussian pyramid construction. First, it makes a convolution operation of the source image with the following kernel:
It then makes a downward sampling by interpolating even rows and columns of the image.
The Pyrup function performs a Gaussian pyramid sampling operation, but it can also be used for the Laplace pyramid.
First, it makes an upward sampling of the source image by inserting a row and column that can be zero, and then convolution the result with the kernel of Pyrdown () multiplied by 4.
<span style= "FONT-SIZE:18PX;" >c++: void Resize (Inputarray src,outputarray DST, Size dsize, double fx=0, double fy=0, int interpolation=inter_linear ) </span>
- The first parameter, the Inputarray type SRC, the input image, namely the source image, fills the Mat class the object to be able.
- The second parameter, the Outputarray type of DST, outputs the image, when it is nonzero, has a dsize (third parameter) dimension, or is computed by src.size ().
- The third argument, the size of type dsize, of the output image, and if it equals zero, is calculated by the following formula:
Among them, Dsize,fx,fy can not be 0.
- The fourth argument, the double type of FX, has a scaling factor along the horizontal axis, has a default value of 0, and when it equals 0 o'clock, it is calculated by the following formula:
- The fifth argument, the double type of FY, has a scaling factor along the vertical axis, has a default value of 0, and when it equals 0 o'clock, it is calculated by the following formula:
- The sixth parameter, interpolation of type int, is used to specify the interpolation method, which defaults to Inter_linear (linear interpolation).
The optional interpolation methods are as follows:
- inter _nearest-Nearest neighbor interpolation
- Inter_linear-linear interpolation (default)
- inter_area-region interpolation (resampling interpolation with pixel region relationship)
- inter_cubic– three spline interpolation (double three interpolation within a 4x4 pixel neighborhood)
-
To narrow the image, it is generally best to interpolate with Cv_inter_area,
To enlarge the image, it is generally best to use cv_inter_cubic (inefficient, slow, deprecated) or cv_inter_linear (high efficiency, faster, recommended).
Iv. introduction of interpolation values
1. Nearest Neighbor Element method
This is the simplest kind of interpolation method, do not need to calculate, in the neighborhood pixels to be asked for pixels, the nearest pixel in the vicinity of the pixels to be asked for pixels. Set I+u, J+v (i, J is a positive integer, U, v is greater than 0 decimals less than 1, the same below) for the pixel coordinates, the pixel gray value f (i+u, j+v) as shown:
if (I+u, j+v) falls in area A, that is u<0.5, v<0.5, then the upper-left pixel of the gray value of the pixels to be asked for pixels, in the same vein, the B area is assigned to the upper right corner of the pixel gray value, falling in the C zone gives the lower-left pixel gray value, falling in the D area is given to the
The nearest neighbor meta-method is computationally small, but may cause discontinuities in the gray scale of the interpolated image, and may appear jagged in the gray area.
2. bilinear interpolation method
bilinear interpolation is a linear interpolation in two directions using the grayscale of four neighboring pixels of the pixel to be obtained, as shown in:
For (I, j+v), the gray change of F (i, j) to F (i, j+1) is linear, then there are:
F (i, j+v) = [F (i, j+1)-F (i, j)] * v + f (i, J)
In the same vein (i+1, j+v) there are:
F (i+1, j+v) = [F (i+1, j+1)-F (I+1, J)] * v + f (i+1, J)
The gray-level change from F (i, j+v) to F (i+1, j+v) is also linear, thus the calculation of the pixel gray scale is deduced as follows:
F (i+u, j+v) = (1-u) * (1-v) * F (i, J) + (1-u) * v * f (i, j+1) + U * (1-v) * F (i+1, J) + U * v * f (i+1, j+1)
The calculation of bilinear interpolation method is more complex than the nearest neighbor point method, and the computational amount is large, but there is no shortcoming of gray scale discontinuity, the result is satisfactory. It has low-pass filtering property, so that the high-frequency component is damaged, the image contour may be a little blurred.
3, three times interpolation method
The method uses the three-time polynomial s (x) to approximate the theoretically best interpolation function sin (x) x, whose mathematical expression is:
The grayscale value of the pixel (x, y) to be determined is interpolated by a weighted interpolation of 16 gray values around it, such as:
The grayscale calculation for the pixel to be obtained is as follows:
f (x, y) = f (i+u, j+v) = ABC
which
The three-time curve interpolation method is computationally large, but the image after interpolation is the best.
V. Comprehensive examples
<span style= "FONT-SIZE:18PX;" > #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/ Imgproc.hpp> using namespace std; using namespace CV; Mat g_srcimage, G_dstimage, g_tmpimage; int main () {showhelptext (); G_srcimage = Imread ("lena.jpg"); if (!g_srcimage.data) {printf ("Oh,no, read srcimage Error ~! \ n "); return false; } namedwindow (Window_name, cv_window_autosize); Imshow (Window_name, g_srcimage); G_tmpimage = G_srcimage; G_dstimage = G_tmpimage; int key = 0; while (1) {Key=waitkey (9); Depending on the value of the key variable, do a different operation switch (key) {case 27:return 0; Break Case ' Q ': return 0; Break Case ' a '://press A to invoke the Pyrup function Pyrup (g_tmpimage, G_dstimage, Size (g_tmpimage.cols*2, g_tmpimage.rows*2)); printf ("> Detected key" A "was pressed to start a picture-based" Pyrup "function to enlarge: Picture size x2 \n "); Break Case ' W '://Key W pressed, call resize function resize (g_tmpimage,g_dstimage,size (g_tmpimage.cols*2, g_tmpimage.rows*2)); printf ("> Detected key" W "was pressed to begin the image enlargement based on" resize "function: Picture size x2 \ n"); Break Case ' 1 '://Key 1 pressed, call resize function resize (g_tmpimage,g_dstimage,size (g_tmpimage.cols*2, g_tmpimage.rows*2)); printf ("> Detected keys" 1 "was pressed to start a picture-based" resize "function to enlarge: Picture size x2 \ n"); Break Case ' 3 '://Key 3 pressed, call Pyrup function Pyrup (g_tmpimage, G_dstimage, Size (g_tmpimage.cols*2, g_tmpimage.rows*2)); printf ("> Detected Keys" 3 "was pressed to start a picture-based" Pyrup "function to enlarge: Picture size x2 \ n"); Break ====================== "Picture reduction phase critical value processing" ======================= case ' d '://Key D pressed, call Pyrdown function Pyrdown ( G_tmpimage, G_dstimage, Size (G_TMPIMAGE.COLS/2, G_TMPIMAGE.ROWS/2)); printf ("> Detected key" D "is pressed to start the image reduction based on" pyrdown "function: Image size/2\n");Break Case ' s '://Key s pressed, call resize function resize (g_tmpimage,g_dstimage,size (G_TMPIMAGE.COLS/2, G_TMPIMAGE.ROWS/2)); printf ("> Detected keys" S "is pressed to begin the image reduction based on" resize "function: Image size/2\n"); Break Case ' 2 '://Key 2 pressed, call resize function resize (g_tmpimage,g_dstimage,size (G_TMPIMAGE.COLS/2, G_TMPIMAGE.ROWS/2), (0,0), ( 0,0), 2); printf ("> Detected Keys" 2 "was pressed to start the image reduction based on" resize "function: Image size/2\n"); Break Case ' 4 '://Key 4 pressed, call Pyrdown function Pyrdown (g_tmpimage, G_dstimage, Size (G_TMPIMAGE.COLS/2, G_TMPIMAGE.ROWS/2)) ; printf ("> Detected Keys" 4 "was pressed to start the image reduction based on" pyrdown "function: Image size/2\n"); Break }//After operation, show the changed figure Imshow (Window_name, g_dstimage); The G_dstimage is assigned to G_tmpimage to facilitate the next cycle g_tmpimage = G_dstimage; } return 0; } </span>
VI, MATLAB
<span style= "FONT-SIZE:18PX;" >i = Imread (' d:\lena.jpg '); A = Imresize (I, 1.5, ' nearest '); B = Imresize (I, 1.5, ' bilinear '); C = Imresize (I, 1.5, ' bicubic '); Subplot (2,2,1), Imshow (I), title (' original '); Subplot (2,2,2), Imshow (A), title (' nearest '); Subplot (2,2,3), Imshow (B), title (' bilinear '); Subplot (2,2,4), Imshow (C), title (' Bicubic '); </span>
Image recognition algorithm Communication QQ Group: 145076161, welcome image recognition and image algorithm, mutual learning and communication
OpenCV image recognition from zero to proficient-----image pyramid, up and down sampling, resize interpolation