我总结图像处理网站pyimagesearch上的图像拼接代码,并附有详细的解释。
文章目录
1. OpenCV panorama stitching,2016年1月11日
GitHub中一样的代码: samggggflynn/panorama-stitching、karanvivekbhargava/PanoramaStiching、SIFT-master
内容:
使用Python和OpenCV进行图像拼接和全景图构建。给定两张图片,将它们“缝合”在一起形成一个简单的全景图。
介绍了全景图像拼接的4个步骤:
步骤1:从两个输入图像中检测关键点(DoG, Harris等)和提取局部不变描述符(SIFT, SURF等)。
步骤2:匹配两个图像之间的描述符。
步骤3:使用RANSAC算法估计我们匹配的特征向量的单应矩阵。
步骤4:使用从步骤3得到的单应矩阵应用翘曲变换。
代码:在panorama.py中封装上述四个步骤,并且定义了一个用于构造全景图的Stitcher类。在stitch.py中,我们调用panorama.py中的Stitcher类,完成拼接。
panorama.py
# import the neccessary packages
import numpy as np
import imutils
import cv2
class Stitcher:
def __init__(self):
# define if we are using OpenCV v3.x
self.isv3 = imutils.is_cv3(or_better=True)
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
stitch.py
# run code use : python stitch.py --first images/img9.jpg --second iamges/img10.jpg
# import the necessary packages
from panorama import Stitcher
import argparse
import imutils
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--first", required=True, help="path to the first image")
ap.add_argument("-s", "--second", required=True, help="path to the second image")
args = vars(ap.parse_args())
# load the two images and resize them to have a width of 400 pixels
# (for faster processing)
imageA = cv2.imread(args["first"])
imageB = cv2.imread(args["second"])
imageA = imutils.resize(imageA, width=400)
imageB = imutils.resize(imageB, width=400)
# stitch the images together to create a panorama
stitcher = Stitcher()
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=True)
# to write the images
cv2.imwrite("Matched_points_mural.jpg", vis)
cv2.imwrite("Panorama_image_mural.jpg", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# show the images
cv2.imshow("Image A", imageA)
cv2.imshow("Image B", imageB)
cv2.imshow("Keypoint Matches", vis)
cv2.imshow("Result", result)
cv2.waitKey(0)
运行代码:
python stitch.py --first images/bryce_left_01.png –second images/bryce_right_01.png
2. Image Stitcing with OpenCV and Python, 2018年12月17日==*==
网址:https://www.pyimagesearch.com/2018/12/17/image-stitching-with-opencv-and-python/
中文翻译版:https://blog.csdn.net/learning_tortosie/article/details/85083825?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162683446416780255241191%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162683446416780255241191&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-4-85083825.first_rank_v2_pc_rank_v29&utm_term=cv2.createStitcher%28%29+%E5%92%8C+cv2.Stitcher_create%28%29+&spm=1018.2226.3001.4187
内容:
学习如何使用Python、OpenCV和cv2.createStitcher以及 cv2.Stitcher_create来执行图像拼接。使用这个代码,可以将多个图像拼接在一起,创建一个拼接图像的全景。
图像拼接算法步骤图:https://www.pyimagesearch.com/wp-content/uploads/2018/12/image_stitching_opencv_pipeline.png
代码参考论文:Automatic Panorama Image Stitching with Invariant Features,论文地址:http://matthewalunbrown.com/papers/ijcv2007.pdf
在GitHub上实现这篇论文的代码如下:avinashk442/Panoramic-Image-Stitching-using-invariant-features
网址:https://github.com/avinashk442/Panoramic-Image-Stitching-using-invariant-features
论文介绍:
不同于以往的图像拼接算法对输入图像的顺序敏感,Automatic Panorama Image Stitching with Invariant Features方法鲁棒性更强,对以下情况不敏感:
· 图片的顺序
· 方向的图片
· 光照变化
· 噪声图像不是全景图的一部分
代码:
image_stitching_simple.py
# run code
# python image_stitching_simple.py --images images/scottsdale --output output.png
# import the necessary packages
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", type=str, required=True,
help="path to input directory of images to stitch")
ap.add_argument("-o", "--output", type=str, required=True,
help="path to the output image")
args = vars(ap.parse_args())
# grab the paths to the input images and initialize our images list
print("[INFO] loading images...")
imagePaths =