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)
相关推荐
AI视觉网奇2 小时前
python 统计相同像素值个数
python·opencv·计算机视觉
hunter2062066 小时前
用opencv生成视频流,然后用rtsp进行拉流显示
人工智能·python·opencv
冰万森15 小时前
【图像处理】——掩码
python·opencv·计算机视觉
Antonio91516 小时前
【opencv】第10章 角点检测
人工智能·opencv·计算机视觉
大哥喝阔落19 小时前
图片专栏——曝光度调整相关
人工智能·python·opencv
鸭鸭鸭进京赶烤21 小时前
OpenAI秘密重塑机器人军团: 实体AGI的崛起!
人工智能·opencv·机器学习·ai·机器人·agi·机器翻译引擎
山川而川-R1 天前
ubuntu电脑调用摄像头拍摄照片
人工智能·opencv·计算机视觉
Antonio9152 天前
【opencv】第9章 直方图与匹配
人工智能·opencv·计算机视觉
油泼辣子多加2 天前
【计算机视觉】人脸识别
人工智能·opencv·计算机视觉
湫ccc2 天前
《Opencv》图像的透视变换--处理发票
人工智能·opencv·计算机视觉