Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Suppose you already have the contours BGR image, by using drawContours for example. Then you can combine your 2 images by Bitwise Operations (The answesr it taken basically from the python tutorials).

Just make sure first to convert your original grayscale image to BGR and then you can do something like this:

img1 = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)    
img2 = contours_img

# Create a mask of contours and create its inverse mask also
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of contours in original image
img1_bg = cv2.bitwise_and(img1, img1, mask = mask_inv)

# Take only region of contours from contours image.
img2_fg = cv2.bitwise_and(img2, img2, mask = mask)

# Put contours in image and modify the main image
dst = cv2.add(img1_bg,img2_fg)    
cv2.imshow('res',dst)