在 Python 中使用 OpenCv 進行影像的新增和混合


眾所周知,當我們解決任何與影像相關的問題時,我們都必須使用一個矩陣。矩陣內容將根據影像型別而有所不同 - 它可能是二進位制影像(0,1)、灰度影像(0-255)或 RGB 影像(255 255 255)。因此,如果我們要新增兩幅影像,則意味著非常簡單,我們必須新增各自的兩個矩陣。

在 OpenCV 庫中,我們有一個函式 cv2.add() 來新增影像。但對於影像新增,兩個影像的大小應該是相同的。

兩張影像的新增

import cv2
# Readingour Image1
my_firstpic = cv2.imread('C:/Users/TP/Pictures/west bengal/bishnupur/mqdefaultILPT6GSR.jpg', 1)
cv2.imshow('image', my_firstpic)
# Readingour Image2
my_secpic = cv2.imread('C:/Users/Satyajit/Pictures/west bengal/bishnupur/pp.jpg', 1)
img = cv2.add(my_firstpic,my_secpic)
cv2.waitKey(0)
cv2.distroyAllWindows()

輸出

Adding Images

融合兩幅影像

cv2.addWeighted() 函式用於融合兩張影像。

示例程式碼

import cv2
# Read our Image1
My_first = cv2.imread('C:/Users/TP/Pictures/west bengal/bishnupur/mqdefaultILPT6GSR.jpg', 1)
# Reading ourImage2
My_second = cv2.imread('C:/Users/TP/Pictures/west bengal/bishnupur/pp.jpg', 1)
# Blending the images with 0.3 and 0.7
My_img = cv2.addWeighted(My_first, 0.3, My_second, 0.7, 0)
# Show the image
cv2.imshow('image', My_img)
# Wait for a key
cv2.waitKey(0)
# Destroy all the window open
cv2.distroyAllWindows()

輸出

Blending Images

更新時間: 30-Jul-2019

381 次瀏覽

開啟你的 職業生涯

完成課程以獲取認證

開始
廣告