OpenCV(python)从入门到精通——运算操作

  • 加法减法操作
python 复制代码
import cv2 as cv
import numpy as np

x = np.uint8([250])
y = np.uint8([10])

x_1 = np.uint8([10])
y_1 = np.uint8([20])

# 加法,相加最大只能为255
print(cv.add(x,y))

# 减法,相互减最小值只能为0
print(cv.subtract(x_1,y_1))
  • 图像加法
python 复制代码
import cv2 as cv
import numpy as np

img1 = cv.imread("./images/1.jpg")

img2 = cv.imread("./images/1_test.jpg")


img = cv.add(img1,img2)

cv.imshow("pic",img)
cv.waitKey(0)
  • 图像权重相加,因为我使用的都是同一张图片
python 复制代码
import cv2 as cv
import numpy as np

img1 = cv.imread("./images/1.jpg")

img2 = cv.imread("./images/1_test.jpg")


# img = cv.add(img1,img2)
img = cv.addWeighted(img1,0.5,img2,0.8,gamma=0)
cv.imshow("pic",img)
cv.waitKey(0)
  • 按位与操作
python 复制代码
import cv2 as cv
import numpy as np

img1 = cv.imread("./images/1.jpg")

img2 = cv.imread("./images/1_test.jpg")


# img = cv.add(img1,img2)
# img = cv.addWeighted(img1,0.5,img2,0.8,gamma=0)
rows,cols,channels = img2.shape
roi = img1[0:rows,0:cols]
img2gray = cv.cvtColor(img2,cv.COLOR_BGR2GRAY)
ret,mask = cv.threshold(img2gray,10,255,cv.THRESH_BINARY|cv.THRESH_OTSU)
mask_inv = cv.bitwise_not(mask)

img1_bg = cv.bitwise_and(roi,roi,mask=mask_inv)
cv.imshow("img1_bg",img1_bg)
cv.waitKey(0)
  • 按位取反操作
python 复制代码
import cv2 as cv
import numpy as np

img1 = cv.imread("./images/1.jpg")

img2 = cv.imread("./images/1_test.jpg")


# img = cv.add(img1,img2)
# img = cv.addWeighted(img1,0.5,img2,0.8,gamma=0)
rows,cols,channels = img2.shape
roi = img1[0:rows,0:cols]
img2gray = cv.cvtColor(img2,cv.COLOR_BGR2GRAY)
ret,mask = cv.threshold(img2gray,10,255,cv.THRESH_BINARY|cv.THRESH_OTSU)
mask_inv = cv.bitwise_not(mask)

img1_bg = cv.bitwise_not(roi,roi,mask=mask_inv)
cv.imshow("img1_bg",img1_bg)
cv.waitKey(0)
  • 按位或
python 复制代码
import cv2 as cv
import numpy as np

img1 = cv.imread("./images/1.jpg")

img2 = cv.imread("./images/1_test.jpg")


# img = cv.add(img1,img2)
# img = cv.addWeighted(img1,0.5,img2,0.8,gamma=0)
rows,cols,channels = img2.shape
roi = img1[0:rows,0:cols]
img2gray = cv.cvtColor(img2,cv.COLOR_BGR2GRAY)
ret,mask = cv.threshold(img2gray,10,255,cv.THRESH_BINARY|cv.THRESH_OTSU)
cv.imshow("img3",roi)
cv.imshow("img1",mask)
mask_inv = cv.bitwise_not(mask)

img1_bg = cv.bitwise_or(roi,roi,mask=mask_inv)
cv.imshow("img1_bg",img1_bg)
cv.waitKey(0)
  • 按位异或
python 复制代码
import cv2 as cv
import numpy as np

img1 = cv.imread("./images/1.jpg")

img2 = cv.imread("./images/1_test.jpg")


# img = cv.add(img1,img2)
# img = cv.addWeighted(img1,0.5,img2,0.8,gamma=0)
rows,cols,channels = img2.shape
roi = img1[0:rows,0:cols]
img2gray = cv.cvtColor(img2,cv.COLOR_BGR2GRAY)
ret,mask = cv.threshold(img2gray,10,255,cv.THRESH_BINARY|cv.THRESH_OTSU)
cv.imshow("img3",roi)
cv.imshow("img1",mask)
mask_inv = cv.bitwise_xor(mask,mask)

img1_bg = cv.bitwise_or(roi,roi,mask=mask_inv)
cv.imshow("img1_bg",img1_bg)
cv.waitKey(0)
相关推荐
雍凉明月夜3 小时前
视觉opencv学习笔记Ⅲ
笔记·opencv·学习
ziwu20 小时前
【鸟类识别系统】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积网络+resnet50算法
图像识别
ziwu21 小时前
【宠物识别系统】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·深度学习·图像识别
ziwu1 天前
海洋生物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·深度学习·图像识别
AndrewHZ2 天前
【图像处理基石】如何在图像中提取出基本形状,比如圆形,椭圆,方形等等?
图像处理·python·算法·计算机视觉·cv·形状提取
深蓝海拓2 天前
opencv的模板匹配(Template Matching)学习笔记
人工智能·opencv·计算机视觉
滨HI02 天前
C++ opencv简化轮廓
开发语言·c++·opencv
ziwu3 天前
昆虫识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
后端·图像识别
AndrewHZ4 天前
【图像处理基石】图像处理的基础理论体系介绍
图像处理·人工智能·算法·计算机视觉·cv·理论体系
技术支持者python,php4 天前
训练模型,物体识别(opencv)
人工智能·opencv·计算机视觉