这一章讲了如下内容:
1.图像算数运算:图像加法、图像减法、图像乘法、图像除法
2.图像逻辑运算:图像与、图像或、图像取反(非)、图像异或
代码:具体见注释
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
void main()
{
Mat img1 = imread("1.jpg");
Mat img2 = imread("2.jpg");
Mat dst; //存储结果
imshow("img1", img1);
imshow("img2", img2);
cout<<"img1 "<<int(img1.at<Vec3b>(10, 10)[0])<<endl;
cout<<"img2 "<<int(img2.at<Vec3b>(10, 10)[0])<<endl;
//图像加法
//dst = img1 + img2; //待操作的两张图片的尺寸和类型需一样.效果同add(),大于255取255
//add(img1, img2, dst);//待操作的两张图片的尺寸和类型需一样.效果同'+',大于255取255
//addWeighted(img1, 0.5, img2, 0.5, 0, dst);
//图像减法
//dst = img1 - img2; //负数取0
//subtract(img1, img2, dst);//同上
//absdiff(img1, img2, dst);//负数取绝对值
//图像乘除法
//dst = 5 * img1 ; //大于255取255
//dst = img1 / 5;
//图像逻辑运算
//bitwise_and(img1, img2, dst);
//bitwise_or(img1, img2, dst);
//bitwise_not(img1, dst);
bitwise_xor(img1, img2, dst);
cout<<"dst "<<int(dst.at<Vec3b>(10, 10)[0])<<endl;
imshow("dst", dst);
waitKey(0);
}
//图像加法
//dst = img1 + img2; //待操作的两张图片的尺寸和类型需一样.效果同add(),大于255取255
//add(img1, img2, dst);//待操作的两张图片的尺寸和类型需一样.效果同'+',大于255取255
//addWeighted(img1, 0.5, img2, 0.5, 0, dst);
//图像减法
//dst = img1 - img2; //负数取0
//subtract(img1, img2, dst);//同上
//absdiff(img1, img2, dst);//负数取绝对值
//图像乘除法
//dst = 5 * img1 ; //大于255取255
//dst = img1 / 5;
//图像逻辑运算
//bitwise_and(img1, img2, dst);
//bitwise_or(img1, img2, dst);
//bitwise_not(img1, dst);
bitwise_xor(img1, img2, dst);
cout<<"dst "<<int(dst.at<Vec3b>(10, 10)[0])<<endl;
imshow("dst", dst);
waitKey(0);
}
运行结果:见ppt,略