# Example showing how to add/subtract/multiply image values together # using numpy array math (not per pixel iteration...) # # We use a higher bit depth format to avoid underflow/overflow) # and then convert back to an 8bit image. # # Python 2.7, OpenCV 2.4.X # # Jay Summet - Summer 2015 # import cv2 import numpy as np def scaleTo8Bit(image, displayMin = None, displayMax=None ): if displayMin == None: displayMin = np.min(image) if displayMax == None: displayMax = np.max(image) np.clip(image, displayMin, displayMax, out=image) image = image - displayMin cf = 255. / (displayMax - displayMin) imageOut = ( cf * image).astype(np.uint8) return imageOut a = cv2.imread("a.jpg") b = cv2.imread("b.jpg") #Doing math with uint8 images will suffer from overflow and underflow! #so we change the data type to int32 a = np.int32(a) b = np.int32(b) c = scaleTo8Bit(a) cv2.imshow("A", c) cv2.waitKey(0) c = scaleTo8Bit(b) cv2.imshow("B", c) #Pure subtraction: c = a-b #But we must convert back to uint8 if we want to display correctly! c = scaleTo8Bit(c) cv2.imshow("pure subtract", c) c = a+b c = scaleTo8Bit(c) cv2.imshow("add", c) c = a+b/2 c = scaleTo8Bit(c) #cv2.imshow("avg", c) c = np.minimum(a,b) c = scaleTo8Bit(c) cv2.imshow("min", c) c = np.maximum(a,b) c = scaleTo8Bit(c) cv2.imshow("max", c) ONE = np.ones( a.shape) ONE = ONE * 255 c = ONE - (ONE-a ) * (ONE-b) c = scaleTo8Bit(c) cv2.imshow("screen",c) cv2.waitKey(0)