- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
] 2
所以如下图所示,我在图像上检测到了关键点,但是包裹透视后的输出图像忽略了左侧的第一张图像,不知道为什么!
import numpy as np
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, imageA,imageB, ratio=0.75, reprojThresh=10.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
#print(M)
#print(matches)
#print(H)
#print(status)
#cv2.imwrite('intermediate.jpg',matches)
result = cv2.warpPerspective(imageA, H,
(imageA.shape[1] + imageB.shape[1], imageA.shape[0]))
result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
#cv2.imshow('intermediate',result)
# 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
#SIFT Algorithm
descriptor = cv2.xfeatures2d.SIFT_create()
#SURF Algorithm
#descriptor = cv2.xfeatures2d.SURF_create()# 400 is hesian threshold, optimum values should be around 300-500
#upright SURF: faster and can be used for panorama stiching i.e our case.
#descriptor.upright = True
print(descriptor.descriptorSize())
(kps, features) = descriptor.detectAndCompute(image, None)
print(len(kps),features.shape)
# 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
#print("features",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))
print(len(matches))
# 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
最佳答案
我自己也有这个问题。如果我没记错的话,您使用的是 this博客作为引用。
问题是 warpPerspective
关于这条线:
result = cv2.warpPerspective(imageA, H,
(imageA.shape[1] + imageB.shape[1], imageA.shape[0]))
result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
.shape[0]
表示的宽度和高度替换像素值来将 imageA 拼接到 imageB 上。和
.shape[1]
.我在 C++ 中解决了这个问题,因此没有要显示的 python 代码,但可以让你了解必须做的事情。
Mat Htr = Mat::eye(3,3,CV_64F);
if (min_x < 0){
max_x = image2.size().width - min_x;
Htr.at<double>(0,2)= -min_x;
}
if (min_y < 0){
max_y = image2.size().height - min_y;
Htr.at<double>(1,2)= -min_y;
}
perspectiveTransform(vector<Point2f> fourPointImage1, vector<Point2f> image1dst, Htr*homography);
perspectiveTransform(vector<Point2f> fourPointImage2, vector<Point2f> image2dst, Htr);
image1dst
获取最小值和最大值四个角和iamge2dst
四个角。 image1dst
的最小值和最大值和 iamge2dst
并使用创建一个新的blank image
正确大小以保存最终拼接的图像。 translation
需要调整每个图像的四个角以确保将它们移动到 blank image
的虚拟空间中warpPerspective(image1, blankImage, (translation*homography),result.size(), INTER_LINEAR,BORDER_CONSTANT,(0));
warpPerspective(image2, image2Updated, translation, result.size(), INTER_LINEAR, BORDER_CONSTANT, (0));
关于image - 关键点检测与图像拼接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46176380/
我正在检测图像中的 SURF 特征,然后将它们写入 yml 文件。然后我想再次从 yml 文件加载特征以尝试检测对象,但目前我无法加载关键点以将它们绘制在图像上。 我是这样写要点的: cv::File
我正在学习 OpenCV,目前我正在尝试了解存储在 KeyPoint 中的基础数据,以便我可以更好地将这些数据用于我正在处理的应用程序。 到目前为止,我已经浏览了这两个页面: http://docs.
我想了解更多关于KeyPoints的知识,谁能告诉我什么是 关键点::角度 在 OpenCV 文档中提到角度是计算关键点的方向(如果不适用则为 -1)。我无法想象那是什么。那么谁能告诉我这是什么意思或
我有 3D 多边形:实体(第一种情况)和平面(第二种情况)。如何确定主控点(key point)?例如:圆柱体的主点是其盖子的中心。 我不能使用凸包算法,因为主点可以是船体的一部分,而且我不能用平面
我想在opencv中实现一揽子单词。在检测器之后-> detect(img,keypoint);当我想使用keypoint.clear()清理关键点时,会检测到关键点;或者当函数想要返回时,将出现以下
我在 python 中有以下代码 import cv2 import numpy as np def save_keypoints(image_path, type_image): img =
我想要代码根据 SIFT 关键点匹配两张图片。? 我有以下用于 SIFT 的代码 public static Image siftFunction(Bitmap sourceBitmap) {
我正在处理生物信息学的任务,需要从一些细胞图像中提取一些特征。 我使用 SIFT 算法提取图像内部的关键点,如图所示。 正如您在图片中也看到的(红色圈出),一些关键点是异常值,我不想计算它们的任何特征
在搜索了这个(1、2、3、4)的答案后,我仍然对两者感到困惑 如果可能的话 如果是,怎么做 我希望能够从一个 .xml 文件中读取,该文件有一个包含 cv::Mat 的 Point2f keypoin
我尝试比较两个不同图像中的两个点,所以我想将坐标转换为关键点,以便稍后用它们计算描述和匹配结果。 我找到了方法: keypoint = cv2.KeyPoint(x, y, 0) vec = [k
我是一名 Asp.net 开发人员,但对 Asp.net Identity 框架非常陌生。我一直在研究示例应用程序并遵循了一些关于身份的教程,但我仍然无法完全掌握这个概念。我对 Asp.net 成员(
我是一名优秀的程序员,十分优秀!