Sunday, October 11, 2015

Python + OpenCV: Arithmetic Operations on Images - add(), addWeighted() and subtract()



import sys
import cv2
import numpy
import urllib

print("Python version: \n" + sys.version)
print("cv2 version: " + cv2.__version__)
print("numpy version: " + numpy.__version__)
print("urllib version: " + urllib.__version__)

#img1 and img2 must be in same size
img1 = cv2.imread('Raspberry_Pi_Logo.png', 1)
img2 = cv2.imread('opencv-logo-white.png', 1)
imgAdd = cv2.add(img1, img2)
imgAddWeighted = cv2.addWeighted(img1, 0.7, img2, 0.3, 0)
imgSub = cv2.subtract(img1, img2)

cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.imshow('imgAdd',imgAdd)
cv2.imshow('imgAddWeighted',imgAddWeighted)
cv2.imshow('imgSub',imgSub)
cv2.waitKey(0)
cv2.destroyAllWindow()



Reference: http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.html

No comments: