黄山网站建设费用,建电影网站程序,云主机是什么,网站不收录的原因前言在这个项目中#xff0c;您将构建一个管道#xff0c;将几幅图像拼接成一个全景图。您还将捕获一组您自己的图像来报告最终的结果。步骤1 特征检测与描述本项目的第一步是对序列中的每幅图像分别进行特征检测。回想一下我们在这个类中介绍过的一些特征探测器#xff1a;…前言在这个项目中您将构建一个管道将几幅图像拼接成一个全景图。您还将捕获一组您自己的图像来报告最终的结果。步骤1 特征检测与描述本项目的第一步是对序列中的每幅图像分别进行特征检测。回想一下我们在这个类中介绍过的一些特征探测器哈里斯角、Blob探测器、SIFT探测器等。与检测步骤相结合就进行了特征描述。您的特性必须对某些转换是不变的。想想你将收集数据集的方式。你需要什么样的不变性探索在OpenCV中可用的特性描述符实现 SIFT、SURF、ORB等1.使用cv2.imread读取输入的图像。2.将图像转换为灰度例如使用cv2.cvtColor以进行特征提取。您也可以通过cv2.resize调整图像的大小例如在参考约塞米蒂序列中那样的480像素的高度以使以下步骤运行得更快。3.选择您的特征检测器。cv2是一个很好的开始。SIFT_creve(或cv2.xfeatures2d。SIFT_Creve在旧的OpenCV版本中)。4.使用检测和计算方法提取关键点和关键点特征。5.图1使用cv2.drawKeypoints可视化了检测到的关键点def flt_show(image, titlesift detects):plt.figure(figsize(10,10)) # Canvas 10x magnificationplt.title(title,fontsize20) # titleplt.axis(off) # Hidden axisplt.imshow(cv2.cvtColor(image,cv2.COLOR_BGR2RGB)) # print imageplt.show() # Display canvas# 1. Read the input images using cv2.imread.
def feature_detection_and_description(image_path):image cv2.imread(image_path)# RGB to Grayimage cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)# Create detectorsift_detecter sift cv2.SIFT_create()# Detection picturekp,des sift.detectAndCompute(image,None) # Keypoints and descriptors# Plot key pointsimg2 cv2.drawKeypoints(image,kp,None,(255,0,0),4)# showflt_show(img2)# Release resourcescv2.waitKey(0)cv2.destroyAllWindows()feature_detection_and_description(yosemite1.jpg)步骤2 特征匹配和图像对齐一旦计算出了关键点和特征代码就需要在连续的图像对之间建立良好的匹配。为此您需要选择一个距离函数和一个匹配算法。1.选择一个匹配算法和一个距离函数。强力匹配(cv2。你可以做这个工作因为你以后会摆脱糟糕的比赛如果您使用二进制特性如简短或ORB看看汉明距离函数(cv2。NORM_HAMMING)否则只需使用欧几里得距离(cv2。norm_l2 ).2.使用匹配方法从一对图像中计算匹配并提供它们的特征作为输入。选择一对有良好重叠的图像。实际上按照预定义的顺序例如从左到右捕捉图像进行全景拼接是很方便的这样相邻的图像总是有很好的重叠。3.使用原始的关键点位置和匹配以两个m×2数组点1和点2的形式构造对应这样点1[i]和点2[i]分别在第一和第二幅图像中存储匹配的位置。如果您正在使用建议的OpenCV功能请查看关键点和DMatch类的接口以了解如何操作返回对象。 4.现在从对应关系中估计图像之间的同源性变换。匹配项将会有很多离群值所以你需要选择一个对离群值很健壮的估计器比如RANSAC。cv2.findHomography对这一步很有用5.最后用模糊的同源性扭曲其中一幅图像 cv2.warpPerspective。6.图2显示了在所选对图像之间检测到的1249个匹配中的前10个匹配基于响应分数。该图是使用cv2.drawMatches创建的。让我们来看看图3和图4。为什么前者的一致性如此成功为什么在后一个数字中会有问题 def detectAndDescribe(image):# convert the image to grayscalegray cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# detect and extract features from the imagedescriptor cv2.SIFT_create()(kps, features) descriptor.detectAndCompute(image, None)# convert the keypoints from KeyPoint objects to NumPy# arrays
# kps np.float32([kp.pt for kp in kps])# return a tuple of keypoints and featuresreturn (kps, features)def matchKeypoints(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. Lowes ratio test)
# if len(m) 2 and m[0].distance m[1].distance * ratio:
# matches.append((m[0].trainIdx, m[0].queryIdx))bf cv2.BFMatcher(cv2.NORM_L2,crossCheckTrue)matches bf.match(featuresA,featuresB)goods sorted(matches,key lambda x:x.distance)# computing a homography requires at least 4 matchesif 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])ptsA np.float32([ kpsA[m.queryIdx].pt for m in goods ]).reshape(-1,1,2)ptsB np.float32([ kpsB[m.trainIdx].pt for m in goods ]).reshape(-1,1,2)# 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 pointreturn (matches, H, status)# otherwise, no homograpy could be computedreturn Nonedef drawMatches(imageA, imageB, kpsA, kpsB, matches, status):img1 imageAimg2 imageBif len(matches) 10:src_pts np.float32([ kpsA[m.queryIdx].pt for m in matches ]).reshape(-1,1,2)dst_pts np.float32([ kpsB[m.trainIdx].pt for m in matches ]).reshape(-1,1,2)M, mask cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)matchesMask mask.ravel().tolist()h,w,_ img1.shapepts np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)draw_params dict(matchColor (0,255,0), # draw matches in green colorsinglePointColor None,matchesMask matchesMask, # draw only inliersflags 2)img_matchers cv2.drawMatches(imageA,kpsA,imageB,kpsB,matches,None,**draw_params)# return the visualizationreturn img_matchersdef feature_matching_and_image_alignment(imageL, imageR, ratio0.75, reprojThresh4.0, showMatchesFalse):imageA, imageB imageR, imageL# local invariant descriptors from them(kpsA, featuresA) detectAndDescribe(imageA)(kpsB, featuresB) detectAndDescribe(imageB)# match features between the two imagesM matchKeypoints(kpsA, kpsB,featuresA, featuresB, ratio, reprojThresh)# if the match is None, then there arent enough matched# keypoints to create a panoramaif M is None:return None# otherwise, apply a perspective warp to stitch the images# together(matches, H, status) Mresult 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 visualizedif showMatches:vis drawMatches(imageA, imageB, kpsA, kpsB, matches,status)# return a tuple of the stitched image and the# visualizationreturn (result, vis)# return the stitched imagereturn resultresult, vis feature_matching_and_image_alignment(cv2.imread(yosemite1.jpg), cv2.imread(yosemite2.jpg), showMatchesTrue)
flt_show(vis)
flt_show(result)步骤3 图像背景去除现在您已经将原始图像1和图像2扭曲到图像1的平面上您需要将它们组合起来以产生无缝的结果。如果你简单地添加图像强度重叠的区域看起来会很不错但其他区域看起来会很暗如图3所示。另一个简单的解决方案是将一幅图像复制粘贴到另一幅图像之上。这还将在边界上创建工件如图5所示。减少伪影强度的一个解决方案是使用自适应权值来混合图像。靠近每幅图像边界的像素不如位于中心的像素可靠。那么如何将这些像素与与这些像素到相应图像边界的距离成比例的权重进行混合呢我们提供了get_tinghe_变换函数它取一个扭曲的RGB图像H×W×3并产生一个单通道距离函数H×W×1。您可以使用这个函数来计算结果为img1 *权重1img2*权重2。不要忘记将生成的图像规格化到0…255的范围。尝试使用np.最大值denom1.0以避免除以零。将您的图像转换回无符号的8位整数与img.astype“uint8”def get_distance_transform(img_rgb):Get distance to the closest background pixel for an RGB imageInput:img_rgb : np.array , HxWx3 RGB imageOutput:dist : np.array , HxWx1 distance imageeach pixel ’s intensity is proportional toits distance to the closest background pixelscaled to [0..255] for plotti# Threshold the image : any value above 0 maps into 255thresh cv2.threshold(img_rgb , 0 , 255 , cv2 . THRESH_BINARY )[1]# Collapse the color dimensionthresh thresh.any(axis 2)# Pad to make sure the border is treated as backgroundthresh np.pad(thresh, 1, modemaximum)# Get distance transformdist distance_transform_edt(thresh) [1: -1 , 1: -1]# HxW - HxWx1dist dist[: , : , None ]return dist / dist . max () * 255.0def getMaskResult(result, showFalse):mask get_distance_transform(result)locs np.where(mask0)xmin np.min(locs[1])xmax np.max(locs[1])ymin np.min(locs[0])ymax np.max(locs[0])result_mask result[ymin:ymax, xmin:xmax]if show:flt_show(result, before)flt_show(result_mask, after)return result_mask
result_mask getMaskResult(result, True)步骤4 缝合多张图像拼接n个图像的一个简单方法是从左到右对序列进行排序并逐步应用成对拼接如果拼接每一对相邻图像将得到n个−1的全景图。您可以一次又一次地对这些图像应用相同的拼接过程直到您只留下一个宽的全景图。您只需要在它上面添加一个for循环。imageList [cv2.imread(yosemite1.jpg), cv2.imread(yosemite2.jpg), cv2.imread(yosemite3.jpg),cv2.imread(yosemite4.jpg)]temp_image feature_matching_and_image_alignment(imageList[0], imageList[1])
temp_image getMaskResult(temp_image)
flt_show(temp_image1, titleStep 1)
for i in range(2, 4):temp_image feature_matching_and_image_alignment(temp_image, imageList[i])temp_image getMaskResult(temp_image)flt_show(temp_image, titleStep {}.format(i))步骤5 创建您自己的全景图并讨论设置和结果。请注意这一步将主要根据您提供的讨论进行分级例如只捕获一些图像并在不编写的情况下生成结果不会给您很多分数。为了获得一个好的全景在捕捉自己的图像时有几件事需要考虑。imageList [cv2.imread(1.png), cv2.imread(2.png), cv2.imread(3.png),cv2.imread(4.png), cv2.imread(5.png)]temp_image feature_matching_and_image_alignment(imageList[0], imageList[1])
temp_image getMaskResult(temp_image)
flt_show(temp_image, titleStep 1)
for i in range(2, 5):temp_image feature_matching_and_image_alignment(temp_image, imageList[i])temp_image getMaskResult(temp_image)flt_show(temp_image, titleStep {}.format(i))# # # #发表评论- 1。这已经在步骤4中完成了。- 2。数据集捕捉了沿河的建筑风景具有良好的视野和易于区分的特征。- 3。相邻的图片有很好的重叠。 4. 摄像机角度的变化容易引起同态现象。如果天气晴朗最好待在室外视野开阔位置好光线也明亮。只要两幅图像之间的特征明显摄像机是否自由移动并不重要。当三张以上的图片拼接在一起时由于黑色背景的原因图像与其他图像的拼接会有很多困难。背景剔除可以解决这个问题。感谢观看由于是作业的缘故文中可能有极少部分会出现语义不通顺的情况但对本文的目的没有什么影响。