Search code examples
pythonopencvimage-processingcomputer-visionpanoramas

python opencv panorama blacklines


I am working on panorama with Python OpenCV. Can someone show me how to get rid of the black lines in my final images? I am thinking of maybe I should first check for the color I.e. 0,0,0 before copying it to the atlas image, but I am not quite sure how to do that.

def warpTwoImages(img1, img2, H):
    '''warp img2 to img1 with homograph H'''
    h1,w1 = img1.shape[:2]
    h2,w2 = img2.shape[:2]
    pts1 = np.float32([[0,0],[0,h1],[w1,h1],[w1,0]]).reshape(-1,1,2)
    pts2 = np.float32([[0,0],[0,h2],[w2,h2],[w2,0]]).reshape(-1,1,2)
    pts2_ = cv2.perspectiveTransform(pts2, H)
    pts = np.concatenate((pts1, pts2_), axis=0)
    [xmin, ymin] = np.int32(pts.min(axis=0).ravel() - 0.5)
    [xmax, ymax] = np.int32(pts.max(axis=0).ravel() + 0.5)
    t = [-xmin,-ymin]
    Ht = np.array([[1,0,t[0]],[0,1,t[1]],[0,0,1]]) # translate

    result = cv2.warpPerspective(img2, Ht.dot(H), (xmax-xmin, ymax-ymin))
    result[t[1]:h1+t[1],t[0]:w1+t[0]] = img1
    return result

Click here to see my reult


Solution

  • This answer depends on warpPrespicteve function to work with RGBA. You can try to use the alpha channel of each image. Before wrapping convert each image to RGBA (See the code below) were the alpha channel will be 0 for the black lines and for all other pixels it will be 255.

    import cv2
    import numpy as np
    
    # Read img
    img = cv2.imread('i.jpg')
    
    # Create mask from all the black lines
    mask = np.zeros((img.shape[0],img.shape[1]),np.uint8)
    cv2.inRange(img,(0,0,0),(1,1,1),mask)
    mask[mask==0]=1
    mask[mask==255]=0
    mask = mask*255
    
    b_channel, g_channel, r_channel = cv2.split(img)
    
    # Create a new image with 4 channels the forth channel Aplha will give the opacity for each pixel
    newImage = cv2.merge((b_channel, g_channel, r_channel, mask))