Recently, a MATLAB program into C + +, on the way to meet INTERP2 this guy, I was Zocha right check, found that no one online summary of this thing, so I came to explore, or Shia, hehe.
1. About INTERP2
Vq = Interp2 (X,y,v,xq,yq, ' linear ', 0), X and Y represent the coordinates of the sampling point, can be vectors or matrices, such as we want to draw a mesh, the grid point coordinates can be understood as the coordinates of the sampling point. XQ and Yq represent the coordinates of the query point, which can also be a vector or a matrix, where we need to interpolate, and of course its step size is less than or equal to the X and y steps, which is equivalent to the refinement of the mesh. V is the value at the sample point. A VQ is the value at which the interpolation point is obtained. Feel a little long-winded, look at the below bar.
What is this function for? Assuming a grayscale image, the coordinate matrix is x and Y, one represents the row, one represents the column, and the corresponding pixel value matrix is v. It is now bilinear interpolation at XQ and YQ, which returns the pixel matrix VQ of the new interpolation point.
Now let's take a look:
[X,y] = Meshgrid ( -3:3); % generate grid coordinate matrix, step is 1
V = peaks (x,y); % produces a three-dimensional Gaussian distribution
Figure
Surf (x,y,v)
title (' Original sampling ');
[Xq,yq] = Meshgrid ( -3:0.25:3); % generate interpolation point coordinate matrix, step is 0.25
Vq = Interp2 (X,Y,V,XQ,YQ); % is interpolated, returns VQ
Figure
Surf (XQ,YQ,VQ);
Title (' Linear interpolation Using finer Grid ');
The first image is the horizontal ordinate is [ -3,3], the function value is a normal distribution of stereo images. The second image is based on the first image of the results of bilinear interpolation, it can be seen from the diagram, the structure is more sophisticated.
2, C + + code
In the OPENCV function library, you can use the Remap function to accomplish the same function. Of course, you have to complete the OPENCV configuration before using this function.
Voidremap (Inputarray src, outputarray DST, Inputarray Map1, Inputarray map2, int interpolation, intbordermode=border_ CONSTANT, const scalar& bordervalue=scalar ())
SRC is the source image
DST is the target image output after interpolation
Map1 and MAP2 are interpolation point coordinates, type CV_16SC2,CV_32FC1 and CV_32FC2, and note that if MAP1 is a form of (x,y), then MAP2 is an empty map.
Interpolation is the type of interpolation method: Inter_nearest,inter_linear,inter_area,inter_cubic,inter_lanczos4.
Intbordermode is the boundary mode, when the bordermode=border_transparent, the target image corresponding to the source image of the external point is not modified.
Bordervalue is used when the boundary is fixed, with a value of 0.
Now let's take a look at the use of this function:
#include "stdafx.h" #include "highgui.h" #include "cv.h" using namespace CV;
using namespace Std; int main (int argc, char** argv) {//generate Flowmap model cvfilestorage* fx=cvopenfilestorage ("Result.txt", 0,cv_s
Torage_write);//ask storage for Save file Mat Xmesh = Cvcreatemat (3, 3, 5);
Mat Ymesh = Cvcreatemat (3, 3, 5);
for (int i = 0; i < xmesh.rows. i++) for (int j = 0; J < Xmesh.cols; J +) {xmesh.at (i,j) =i*0.5;
ymesh.at (i,j) =j*0.5;
}//generate Optical Flow folder Mat U=cvcreatemat (3, 3, 5);
Mat V=cvcreatemat (3, 3, 5);
for (int i = 0; I (i,j) = (i+1) *0.1;
v.at (i,j) = (j+1) *0.1;
} Remap (U,v,ymesh,xmesh,inter_linear,0,cvscalarall (0));
Convert mat to iplimage iplimage* xmesh_a = Cvcloneimage (& (Iplimage) Xmesh);
iplimage* ymesh_a = Cvcloneimage (& (Iplimage) Ymesh);
iplimage* u_a = Cvcloneimage (& (Iplimage) u); iplimage* v_a = Cvcloneimage (& (Iplimage) v);
Save End to TXT cvwrite (FX, "Xmesh", Xmesh_a,cvattrlist ());
Cvwrite (FX, "Ymesh", Ymesh_a,cvattrlist ());
Cvwrite (FX, "U", U_a,cvattrlist ());
Cvwrite (FX, "V", V_a,cvattrlist ());
Cvreleasefilestorage (&FX);
Waitkey ();
} results are as follows:
%yaml:1.0
Xmesh:!! Opencv-image
width:3
height:3
origin:top-left
layout:interleaved
dt:f
data: [0., 0., 0., 5.00000000e-001, 5.00000000e-001, 5.00000000e-001,
1, 1., 1.]
Ymesh:!! Opencv-image
width:3
height:3
origin:top-left
layout:interleaved
dt:f
data: [0., 5.00000000e-001, 1., 0., 5.00000000e-001, 1, 0.,
5.00000000e-001, 1.]
U:!! Opencv-image
width:3
height:3
origin:top-left
layout:interleaved
dt:f
data: [ 1.00000001e-001, 1.00000001e-001, 1.00000001e-001,
2.00000003e-001, 2.00000003e-001, 2.00000003e-001,
3.00000012e-001, 3.00000012e-001, 3.00000012e-001]
V:!! Opencv-image
width:3
height:3
origin:top-left
layout:interleaved
dt:f
data: [ 1.00000001e-001, 1.00000001e-001, 1.00000001e-001,
1.50000006e-001, 1.50000006e-001, 1.50000006e-001,
2.00000003e-001, 2.00000003e-001, 2.00000003e-001]
Observe this result, you will be surprised to find that the remap function in OpenCV really realizes the INTERP2 function in Matlab, oh yes, perfect.