• 二:OpenCV图片叠加逻辑运算


    通过图片叠加逻辑运算可以实现多种效果,如模版截取感兴趣区域,图片融合,色彩交叉等,本文涉及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 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    效果如下
    在这里插入图片描述
    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 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    效果如下:
    在这里插入图片描述
    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 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    效果如下
    在这里插入图片描述

    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 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    效果如下
    在这里插入图片描述
    在这里插入图片描述
    文章来源>>【二:OpenCV图片叠加逻辑运算详细说明】更多使用方法本站将持续更新,欢迎Star

  • 相关阅读:
    2022 年夏招 Java 面试题,看到必进。
    TypeScript-02基础知识之函数
    python将csv数据导入neo4j
    Spring高手之路13——BeanFactoryPostProcessor与BeanDefinitionRegistryPostProcessor解析
    Dockerfile中安装crontab
    项目接入腾讯云短信服务SMS实现向用户发送手机验证码
    大数据关键技术:自然语言处理入门篇
    mac系统如何安装nacos(window系统通用)?详细教程一文解决
    【HarmonyOS】鸿蒙入门学习
    【微服务部署】07-调用链追踪
  • 原文地址:https://blog.csdn.net/leifengpeng/article/details/126145926