OpenCV-Python快速入门(三):图像运算

OpenCV-Python快速入门(三):图像运算

    • 前言
    • 前提条件
    • 实验环境
    • 图像运算
      • 加法运算”+“、add()
        • "+"运算
        • add()
        • 显示"+"、add()效果图
      • 图像加权和addWeighted()
        • 显示addWeighted()效果图
      • 按位运算
      • 掩模mask
    • 参考文献

前言

  • 本文是个人快速入门OpenCV-Python的电子笔记,由于水平有限,难免出现错漏,敬请批评改正。
  • 更多精彩内容,可点击进入
    OpenCV-Python快速入门专栏或我的个人主页查看

前提条件

  • 熟悉Python

实验环境

  • Python 3.x (面向对象的高级语言)
  • OpenCV 4.0(python第三方库)pip3 install opencv-python

图像运算

加法运算”+“、add()

"+"运算

a + b = { a + b , a + b ≤ 255 ( a + b ) % 256 , a + b > 255 a+b= \begin{cases} a+b,\quad a+b\leq 255\\ (a+b)\%256, \quad a+b>255 \end{cases} a+b={a+b,a+b255(a+b)%256,a+b>255

import cv2
img1=cv2.imread("1.jpg",0)
img2=cv2.imread("2.jpg",0)
img1_resize=cv2.resize(img1,(5,5))
img2_resize=cv2.resize(img2,(5,5))
print("img1_resize=\n",img1_resize)
print("img2_resize=\n",img2_resize)
print("img1_resize+img2_resize=\n",img1_resize+img2_resize)

OpenCV-Python快速入门(三):图像运算_第1张图片

注:(139+149)%256=32

add()

a + b = { a + b , a + b ≤ 255 255 , a + b > 255 a+b= \begin{cases} a+b,\quad a+b\leq 255\\ 255, \quad a+b>255 \end{cases} a+b={a+b,a+b255255,a+b>255

import cv2
img1=cv2.imread("1.jpg",0)
img2=cv2.imread("2.jpg",0)
img1_resize=cv2.resize(img1,(5,5))
img2_resize=cv2.resize(img2,(5,5))
print("img1_resize=\n",img1_resize)
print("img2_resize=\n",img2_resize)
img3 = cv2.add(img1_resize,img2_resize)
print("cv2.add(img1_resize,img2_resize)=\n",img3)

OpenCV-Python快速入门(三):图像运算_第2张图片

显示"+"、add()效果图

import cv2
img1=cv2.imread("1.jpg",0)
img2=cv2.imread("2.jpg",0)
img1_resize=cv2.resize(img1,(400,400))
img2_resize=cv2.resize(img2,(400,400))
cv2.imshow("origin",img1_resize)
cv2.imshow("img1_resize+img2_resize",img1_resize+img2_resize)
cv2.imshow("cv2.add(img1_resize,img2_resize)",cv2.add(img1_resize,img2_resize))
cv2.waitKey()
cv2.destroyAllWindows()

图像加权和addWeighted()

  • res=cv2.addWeighted(img1, alpha, img2, beta, gamma)
  • r e s = i m g 1 × α + i m g 2 × β + γ res=img1\times \alpha+img2\times\beta+\gamma res=img1×α+img2×β+γ
  • 通俗地说,结果图像=图像1×系数1+图像2×系数2+亮度调节量
import cv2
img1=cv2.imread("1.jpg",0)
img2=cv2.imread("2.jpg",0)
img1_resize=cv2.resize(img1,(3,3))
img2_resize=cv2.resize(img2,(3,3))
res=cv2.addWeighted(img1_resize,0.6,img2_resize,5,3)
print("img1_resize=\n",img1_resize)
print("img2_resize=\n",img2_resize)
print("cv2.addWeighted(img1_resize,0.6,img1_resize,5,3)=\n",res)

OpenCV-Python快速入门(三):图像运算_第3张图片

注:1920.6+05+3=118

显示addWeighted()效果图

import cv2
img1=cv2.imread("1.jpg",0)
img2=cv2.imread("2.jpg",0)
img1_resize=cv2.resize(img1,(400,400))
img2_resize=cv2.resize(img2,(400,400))
res=cv2.addWeighted(img1_resize,0.6,img2_resize,5,3)
cv2.imshow("img1_resize",img1_resize)
cv2.imshow("img2_resize",img2_resize)
cv2.imshow("cv2.addWeighted(img1_resize,0.6,img2_resize,5,3)",res)
cv2.waitKey()
cv2.destroyAllWindows()

按位运算

函数名 基本含义
cv2.bitwise_and() 按位与
cv2.bitwise_or() 按位或
cv2.bitwise_xor() 按位异或
cv2.bitwise_not() 按位取反
import cv2
img1=cv2.imread("1.jpg",0)
img2=cv2.imread("2.jpg",0)
img1_resize=cv2.resize(img1,(400,400))
img2_resize=cv2.resize(img2,(400,400))
cv2.imshow("cv2.bitwise_and(img1_resize,img2_resize)",cv2.bitwise_and(img1_resize,img2_resize))
cv2.imshow("cv2.bitwise_or(img1_resize,img2_resize)",cv2.bitwise_or(img1_resize,img2_resize))
cv2.imshow("cv2.bitwise_xor(img1_resize,img2_resize)",cv2.bitwise_xor(img1_resize,img2_resize))
cv2.imshow("cv2.bitwise_not(img1_resize,img2_resize)",cv2.bitwise_not(img1_resize,img2_resize))
cv2.waitKey()
cv2.destroyAllWindows()

掩模mask

res=cv2.add(img1 , img2 , mask)

import cv2
import numpy as np
img1=cv2.imread("1.jpg",0)
img2=cv2.imread("2.jpg",0)
img1_resize=cv2.resize(img1,(5,5))
img2_resize=cv2.resize(img2,(5,5))
mask=np.zeros((5,5),dtype=np.uint8)
mask[2:4,2:4]=1
res=cv2.add(img1_resize,img2_resize, mask=mask)
print("img1_resize=\n",img1_resize)
print("img2_resize=\n",img2_resize)
print("mask=\n",mask)
print("cv2.add(img1_resize,img2_resize, mask=mask)=\n",res)

OpenCV-Python快速入门(三):图像运算_第4张图片

import cv2
import numpy as np
img1=cv2.imread("1.jpg",0)
img2=cv2.imread("2.jpg",0)
img1_resize=cv2.resize(img1,(400,400))
img2_resize=cv2.resize(img2,(400,400))
mask=np.ones((400,400),dtype=np.uint8)
mask[200:400,200:400]=0
res=cv2.add(img1_resize,img2_resize, mask=mask)
print("img1_resize=\n",img1_resize)
print("img2_resize=\n",img2_resize)
print("mask=\n",mask)
print("cv2.add(img1_resize,img2_resize, mask=mask)=\n",res)
cv2.imshow("cv2.add(img1_resize,img2_resize, mask=mask)",res)
cv2.waitKey()
cv2.destroyAllWindows()

OpenCV-Python快速入门(三):图像运算_第5张图片

参考文献

[1] https://opencv.org/

  • 更多精彩内容,可点击进入
    OpenCV-Python快速入门专栏或我的个人主页查看

你可能感兴趣的:(opencv,python,计算机视觉)