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)
相关推荐
阿linlin27 分钟前
OpenCV--图像预处理学习01
opencv·学习·计算机视觉
程序员Linc2 小时前
边缘检测技术现状初探2:多尺度与形态学方法
计算机视觉·边缘检测·形态学
jndingxin4 小时前
OpenCV 图形API(10)用于执行标量除以矩阵的逐元素操作函数divRC()
人工智能·opencv
weixin_442424035 小时前
Opencv计算机视觉编程攻略-第七节 提取直线、轮廓和区域
人工智能·opencv·计算机视觉
闲人编程6 小时前
形态学操作(腐蚀/膨胀/开闭运算)
python·opencv·图像识别
鸿蒙布道师19 小时前
OpenAI战略转向:开源推理模型背后的行业博弈与技术趋势
人工智能·深度学习·神经网络·opencv·自然语言处理·openai·deepseek
jndingxin1 天前
OpenCV 图形API(或称G-API)(1)
人工智能·opencv·计算机视觉
合合技术团队1 天前
实测对比|法国 AI 独角兽公司发布的“最强 OCR”,实测效果如何?
大数据·人工智能·图像识别
契合qht53_shine1 天前
OpenCV 从入门到精通(day_03)
人工智能·opencv·计算机视觉
知舟不叙1 天前
OpenCV的基础操作
人工智能·opencv·计算机视觉