通过图片叠加逻辑运算可以实现多种效果,如模版截取感兴趣区域,图片融合,色彩交叉等,本文涉及4个图片矩阵叠加逻辑运算函数,如下:
1.bitwise_and
2.bitwise_or
3.bitwise_not
4.bitwise_xor
1.函数bitwise_and
定义:
void bitwise_and(InputArray src1, InputArray src2,OutputArray dst, InputArray mask = noArray());
def bitwise_and(src1, src2, dst=None, mask=None)
参数:
src1:输入图像或矩阵1
src2:输入图像或矩阵2
dst:输出图像
mask:掩码,通常采用默认值
作用:将两幅图像进行与运算。
使用案例
#python code: import cv2 image_mat1=cv2.imread(image1path) image_mat1=cv2.threshold(image_mat1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) image_mat2=cv2.imread(image2path) image_mat2=cv2.threshold(image_mat2, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) res = cv2.bitwise_and(image_mat1, image_mat2) plt.figure("bitwise_and") plt.title("bitwise_and") plt.imshow(res) plt.show() #C code: #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main(int argc, char** argv) { Mat src1 = imread("xxx/x1.jpg", 1); Mat src2 = imread("xxx/x2.jpg", 1); Mat andMat; bitwise_and(src1, src2, andMat); namedWindow("andMat", 0); imshow("andMat", andMat); waitKey(0); return 0; }
效果如下

2.函数bitwise_or
定义:
void bitwise_or(InputArray src1, InputArray src2,OutputArray dst, InputArray mask = noArray());
def bitwise_or(src1, src2, dst=None, mask=None)
参数:
src1:输入图像或矩阵1
src2:输入图像或矩阵2
dst:输出图像
mask:掩码,通常采用默认值
作用:将两幅图像进行或运算。
使用案例
#python code: import cv2 image_mat1=cv2.imread(image1path) image_mat1=cv2.threshold(image_mat1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) image_mat2=cv2.imread(image2path) image_mat2=cv2.threshold(image_mat2, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) res = cv2.bitwise_or(image_mat1, image_mat2) plt.figure("res_bitwise_or") plt.title("res_bitwise_or") plt.imshow(res) plt.show() #C code #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main(int argc, char** argv) { Mat src1 = imread("e:/TestImage/4.png", 1); Mat src2 = imread("e:/TestImage/6.png", 1); Mat orMat; bitwise_or(src1, src2, orMat); namedWindow("orMat", 0); imshow("orMat", orMat); waitKey(0); return 0; }
效果如下:

3.函数bitwise_not
定义:
void bitwise_not(InputArray src, OutputArray dst,InputArray mask = noArray());
def bitwise_not(src, dst=None, mask=None)
参数:
src:输入图像或矩阵1
dst:输出图像
mask:掩码,通常采用默认值
作用:将两幅图像进行非运算。
使用案例
#python code: import cv2 image_mat1=cv2.imread(image1path) image_mat1=cv2.threshold(image_mat1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) image_mat2=cv2.imread(image2path) image_mat2=cv2.threshold(image_mat2, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) res = cv2.bitwise_not(image_mat1, image_mat2) plt.figure("bitwise_not") plt.title("bitwise_not") plt.imshow(res) plt.show() #C code #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main(int argc, char** argv) { Mat src = imread("xxx/4.jpg", 1); Mat notMat; bitwise_not(src, notMat); namedWindow("notMat", 0); imshow("notMat", notMat); waitKey(0); return 0; }
效果如下:

4.函数bitwise_xor
定义:
void bitwise_xor(InputArray src1, InputArray src2,OutputArray dst, InputArray mask = noArray());
def bitwise_xor(src1, src2, dst=None, mask=None)
参数说明:
src1:输入图像或矩阵1
src2:输入图像或矩阵2
dst:输出图像
mask:掩码,通常采用默认值
作用:将两幅图像进行异或运算。
使用案例:
#python code: import cv2 image_mat1=cv2.imread(image1path) image_mat1=cv2.threshold(image_mat1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) image_mat2=cv2.imread(image2path) image_mat2=cv2.threshold(image_mat2, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) res = cv2.bitwise_xor(image_mat1, image_mat2) plt.figure("bitwise_xor") plt.title("bitwise_xor") plt.imshow(res) plt.show() #C code #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main(int argc, char** argv) { Mat src1 = imread("xxx/1.jpg", 1); Mat src2 = imread("xxx/2.jpg", 1); Mat xorMat; bitwise_xor(src1, src2, xorMat); namedWindow("xorMat", 0); imshow("xorMat", xorMat); waitKey(0); return 0; }
效果如下:

上篇推荐>> 一:OpenCV图片读取与写入文件