(opencv) image geometric transformation - coordinate mapping

The geometric transformation of the image refers to the transformation of the image pixels and the retrograde space set without changing the image pixel value. Common geometric transformations include distance transformation, coordinate mapping, translation, mirroring, rotation, scaling, affine transformation, etc.

coordinate mapping

The coordinate mapping of the image is to establish a mapping relationship between the original image and the target image. There are two types of this mapping relationship: one is to calculate the coordinate position of any pixel in the original image after the mapped image; the other is to calculate the transformed image Any pixel response is set at the coordinates of the original image. The mapping from the original image to the target image is called forward mapping, on the contrary, the original image obtained from the target image through the mapping relationship is called reverse mapping. Since the forward mapping often has incomplete mapping and repeated mapping, inverse mapping is generally adopted in the process of image processing to ensure that each pixel of the output target image can find a unique corresponding pixel in the source image through the mapping relationship.

Remapping related operations are provided in OpenCV. Remapping refers to converting a pixel at a position in an image to a specified position of a zero-one image through a mapping relationship. For the input source image f(x,y), the target image is g(x,y), and the mapping relationship is T, then satisfy

g(x,y)=T(f(x,y))

It should be noted that the target image obtained through the mapping relationship T may have a non-integer pixel value, and interpolation or rounding up can generally be considered.

void remap(InputArray src,OutputArray dst,InputArray map1,InputArray map2,
	int interpolation,int borderMode=BORDER_CONSTANT,const Scallar& borderValue=Scalar())

Image remapping operations. map1 indicates the coordinates of (x, y) points or x coordinates, which can be CV_16SC2, CV_32FC1, CV_32FC2 types; map2 indicates y coordinates, which can be CV_16UC1, CV_32FC1 types, if map1 is (x, y), map2 can be selected without; interpolation indicates the interpolation method; borderMode indicates the boundary interpolation type; borderValue indicates the interpolation value. 

 Application examples

#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
	Mat src = imread("C:\\Users\\32498\\Pictures\\16.png");
	if (!src.data)
	{
		return -1;
	}
	//输出矩阵定义
	Mat result(src.size(), src.type());
	//x方向与y方向矩阵
	Mat xMap(src.size(), CV_32FC1);
	Mat yMap(src.size(), CV_32FC1);
	//取图像的宽和高
	int rows = src.rows;
	int cols = src.cols;

	//图像遍历
	for (int j = 0; j < rows; j++)
	{
		for (int i = 0; i < cols; i++)
		{
			//x与y均翻转
			xMap.at<float>(j, i) = cols - i;
			yMap.at<float>(j, i) = rows - j;
		}
	}
	//重映射操作
	remap(src, result, xMap, yMap, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
	//输出结果
	imshow("src", src);
	imshow("result", result);
	waitKey();
	return 0;
}

Guess you like

Origin blog.csdn.net/yangSHU21/article/details/131139189