【发布时间】:2018-11-09 04:22:37
【问题描述】:
我正在尝试将具有重叠区域的图像拼接在一起。 图像被排序,每个图像与前一个图像有重叠区域。例如:
我已经尝试了https://www.pyimagesearch.com/2016/01/11/opencv-panorama-stitching/ 的代码,我对其稍作更改并使用了倾斜图像,但最终结果 (https://imgur.com/a/B2d2VBL) 与预期不符。
问题是否来自右侧为黑色的第 5 张图像?不知道为什么添加黑色以及如何避免它。
任何人都知道我如何修复代码以在我添加越来越多的图像时不扭曲图像?欢迎更好的代码示例供我使用。
~~~~~~~~ 编辑~~~~~~~ 正如丹在 cmets 中指出的那样,我使用了错误的工具(warpPerspective)来完成这项工作。我真正在寻找的是一种方法来找到两个图像中匹配的关键点,将其转换为每个图像中正确的 Y,这样我就可以剪切图像然后相应地缝合它们。
所以现在关于如何获取匹配的关键点并将其转换为 Y 坐标的问题可能有点简单。
请忽略代码,因为它只是我从哪里开始的一个例子,它只是在这一点上具有误导性。
下面的代码示例输入包含图像的目录路径 ["0.png", "1.png", "2.png", "3.png"]
from PIL import Image
import numpy as np
import imutils
import cv2
# from panorama import Stitcher
import argparse
import imutils
import cv2
class Stitcher:
def __init__(self):
# determine if we are using OpenCV v3.X
self.isv3 = imutils.is_cv3()
def stitch(self, images, ratio=0.75, reprojThresh=4.0,
showMatches=False):
# unpack the images, then detect keypoints and extract
# local invariant descriptors from them
(imageB, imageA) = images
(kpsA, featuresA) = self.detectAndDescribe(imageA)
(kpsB, featuresB) = self.detectAndDescribe(imageB)
# match features between the two images
M = self.matchKeypoints(kpsA, kpsB,
featuresA, featuresB, ratio, reprojThresh)
# if the match is None, then there aren't enough matched
# keypoints to create a panorama
if M is None:
return None
# otherwise, apply a perspective warp to stitch the images
# together
(matches, H, status) = M
result = cv2.warpPerspective(imageA, H,
(imageA.shape[1] + imageB.shape[1], imageA.shape[0]))
result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
# check to see if the keypoint matches should be visualized
if showMatches:
vis = self.drawMatches(imageA, imageB, kpsA, kpsB, matches,
status)
# return a tuple of the stitched image and the
# visualization
return (result, vis)
# return the stitched image
return result
def detectAndDescribe(self, image):
# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# check to see if we are using OpenCV 3.X
if self.isv3:
# detect and extract features from the image
descriptor = cv2.xfeatures2d.SIFT_create()
(kps, features) = descriptor.detectAndCompute(image, None)
# otherwise, we are using OpenCV 2.4.X
else:
# detect keypoints in the image
detector = cv2.FeatureDetector_create("SIFT")
kps = detector.detect(gray)
# extract features from the image
extractor = cv2.DescriptorExtractor_create("SIFT")
(kps, features) = extractor.compute(gray, kps)
# convert the keypoints from KeyPoint objects to NumPy
# arrays
kps = np.float32([kp.pt for kp in kps])
# return a tuple of keypoints and features
return (kps, features)
def matchKeypoints(self, kpsA, kpsB, featuresA, featuresB,
ratio, reprojThresh):
# compute the raw matches and initialize the list of actual
# matches
matcher = cv2.DescriptorMatcher_create("BruteForce")
rawMatches = matcher.knnMatch(featuresA, featuresB, 2)
matches = []
# loop over the raw matches
for m in rawMatches:
# ensure the distance is within a certain ratio of each
# other (i.e. Lowe's ratio test)
if len(m) == 2 and m[0].distance < m[1].distance * ratio:
matches.append((m[0].trainIdx, m[0].queryIdx))
# computing a homography requires at least 4 matches
if len(matches) > 4:
# construct the two sets of points
ptsA = np.float32([kpsA[i] for (_, i) in matches])
ptsB = np.float32([kpsB[i] for (i, _) in matches])
# compute the homography between the two sets of points
(H, status) = cv2.findHomography(ptsA, ptsB, cv2.RANSAC,
reprojThresh)
# return the matches along with the homograpy matrix
# and status of each matched point
return (matches, H, status)
# otherwise, no homograpy could be computed
return None
def drawMatches(self, imageA, imageB, kpsA, kpsB, matches, status):
# initialize the output visualization image
(hA, wA) = imageA.shape[:2]
(hB, wB) = imageB.shape[:2]
vis = np.zeros((max(hA, hB), wA + wB, 3), dtype="uint8")
vis[0:hA, 0:wA] = imageA
vis[0:hB, wA:] = imageB
# loop over the matches
for ((trainIdx, queryIdx), s) in zip(matches, status):
# only process the match if the keypoint was successfully
# matched
if s == 1:
# draw the match
ptA = (int(kpsA[queryIdx][0]), int(kpsA[queryIdx][1]))
ptB = (int(kpsB[trainIdx][0]) + wA, int(kpsB[trainIdx][1]))
cv2.line(vis, ptA, ptB, (0, 255, 0), 1)
# return the visualization
return vis
if __name__ == '__main__':
images_folder = sys.argv[1]
images = ["0.png", "1.png", "2.png", "3.png"]
imageA = cv2.imread(images_folder+images[0])
imageB = cv2.imread(images_folder+images[1])
# stitch the images together to create a panorama
stitcher = Stitcher()
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=True)
count = 0
imgRGB=cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
img = Image.fromarray(imgRGB)
current_stiched_image = images_folder + "lol10{}.png".format(count)
img.save(current_stiched_image)
for image in images[2:]:
count+=1
print("image: {}".format(image))
print("count: {}".format(count))
print("current_stiched_image: {}".format(current_stiched_image))
imageA1 = cv2.imread(current_stiched_image)
imageB1 = cv2.imread(images_folder + image)
(result, vis) = stitcher.stitch([imageA1, imageB1], showMatches=True)
imgRGB=cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
img = Image.fromarray(imgRGB)
current_stiched_image = images_folder + "lol10{}.png".format(count)
print("new current_stiched_image: {}".format(current_stiched_image))
img.save(current_stiched_image)
【问题讨论】:
-
问题标题似乎也有点奇怪——重叠不是拼接工作的先决条件吗? |有了这样的输入,
warpPerspective似乎有点适得其反。 -
@DanMašek 是的,我同意,因为我不熟悉使用 cv2 我使用了一个我发现的示例,在今天深入研究之后,我确实发现使用 warpPerspective 绝对不是正确的方法,因为它是在全景等情况下与拼接图像更相关。我想我真正需要的只是找到具有最大匹配点的区域并应用剪切和缝合。但由于我是新手,我不确定如何找到最佳匹配的关键点,以及如何将该输出转换为 Y 坐标(切割位置)。