【发布时间】:2021-10-04 11:56:56
【问题描述】:
我正在拼接多个图像。在拼接两个图像时,它在拼接之间显示黑色虚线,如下所示。
有谁知道我如何删除或摆脱这条黑色虚线?
拼接代码的主要部分,它拼接两个图像并使用前一个拼接图像的结果调用下一个图像,直到所有图像都结束:
detector = cv2.xfeatures2d.SURF_create(400)
gray1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
ret1, mask1 = cv2.threshold(gray1,1,255,cv2.THRESH_BINARY)
kp1, descriptors1 = detector.detectAndCompute(gray1,mask1)
gray2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)
ret2, mask2 = cv2.threshold(gray2,1,255,cv2.THRESH_BINARY)
kp2, descriptors2 = detector.detectAndCompute(gray2,mask2)
keypoints1Im = cv2.drawKeypoints(image1, kp1, outImage = cv2.DRAW_MATCHES_FLAGS_DEFAULT, color=(0,0,255))
util.display("KEYPOINTS",keypoints1Im)
keypoints2Im = cv2.drawKeypoints(image2, kp2, outImage = cv2.DRAW_MATCHES_FLAGS_DEFAULT, color=(0,0,255))
util.display("KEYPOINTS",keypoints2Im)
matcher = cv2.BFMatcher()
matches = matcher.knnMatch(descriptors2,descriptors1, k=2)
good = []
for m, n in matches:
if m.distance < 0.55 * n.distance:
good.append(m)
print (str(len(good)) + " Matches were Found")
if len(good) <= 10:
return image1
matches = copy.copy(good)
matchDrawing = util.drawMatches(gray2,kp2,gray1,kp1,matches)
util.display("matches",matchDrawing)
src_pts = np.float32([ kp2[m.queryIdx].pt for m in matches ]).reshape(-1,1,2)
dst_pts = np.float32([ kp1[m.trainIdx].pt for m in matches ]).reshape(-1,1,2)
A = cv2.estimateRigidTransform(src_pts,dst_pts,fullAffine=False)
if A is None:
HomogResult = cv2.findHomography(src_pts,dst_pts,method=cv2.RANSAC)
H = HomogResult[0]
height1,width1 = image1.shape[:2]
height2,width2 = image2.shape[:2]
corners1 = np.float32(([0,0],[0,height1],[width1,height1],[width1,0]))
corners2 = np.float32(([0,0],[0,height2],[width2,height2],[width2,0]))
warpedCorners2 = np.zeros((4,2))
for i in range(0,4):
cornerX = corners2[i,0]
cornerY = corners2[i,1]
if A is not None: #check if we're working with affine transform or perspective transform
warpedCorners2[i,0] = A[0,0]*cornerX + A[0,1]*cornerY + A[0,2]
warpedCorners2[i,1] = A[1,0]*cornerX + A[1,1]*cornerY + A[1,2]
else:
warpedCorners2[i,0] = (H[0,0]*cornerX + H[0,1]*cornerY + H[0,2])/(H[2,0]*cornerX + H[2,1]*cornerY + H[2,2])
warpedCorners2[i,1] = (H[1,0]*cornerX + H[1,1]*cornerY + H[1,2])/(H[2,0]*cornerX + H[2,1]*cornerY + H[2,2])
allCorners = np.concatenate((corners1, warpedCorners2), axis=0)
[xMin, yMin] = np.int32(allCorners.min(axis=0).ravel() - 0.5)
[xMax, yMax] = np.int32(allCorners.max(axis=0).ravel() + 0.5)
translation = np.float32(([1,0,-1*xMin],[0,1,-1*yMin],[0,0,1]))
warpedResImg = cv2.warpPerspective(image1, translation, (xMax-xMin, yMax-yMin))
if A is None:
fullTransformation = np.dot(translation,H) #again, images must be translated to be 100% visible in new canvas
warpedImage2 = cv2.warpPerspective(image2, fullTransformation, (xMax-xMin, yMax-yMin))
else:
warpedImageTemp = cv2.warpPerspective(image2, translation, (xMax-xMin, yMax-yMin))
warpedImage2 = cv2.warpAffine(warpedImageTemp, A, (xMax-xMin, yMax-yMin))
result = np.where(warpedImage2 != 0, warpedImage2, warpedResImg)
请帮帮我。谢谢。
编辑:
输入图片1(调整大小)
输入图像2(调整大小)
结果(调整大小)
更新:
@fmw42 anwser 之后的结果:
【问题讨论】:
-
作为猜测,我怀疑它在你的最后一行
result = np.where(warpedImage2 != 0, warpedImage2, warpedResImg)中是“warpedImage2 != 0”。您正在从边缘的二元决策中获得楼梯混叠。两侧的边缘很可能会使用插值中的黑色进行抗锯齿处理,因此它们有一些黑色与实际颜色混合。你可以尝试一些测试。使用最近邻插值进行 warpAffine,看看在这些接缝处会发生什么。您也可以尝试使用“warpedImage2 -
@fmw42 感谢您的回复。我不是专业人士,因此无法正确理解您的评论。但我试过
warpedImage2 = cv2.warpAffine(warpedImageTemp, A, (xMax-xMin, yMax-yMin),cv2.INTER_NEAREST)和ret1, mask1 = cv2.threshold(gray1,120,255,cv2.THRESH_BINARY)仍然没有删除黑线。如果可能,请让我知道我需要更改和测试什么。 -
要尝试的一件事是 cv2.warpAffine() 中的不同标志(插值)。见docs.opencv.org/4.1.1/da/d54/… 和docs.opencv.org/4.1.1/da/d54/…
-
要尝试的另一件事是将
result = np.where(warpedImage2 != 0, warpedImage2, warpedResImg)中的warpedImage2 != 0更改为warpedImage2 <T,在其中将 T 设置为不同的值。从 32 岁开始。看看会发生什么。然后加倍或减半,看看会发生什么。 -
@ fmw42 with T=64 及以上结果图像如问题所示。其他更改没有得到正确的结果。
标签: python opencv image-processing computer-vision homography