#include
#include
using namespace std;
using namespace cv;
#include
int main()
{
Mat img = imread("colors.jpg");
if (img.empty()) {
cout << "图像读取失败" << endl;
return -1;
};
Mat src, small_img, near, linear, cubic;
resize(img, small_img, Size(200, 200), 0, 0, INTER_AREA); //将图像变小
imshow("small_img", small_img);
resize(small_img, near, Size(800, 800), 0, 0, INTER_NEAREST); //最近邻插值
resize(small_img, linear, Size(800, 800), 0, 0, INTER_LINEAR); //线性插值
resize(small_img, cubic, Size(800, 800), 0, 0, INTER_CUBIC); //立方插值
imshow("near", near); //展示不同插值方式放大图像的效果
imshow("linear", linear);
imshow("cubic", cubic);
waitKey(0);
return 0;
}
#include
#include
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("colors.jpg");
if (img.empty()) {
cout << "图片读取失败" << endl;
return -1;
}
Mat h_flip, v_flip, hv_flip;
flip(img, h_flip, 0); //水平翻转
flip(img, v_flip, 1); //垂直翻转
flip(img, hv_flip, -1); //先水平翻转再竖直翻转
vectorout; //并排展示翻转的结果
Mat result;
out.push_back(h_flip);
out.push_back(v_flip);
out.push_back(hv_flip);
hconcat(out, result);
imshow("out", result);
waitKey(0);
return 0;
}
#include
#include
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("colors.jpg");
if (img.empty()) {
cout << "图像读取失败" << endl;
return -1;
}
Size dst_size(img.rows, img.cols); //设置输出图像的的大小
// -------------根据旋转角度变换------------------
double angle = 45;
Point2f center(img.rows / 2.0, img.cols / 2.0); //设置旋转中心
Mat r_mat1 = getRotationMatrix2D(center, angle, 1); //计算放射变换的矩阵
Mat warp1;
warpAffine(img,warp1, r_mat1, dst_size); //进行仿射变换(此处为旋转45°)
imshow("warp1", warp1);
//--------------根据定义的3个点进行变换-------------
Point2f src_p[3];
Point2f dst_p[3];
//原图上的3个点
src_p[0] = Point2f(0, 0);
src_p[1] = Point2f(0, (float)(img.cols - 10));
src_p[2] = Point2f((float)(img.rows - 10), (float)(img.cols - 10));
//原图上3个点在目标图中的位置
dst_p[0] = Point2f((float)(img.rows) * 0.1, (float)(img.cols) * 0.2);
dst_p[1] = Point2f((float)(img.rows) * 0.15, (float)(img.cols) * 0.7);
dst_p[2] = Point2f((float)(img.rows) * 0.8, (float)(img.cols) * 0.9);
Mat r_mat2, warp2;
r_mat2 = getAffineTransform(src_p, dst_p); //根据对应的点求变换矩阵
cout << "变换矩阵为:" << r_mat2 << endl;
warpAffine(img, warp2, r_mat2, dst_size); //通过变换矩阵进行仿射变换
imshow("warp2", warp2);
waitKey(0);
return 0;
}
#include
#include
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("colors.jpg");
if (img.empty()) {
cout << "图像读取失败" << endl;
return -1;
}
Point2f src_p[4], dst_p[4];
src_p[0] = Point2f(200, 300); //原图中的四个点
src_p[1] = Point2f(300, 600);
src_p[2] = Point2f(600, 800);
src_p[3] = Point2f(800, 800);
dst_p[0] = Point2f(10, 800); //原图中的四个点对应到变换后的的坐标位置
dst_p[1] = Point2f(800, 200);
dst_p[2] = Point2f(600, 100);
dst_p[3] = Point2f(500, 10);
Mat r_mat = getPerspectiveTransform(src_p, dst_p); //通过对应的点找到两者之间的变换矩阵
Mat result;
warpPerspective(img, result, r_mat, img.size()); // 进行透视变换
imshow("img", img);
imshow("result", result);
waitKey(0);
return 0;
}
参考资料:
《OpenCV4快速入门》
GitHub - fengzhenHIT/learnOpenCV4: OpenCV 4相关文件