- 本文是个人快速入门OpenCV-Python的电子笔记,由于水平有限,难免出现错漏,敬请批评改正。
- 更多精彩内容,可点击进入
OpenCV-Python快速入门专栏或我的个人主页查看
- 熟悉Python
- Python 3.x (面向对象的高级语言)
- OpenCV 4.0(python第三方库)
pip3 install opencv-python
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+b≤255(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)
注:(139+149)%256=32
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+b≤255255,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)
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()
- 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)
注:1920.6+05+3=118
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()
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)
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()
[1] https://opencv.org/
- 更多精彩内容,可点击进入
OpenCV-Python快速入门专栏或我的个人主页查看