目录
1--图像混合的代码
# include<opencv2/opencv.hpp>
# include<cstdio>
using namespace cv;
int main(int argc, char *argv[]){
Mat img1 = imread("./test1.jpg");
Mat img2 = imread("./test2.jpg");
resize(img2, img2, img1.size()); // resize
Mat dst;
float a = 0.7;
addWeighted(img1, a, img2, 1-a, 1, dst);
namedWindow("test1");
namedWindow("test2");
namedWindow("blending");
imshow("test1", img1);
imshow("test2", img2);
imshow("blending", dst);
waitKey(0);
return 0;
}
原理公式:dst = img1 * a + img2 * (1-a) + gamma;
2--图像混合的结果
3--图像旋转和镜像的代码
# include<opencv2/opencv.hpp>
# include<cstdio>
using namespace cv;
int main(int argc, char *argv[]){
Mat img = imread("./test1.jpg");
Mat rot;
Mat flip1;
Mat flip2;
Mat flip3;
cv::rotate(img, rot, ROTATE_90_CLOCKWISE); // 顺时针90°旋转
cv::flip(img, flip1, 0); // 上下翻转
cv::flip(img, flip2, 1); // 左右翻转
cv::flip(img, flip3, -1); // 上下左右翻转
namedWindow("origin");
namedWindow("rot");
namedWindow("flip1");
namedWindow("flip2");
namedWindow("flip3");
imshow("origin", img);
imshow("rot", rot);
imshow("flip1", flip1);
imshow("flip2", flip2);
imshow("flip3", flip3);
waitKey(0);
return 0;
}
4--图像旋转和镜像的结果
5--图像并排的代码:
# include<opencv2/opencv.hpp>
# include<cstdio>
using namespace cv;
int main(int argc, char *argv[]){
Mat img1 = imread("test1.jpg");
Mat img2 = imread("test2.jpg");
Mat des;
int height = img1.rows;
int width1 = img1.cols;
int width2 = img2.cols;
// 将图片大小 缩放成 一致(与尺寸较小的一致)
if (img1.rows > img2.rows){
height = img2.rows;
width1 = img1.cols * (float(img2.rows) / float(img1.rows));
resize(img1, img1, Size(width1, height));
}
else if (img1.rows < img2.rows){
height = img1.rows;
width2 = img2.cols * (float(img1.rows) / float(img2.rows));
resize(img2, img2, Size(width2, height));
}
des.create(height, width1 + width2, img1.type());
Mat r1 = des(Rect(0, 0, width1, height)); // 创建roi
img1.copyTo(r1);
Mat r2 = des(Rect(width1, 0, width2, height));
img2.copyTo(r2);
namedWindow("origin1");
namedWindow("origin2");
namedWindow("test");
imshow("origin1", img1);
imshow("origin2", img2);
imshow("test", des);
waitKey(0);
return 0;
}