倾斜:
Mat img = imread("1.png");
Mat dst, dst2, dst3, dst4;
Point2f imgPoints[3];
Point2f dstPoints[3];
//cout <<dstPoints[3]; //倾斜
imgPoints[0] = Point2f(0, 0); //0,0
imgPoints[1] = Point2f(0, img.rows);//0,375
imgPoints[2] = Point2f(img.cols, 0);//585,0
//cout << imgPoints[2];
dstPoints[0] = Point2f(0, img.rows*0.3); //[0, 112.2]
dstPoints[1] = Point2f(img.cols*0.25, img.rows*0.75);//[146.25, 280.5]
dstPoints[2] = Point2f(img.cols*0.75, img.rows*0.25); //[438.75, 93.5]
cout << dstPoints[0];
Mat M1 = getAffineTransform(imgPoints, dstPoints); //由三个点对计算变换矩阵
warpAffine(img, dst, M1, img.size());//倾斜
imshow("原图", img);
imshow("修改", dst);
waitKey(0);
效果:
平移:
imgPoints[0] = Point2f(0, 0); //0,0 //平移
imgPoints[1] = Point2f(0, img.rows);//0,375
imgPoints[2] = Point2f(img.cols, 0);//585,0
dstPoints[0] = Point2i(img.cols / 3, 0);
dstPoints[1] = Point2i(img.cols / 3, img.rows);
dstPoints[2] = Point2i(img.cols + img.cols / 3, 0);
cout << dstPoints[2];
Mat M3 = getAffineTransform(imgPoints, dstPoints); //由三个点对计算变换矩阵
//warpAffine(img, dst, M1, img.size()); //倾斜
warpAffine(img, dst, M3, Size(img.cols + img.cols / 3, img.rows));
imshow("原图", img);
imshow("修改", dst);
waitKey(0);
效果:
旋转:
Point center(img.cols / 2, img.rows / 2);//旋转中心
//Point center(img.rows / 3, img.cols / 3);//旋转中心
double angle = 45;//逆时针旋转45度 正数逆时针负数顺时针
double scale = 1;//缩放比例
Mat M2 = getRotationMatrix2D(center, angle, scale);
warpAffine(img, dst2, M2, img.size());
imshow("原图", img);
imshow("修改", dst2);
waitKey(0);
效果:
翻转:
Point2f imgPoints2[3]; //翻转
Point2f stdPoints2[3];
imgPoints2[0] = Point2i(0, 0);
imgPoints2[1] = Point2i(0, img.rows);
imgPoints2[2] = Point2i(img.cols, 0);
stdPoints2[0] = Point2i(img.cols, 0);
stdPoints2[1] = Point2i(img.cols, img.rows);
stdPoints2[2] = Point2i(0, 0);
Mat M4 = getAffineTransform(imgPoints2, stdPoints2);
warpAffine(img, dst3, M4, Size(img.cols, img.rows));
imshow("原图", img);
imshow("修改", dst3);
waitKey(0);
效果:
放大放小:
//放大放小
float p = 1.5; //缩小放大多少倍
Mat src2(Size(int(img.cols*p), int(img.rows*p)), img.type(), Scalar(0));
for (int i = 0; i < src2.rows; i++)
{
for (int j = 0; j < src2.cols; j++)
{
//if ((0 <= int(i / p) && int(i / p) < img.rows) && (0 <= int(j / p) && int(j / p) < img.cols)) {
src2.at<Vec3b>(i, j) = img.at<Vec3b>(int(i / p), int(j / p));
//}
}
}
imshow("img", img);
imshow("放大缩小", src2);
waitKey(0);
效果: