Ask Your Question
1

How to remove the black border around rotated & masked image in result? OpenCv Python

asked 2018-06-15 04:27:07 -0600

subashbasnyat gravatar image

updated 2018-06-28 04:20:37 -0600

Hi,

How do I remove the black border around mainlogo.png in Result image?

import cv2
import numpy as np
import imutils

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

img2 = cv2.imread('mainlogo.png')
img2 = imutils.rotate_bound(img2, 10)
img2[np.where((img2==[0,0,0]).all(axis=2))] = [255,255,255]            

rows,cols,channels = img2.shape

overlay_img1[0:rows, 0:cols ] = img2

img2gray = cv2.cvtColor(overlay_img1,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 220, 255, 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)
cv2.imshow('Temp2', temp2)
result = cv2.add(temp1,temp2)
cv2.imshow("Result",result)
cv2.imwrite("Result.jpg", result)
cv2.waitKey(0)    
cv2.destroyAllWindows()

ralph.jpg image description

mainlogo.png image description

Result: image description

edit retag flag offensive close merge delete

Comments

can you try to debug it, by adding an imshow() after each processing step ?

(i'm almost sure, the artefacts come already from the imutils.rotate() step)

berak gravatar imageberak ( 2018-06-28 07:26:24 -0600 )edit

Did you manage to solve this problem with the black border?

KostasTsig gravatar imageKostasTsig ( 2018-11-14 15:03:55 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2019-03-04 12:19:57 -0600

supra56 gravatar image

Here is code:

#!/usr/bin/env python35
#OpenCV 4.0.1, Raspberry pi3B/+, IDE 3.5.
#Date: 4th March, 2019

import cv2
import numpy as np

img1 = cv2.imread('ralph1.jpg')
overlay_img1 = np.ones(img1.shape,np.uint8)*255
img2 = cv2.imread('mainlogo.png')
rows,cols,channels = img2.shape
overlay_img1[0:rows, 0:cols ] = 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)
cv2.imshow('Temp2', temp2)
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

2 followers

Stats

Asked: 2018-06-15 04:18:05 -0600

Seen: 11,472 times

Last updated: Mar 04 '19