#Example code showing the use of slicing operators to copy regions #of one image into another. # #Jay Summet - Summet 2015 # # OpenCV 2.4.x, Python 2.7 import cv2 #Because the color channels are the last "dimension", 2D slicing will #also automatically copy all 3 color channels. # a = cv2.imread("blueSky.jpg", cv2.IMREAD_GRAYSCALE) b = cv2.imread("holes.jpg", cv2.IMREAD_GRAYSCALE) #Copy a region from B into A: # a[133:367, 416:605] = b[133:367, 416:605] #Copy the same region using variables... xPos = 416 width = 189 yPos = 133 height = 234 a[yPos:yPos+height, xPos:xPos+width] = b[yPos:yPos+height, xPos:xPos+width] #Copy it into a NEW position! newY = 0 newX = 0 a[newY:newY+height, newX:newX+width] = b[yPos:yPos+height, xPos:xPos+width] cv2.imshow("Copy Rectangle", a) cv2.waitKey(0)