Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การเพิ่มและการผสมรูปภาพโดยใช้ OpenCv ใน Python


เรารู้ว่าเมื่อเราแก้ปัญหาใดๆ ที่เกี่ยวกับรูปภาพ เราต้องใช้เมทริกซ์ เนื้อหาเมทริกซ์จะแตกต่างกันไปตามประเภทของภาพ ไม่ว่าจะเป็นภาพไบนารี (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()

ผลลัพธ์

การเพิ่มและการผสมรูปภาพโดยใช้ OpenCv ใน Python

การผสมสองภาพ

ฟังก์ชัน 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()

ผลลัพธ์

การเพิ่มและการผสมรูปภาพโดยใช้ OpenCv ใน Python