2016-08-01 144 views
0

我正在与Python OpenCV全景工作。有人能告诉我如何摆脱我最终图像中的黑线吗?我想也许我应该先检查颜色I.e. 0,0,0复制到地图集图像之前,但我不太确定如何做到这一点。蟒蛇opencv全景黑线

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

回答

0

这个答案取决于warpPrespicteve功能与RGBA工作。 您可以尝试使用每个图像的Alpha通道。 之前缠绕的每个图像转换为RGBA(请参见下面的代码)分别为α通道将是0为黑线和用于所有其它像素将是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))