Ask Your Question
-1

Inserting Logo in an image

asked 2020-06-09 09:02:13 -0600

Aastha gravatar image

updated 2020-06-09 10:34:05 -0600

supra56 gravatar image

I want to insert logo in an image. For that I tried below code:

import cv2
import numpy as np

img1 = cv2.imread('xyz.JPG')

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

rows,col,channel = img2.shape

roi = img1[0:rows,0:col]

img2g = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)

ret, mask = cv2.threshold(img2g,180,255,cv2.THRESH_BINARY_INV)

mask_inv = cv2.bitwise_not(mask)

img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

img2_fg = cv2.bitwise_and(img2,img2,mask=mask)

dst = cv2.add(img1_bg,img2_fg)

img1[0:rows,0:col] = dst

cv2.imshow('img',img1)

cv2.waitKey(0)

cv2.destroyAllWindows()

the code is working fine but what would be the roi if i want to insert my logo at any position other then top left corner?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-06-29 22:12:11 -0600

supra56 gravatar image

updated 2020-07-01 04:52:04 -0600

I solved problem. Used double for both numbers not unique. For instance for row is 450 and 450, but not 450 and 150. Otherwise, you will get an error. Look in line 29:

#!/usr/bin/python37
#OpenCV 4.3.0, Raspberry pi3B/+, 4b/4g/8g, Thonny 3.7.3
#Date: 29th June, 2020

import cv2
import numpy as np

img1 = cv2.imread('ralph.jpg')
img2 = cv2.imread('mainlogo.png')

# I want to put logo on top-left corner, So I create a ROI
rows,cols,channels = img2.shape

roi = img1[0:rows, 0:cols]

# Now create a mask of logo 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 logo in ROI
img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)

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

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg, img2_fg)
img1[450:rows+450, 450:cols+450] = dst

cv2.imshow('res', img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: image description

To get overlay and remove border line:

#!/usr/bin/env python35
#OpenCV 4.3.0, Raspberry pi3B/+, 4b/4g/8g, Thonny 3.7.3
#Date: 1st July, 2020

import cv2
import numpy as np

img1 = cv2.imread('ralph.jpg')
overlay_img1 = np.ones(img1.shape,np.uint8)*255

img2 = cv2.imread('mainlogo.png')
rows,cols,channels = img2.shape
overlay_img1[450:rows+450, 450:cols+450 ] = img2

img2gray = cv2.cvtColor(overlay_img1,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray,220,55,cv2.THRESH_BINARY_INV)
mask_inv = cv2.bitwise_not(mask)
temp1 = cv2.bitwise_and(img1,img1,mask = mask_inv)
temp2 = cv2.bitwise_and(overlay_img1,overlay_img1, mask = mask)

result = cv2.add(temp1,temp2)
cv2.imshow("Result",result)
cv2.imwrite("Result.jpg",result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

image description

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-06-09 09:02:13 -0600

Seen: 6,314 times

Last updated: Jul 01 '20