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)
相关推荐
Byron Loong7 小时前
Python+OpenCV系列:AI看图识人、识车、识万物
人工智能·python·opencv
E___V___E10 小时前
opencv 项目--图像匹配
人工智能·opencv·计算机视觉
乌漆嘎嘎黑11 小时前
Hu矩原理 | cv2中基于Hu矩计算图像轮廓相似度差异的函数cv2.matchShapes【小白记笔记】
人工智能·opencv·计算机视觉·hu矩·轮廓相似度
Jack黄从零学c++12 小时前
opencv(c++)人脸检测
c++·opencv·计算机视觉
pchmi17 小时前
C# OpenCV机器视觉:图像旋转(让生活的角度更美好!)
opencv·c#·生活
YRr YRr1 天前
解决 OpenCV 与 FFmpeg 版本不兼容导致的编译错误
人工智能·opencv·ffmpeg
ኈ ቼ ዽ1 天前
opencv小练习(未完成版)
人工智能·opencv·计算机视觉
坐井观老天1 天前
如何在OpenCV中运行自定义OCR模型
opencv·计算机视觉·ocr
天天代码码天天1 天前
C# OnnxRuntime Gaze-LLE 凝视目标估计,通过利用冻结的DINOv2编码器的特征来简化注视目标估计,预测一个人在场景中看的位置。
人工智能·深度学习·神经网络·opencv·目标检测·计算机视觉·凝视目标估计