原理
通过几何变换对原图进行旋转,放大,缩小:其实就是一种src 到 dst的一种映射关系。
比如说:对图像进行左右翻转:
d
s
t
=
(
s
r
c
.
c
o
l
s
−
x
,
y
)
dst=(src.cols-x,y)
dst=(src.cols−x,y)
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; void updata_map(int& ind, Mat& map_x,Mat& map_y); Mat src; int main(void) { src = imread("../res/beauty.jpg",cv::IMREAD_COLOR); if(src.empty()) { cout << "can't load image,please confirm your path"<< endl; } Mat dst(src.size(),src.type()); Mat map_x(src.size(), CV_32FC1); //x方向的映射关系 Mat map_y(src.size(), CV_32FC1);//y方向的映射关系 const char* window_name = "Remapping Demo" ; namedWindow(window_name, cv::WINDOW_AUTOSIZE); int ind =0; for(;;) { updata_map(ind,map_x,map_y); //更新映射关系 remap(src,dst,map_x,map_y,cv::INTER_LINEAR,cv::BORDER_CONSTANT,Scalar(0,0,0)); imshow(window_name,dst); char c = (char)waitKey(1000); if(c == 27) { break; } } return 0; }; void updata_map(int& ind, Mat& map_x,Mat& map_y) { for(int i=0; i<src.rows; i++) for(int j=0; j<src.cols; j++) { switch(ind) { case 0: if(i>src.rows*0.25 && i<src.rows*0.75 && j>src.cols*0.25 && j<src.cols*0.75) //在中间 { map_x.at<float>(i,j) = 2*j-0.5*src.cols+0.5; //x进行缩小一半 见 下面图的说明 map_y.at<float>(i,j) = 2*i-0.5*src.rows+0.5;// x进行缩小一半 见 下面图的说明 } else//没有映射关系 { map_x.at<float>(i,j) = 0; map_y.at<float>(i,j) = 0; } break; case 1: map_x.at<float>(i,j) = j; map_y.at<float>(i,j) =src.rows-i; //y方向翻转 break; case 2: map_x.at<float>(i,j) = src.cols-j; //x方向翻转 map_y.at<float>(i,j) = i; break; case 3: map_x.at<float>(i,j) = src.cols-j; //x方向翻转 map_y.at<float>(i,j) = src.rows-i; //y方向翻转 break; default: break; } } ind = (ind+1)%4; }
结果:




OpenCV API
void cv::remap
(
InputArray src, // 输入图像
OutputArray dst, // 输出图像
InputArray map1, // x方向对应关系map_x
InputArray map2, // y方向对应关系map_y
int interpolation, // 对于不是整数坐标的(x,y)的插值方法(因为有时候map_xy里面坐标不是整数)
int borderMode = BORDER_CONSTANT, // 边界处理方式
const Scalar & borderValue = Scalar() // 如果是 BORDER_CONSTANT ,此参数代表边界值
)
最后
以上就是闪闪钻石最近收集整理的关于Remapping的全部内容,更多相关Remapping内容请搜索靠谱客的其他文章。
发表评论 取消回复