7

I am attempting to paint a logo over the top of a landscape image in OpenCV python. I have found answers that can 'blend'/watermark images but I don't want to make the logo transparent, I simply want to display the logo on top of the landscape image and keep the logo opacity (or lack thereof).

I have tried both cv2.add() and cv2.bitwise_and() but these are not copying the logo across.

src = cv2.imread('../images/pan1.jpg')
logo = cv2.imread('../images/logo.png')  # the background is black which is good because I dont want the black parts

# Ensure the logo image is the same size as the landscape image
logo_resized = np.zeros(src.shape, dtype="uint8")
# Place the logo at the bottom right
logo_resized[ src.shape[0] - logo.shape[0] : src.shape[0], src.shape[1] - logo.shape[1] : src.shape[1]] = logo

# Convert the logo to single channel
logo_gray = cv2.cvtColor(logo_resized, cv2.COLOR_BGR2GRAY)
# Create a mask of the logo image
ret, mask = cv2.threshold(logo_gray, 1, 255, cv2.THRESH_BINARY)

# Combine the landscape and the logo images but use a mask so the black parts of logo don't get copied across
# combined = cv2.bitwise_and(logo_resized, logo_resized, mask=mask)
combined = cv2.add(src, logo_resized, mask=mask)

My result:

enter image description here enter image description here enter image description here

sazr
  • 24,984
  • 66
  • 194
  • 362
  • 1
    Are you wedded to opencv for some reason? This looks trivial to do with basic numpy operations – en_Knight Jul 16 '18 at 15:22
  • @en_Knight I would prefer to use OpenCV because I prototype in Python then implement in C++. But numpy could work aswell. – sazr Jul 16 '18 at 15:24
  • can you provide both original images? the logo and the landscape. Actually you just need to set to 0 everything that is not black in the logo in the image and then add it with the logo – api55 Jul 16 '18 at 15:38
  • You want the `cv::Mat::copyTo` function in python which is explained perfectly in [this post](https://stackoverflow.com/a/41573727/2286337). – zindarod Jul 16 '18 at 15:40

1 Answers1

12

I presume the following is what you had in mind.

Code:

room = cv2.imread('room.JPG' )
logo = cv2.imread('logo.JPG' )

#--- Resizing the logo to the shape of room image ---
logo = cv2.resize(logo, (room.shape[1], room.shape[0]))

#--- Apply Otsu threshold to blue channel of the logo image ---
ret, logo_mask = cv2.threshold(logo[:,:,0], 0, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)
cv2.imshow('logo_mask', logo_mask)

room2 = room.copy() 

#--- Copy pixel values of logo image to room image wherever the mask is white ---
room2[np.where(logo_mask == 255)] = logo[np.where(logo_mask == 255)]

cv2.imshow('room_result.JPG', room2)
cv2.waitKey()
cv2.destroyAllWindows()

Result:

Final result:

enter image description here

The following is the mask obtained from applying Otsu threshold on the blue channel of the logo:

enter image description here

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87