威望0
积分7966
贡献0
在线时间763 小时
UID1
注册时间2021-4-14
最后登录2024-11-23
管理员
- UID
- 1
- 威望
- 0
- 积分
- 7966
- 贡献
- 0
- 注册时间
- 2021-4-14
- 最后登录
- 2024-11-23
- 在线时间
- 763 小时
|
[mw_shl_code=cpp,true]#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
using namespace std;
int main(int argc, char* argv) {
Mat src, dst;
src = imread("D://opencv//picture//space.jfif");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
namedWindow("original image", CV_WINDOW_AUTOSIZE);
imshow("original image", src);
Mat dst_warp, dst_warpRotateScale, dst_warpTransformation, dst_warpFlip;
Point2f srcPoints[3];
Point2f dstPoints[3];
srcPoints[0] = Point2f(0, 0);
srcPoints[1] = Point2f(0, src.rows);
srcPoints[2] = Point2f(src.cols, 0);
dstPoints[0] = Point2f(0, src.rows*0.3);
dstPoints[1] = Point2f(src.cols*0.25, src.rows*0.75);
dstPoints[2] = Point2f(src.cols*0.75, src.rows*0.25);
Mat M1 = getAffineTransform(srcPoints, dstPoints);
warpAffine(src, dst_warp, M1, src.size());
Point2f center(src.cols / 2, src.rows / 2);
double angle = 45;
double scale = 0.5;
Mat M2 = getRotationMatrix2D(center, angle, scale);
warpAffine(src, dst_warpRotateScale, M2, Size(src.cols, src.rows), INTER_LINEAR);
//仿射变换—平移
Point2f srcPoints1[3];
Point2f dstPoints1[3];
srcPoints1[0] = Point2i(0, 0);
srcPoints1[1] = Point2i(0, src.rows);
srcPoints1[2] = Point2i(src.cols, 0);
dstPoints1[0] = Point2i(src.cols / 3, 0);
dstPoints1[1] = Point2i(src.cols / 3, src.rows);
dstPoints1[2] = Point2i(src.cols + src.cols / 3, 0);
Mat M3 = getAffineTransform(srcPoints1, dstPoints1);
warpAffine(src, dst_warpTransformation, M3, Size(src.cols + src.cols / 3, src.rows));
Point2f srcPoints2[3];
Point2f dstPoints2[3];
srcPoints2[0] = Point2i(0, 0);
srcPoints2[1] = Point2i(0, src.rows);
srcPoints2[2] = Point2i(src.cols, 0);
dstPoints2[0] = Point2i(src.cols, 0);
dstPoints2[1] = Point2i(src.cols, src.rows);
dstPoints2[2] = Point2i(0, 0);
Mat M4 = getAffineTransform(srcPoints2, dstPoints2);
warpAffine(src, dst_warpFlip, M4, Size(src.cols, src.rows));
//flip(src, dst_warpFlip, 1);// flipCode:= 0 图像向下翻转
//> 0 图像向右翻转
//< 0 图像同时向下向右翻转
imshow("affine transformation1(三点法)", dst_warp);
imshow("affine transfoemation2(指定比例和角度)", dst_warpRotateScale);
imshow("affine transfoemation3(仿射变换平移)", dst_warpTransformation);
imshow("affine transformation4(仿射变换镜像)", dst_warpFlip);
waitKey(0);
return 0;
}
[/mw_shl_code] |
|