矩阵自定义初始化
Mat kern = (Mat_<char>(5, 5) << -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1,
-1, -1, 24, -1, -1,
-1, -1, -1, -1, -1,
-1, -1, -1, -1, -1);
变换
缩放
强制缩放到像素大小20x20
cv::resize(frame, final,cv::Size(20,20),0,0,INTER_AREA);
缩放到0.5倍
cv::resize(frame, final,cv::Size(),0.5,0.5,INTER_AREA);
缩放到2倍
cv::resize(frame, final,cv::Size(),2,2,INTER_LINEAR);

image.png
平移
int dx=50,dy=50;
Mat m = (Mat_<double>(2,3)<<1,0,dx,0,1,dy);
warpAffine(frame, final, m, {frame.cols,frame.rows});

image.png
旋转
Mat m = getRotationMatrix2D(Point2f(frame.cols/2,frame.rows/2), 45, 1);
warpAffine(frame, final, m, {frame.cols,frame.rows});

image.png
仿射变换 / 斜切
Point2f p1[3] = {Point2f(0,0),Point2f(0,1),Point2f(1,1)};
Point2f p2[3] = {Point2f(0,0),Point2f(.1,1),Point2f(1.1,1.1)};
Mat m = getAffineTransform(p1, p2);
warpAffine(frame, final, m, {frame.cols,frame.rows});

image.png
透视变换
Point2f p1[4] = {Point2f(0,0),Point2f(0,100),Point2f(100,100),Point2f(100,0)};
Point2f p2[4] = {Point2f(10,15),Point2f(15,90),Point2f(80,100),Point2f(90,20)};
Mat m = getPerspectiveTransform(p1, p2);
warpPerspective(frame, final, m, {frame.cols,frame.rows});

image.png
基础矩阵操作,很多语言中都涉及到。