The position of each pixel is expressed by remapping:g(x,y)=f(h(x,y)), and h(x,y) is a mapping method function. When h(x,y) = (I.cols()-x,y), it means that deflection occurs in the x-axis direction.
Function: Imgproc.remap(Mat src, Mat dst, Mat map1, Mat map2, int interpolation, int borderMode, Scalar borderValue)
Parameter description:
src: source image
dst: Target image
map1: It has two possible objects to represent, one is the first map representing the point (x,y), and the other is the X value of type CV_16SC2, CV_32FC1, and CV_32FC2
map2: It has two possible objects to represent. One is when map1 represents the first map of point (x,y), which does not represent any value. The other is the Y value of type CV_16UC1 and CV_32FC1
Interpolation: Interpolation method, INTER_AREA does not support
borderMode: Boundary mode, default BORDER_CONTANT
borderValue: The value used when there is a constant boundary, default is 0
Sample code:
public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat src = Imgcodecs.imread("F://t0105b07b97736d453d.jpg"); Mat dst = src.clone(); Mat map_x = new Mat(src.size(), CvType.CV_32FC1); Mat map_y = new Mat(src.size(), CvType.CV_32FC1); int key = 1; // key values 1, 2, 3, 4 for (int i = 0; i < src.rows(); i++) { for (int j = 0; j < src.cols(); j++) { switch (key) { case 1: // Remap 1 if (j > src.cols() * 0.25 && j < src.cols() * 0.75 && i > src.rows() * 0.25 && i < src.rows() * 0.75) { map_x.put(i, j, 2 * (j - src.cols() * 0.25) + 0.5); map_y.put(i, j, 2 * (i - src.rows() * 0.25) + 0.5); } else { map_x.put(i, j, 0.0); map_y.put(i, j, 0.0); } break; case 2: // Remap 2 map_x.put(i, j, j); map_y.put(i, j, src.rows() - i); break; case 3: // Remap 3 map_x.put(i, j, src.cols() - j); map_y.put(i, j, i); break; case 4: // Remap 4 map_x.put(i, j, src.cols() - j); map_y.put(i, j, src.rows() - i); break; default: break; } } } Imgproc.remap(src, dst, map_x, map_y, Imgproc.INTER_LINEAR, Core.BORDER_CONSTANT, new Scalar(0, 0, 0)); Imgcodecs.imwrite("F://dst.jpg", dst); } Source image:
The first mapping:
The second mapping:
The third mapping:
The fourth mapping:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.