• 修复一份SFN代码(可运行)


    1. 环境:python,cv2,需自行安装:mayavi
    2. config.py(参数配置文件):
      1. import os
      2. import numpy as np
      3. image_dir = '设置图片目录路径'
      4. MRT = 0.7
      5. #相机内参矩阵,其中,K[0][0]和K[1][1]代表相机焦距,而K[0][2]和K[1][2]
      6. #代表图像的中心像素。
      7. K = np.array([
      8. [2362.12, 0, 720],
      9. [0, 2362.12, 578],
      10. [0, 0, 1]])
      11. #选择性删除所选点的范围。
      12. x = 0.5
      13. y = 1
    3. main.py(主文件):
      1. '''
      2. 原理可参考https://zhuanlan.zhihu.com/p/30033898
      3. '''
      4. import os
      5. import cv2
      6. import sys
      7. import math
      8. import config
      9. import collections
      10. import numpy as np
      11. import matplotlib.pyplot as plt
      12. from mayavi import mlab
      13. from scipy.linalg import lstsq
      14. from mpl_toolkits.mplot3d import Axes3D
      15. from scipy.optimize import least_squares
      16. ##########################
      17. #两张图之间的特征提取及匹配
      18. ##########################
      19. def extract_features(image_names):
      20. #-------------wsy fixed--------------------------------
      21. # sift = cv2.xfeatures2d.SIFT_create(0, 3, 0.04, 10)
      22. sift = cv2.SIFT_create(0, 3, 0.04, 10)
      23. #-------------------------------------------------
      24. key_points_for_all = []
      25. descriptor_for_all = []
      26. colors_for_all = []
      27. for image_name in image_names:
      28. image = cv2.imread(image_name)
      29. if image is None:
      30. continue
      31. key_points, descriptor = sift.detectAndCompute(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), None) # 抽取关键点和描述符
      32. if len(key_points) <= 10:
      33. continue
      34. key_points_for_all.append(key_points) # 1788,1245, 2226
      35. descriptor_for_all.append(descriptor)
      36. colors = np.zeros((len(key_points), 3)) # 保存关键点的颜色信息(rgb)
      37. for i, key_point in enumerate(key_points):
      38. p = key_point.pt
      39. colors[i] = image[int(p[1])][int(p[0])]
      40. colors_for_all.append(colors)
      41. return np.array(key_points_for_all), np.array(descriptor_for_all), np.array(colors_for_all)
      42. def match_features(query, train): # 使用knn算法匹配特征点
      43. bf = cv2.BFMatcher(cv2.NORM_L2)
      44. knn_matches = bf.knnMatch(query, train, k=2)
      45. matches = []
      46. #Apply Lowe's SIFT matching ratio test(MRT),值得一提的是,这里的匹配没有
      47. #标准形式,可以根据需求进行改动。
      48. for m, n in knn_matches:
      49. if m.distance < config.MRT * n.distance: # 调试:此处条件可以满足所以暂时先不注释
      50. matches.append(m)
      51. return np.array(matches) # 44 ,26
      52. def match_all_features(descriptor_for_all):
      53. matches_for_all = []
      54. for i in range(len(descriptor_for_all) - 1):
      55. matches = match_features(descriptor_for_all[i], descriptor_for_all[i + 1])
      56. matches_for_all.append(matches)
      57. return np.array(matches_for_all)
      58. ######################
      59. #寻找图与图之间的对应相机旋转角度以及相机平移
      60. ######################
      61. def find_transform(K, p1, p2):
      62. focal_length = 0.5 * (K[0, 0] + K[1, 1])
      63. principle_point = (K[0, 2], K[1, 2])
      64. E,mask = cv2.findEssentialMat(p1, p2, focal_length, principle_point, cv2.RANSAC, 0.999, 1.0)
      65. cameraMatrix = np.array([[focal_length, 0, principle_point[0]], [0, focal_length, principle_point[1]], [0, 0, 1]])
      66. pass_count, R, T, mask = cv2.recoverPose(E, p1, p2, cameraMatrix, mask)
      67. return R, T, mask
      68. def get_matched_points(p1, p2, matches):
      69. src_pts = np.asarray([p1[m.queryIdx].pt for m in matches])
      70. dst_pts = np.asarray([p2[m.trainIdx].pt for m in matches])
      71. return src_pts, dst_pts
      72. def get_matched_colors(c1, c2, matches):
      73. color_src_pts = np.asarray([c1[m.queryIdx] for m in matches])
      74. color_dst_pts = np.asarray([c2[m.trainIdx] for m in matches])
      75. return color_src_pts, color_dst_pts
      76. #选择重合的点
      77. def maskout_points(p1, mask):
      78. p1_copy = []
      79. for i in range(len(mask)):
      80. if mask[i] > 0:
      81. p1_copy.append(p1[i])
      82. return np.array(p1_copy) # 也不为空
      83. def init_structure(K, key_points_for_all, colors_for_all, matches_for_all):
      84. p1, p2 = get_matched_points(key_points_for_all[0], key_points_for_all[1], matches_for_all[0])
      85. c1, c2 = get_matched_colors(colors_for_all[0], colors_for_all[1], matches_for_all[0])
      86. if find_transform(K, p1, p2):
      87. R,T,mask = find_transform(K, p1, p2)
      88. else:
      89. R,T,mask = np.array([]), np.array([]), np.array([])
      90. p1 = maskout_points(p1, mask)
      91. p2 = maskout_points(p2, mask)
      92. colors = maskout_points(c1, mask)
      93. #设置第一个相机的变换矩阵,即作为剩下摄像机矩阵变换的基准。
      94. R0 = np.eye(3, 3)
      95. T0 = np.zeros((3, 1))
      96. structure = reconstruct(K, R0, T0, R, T, p1, p2)
      97. rotations = [R0, R]
      98. motions = [T0, T]
      99. correspond_struct_idx = []
      100. for key_p in key_points_for_all:
      101. correspond_struct_idx.append(np.ones(len(key_p)) *- 1) # 说明此处len(key_p)=0 不是,这个只是用来指定维度的
      102. correspond_struct_idx = np.array(correspond_struct_idx) # 此处得到的全是-1
      103. idx = 0
      104. matches = matches_for_all[0] # 注此处只取了0
      105. print("####################init_structure:queryIdx & trainIdx")
      106. for i, match in enumerate(matches):
      107. if mask[i] == 0:
      108. continue
      109. correspond_struct_idx[0][int(match.queryIdx)] = idx # 此处代码也会被执行到 correspond[0]元素其实就是queryidx和trainidx进行相互对应
      110. #------------wsy add------------------------
      111. print(match.queryIdx," & ", match.trainIdx)
      112. #-----------------------------------------
      113. correspond_struct_idx[1][int(match.trainIdx)] = idx #
      114. idx += 1
      115. print("")
      116. return structure, correspond_struct_idx, colors, rotations, motions
      117. #############
      118. #三维重建
      119. #############
      120. def reconstruct(K, R1, T1, R2, T2, p1, p2):
      121. proj1 = np.zeros((3, 4))
      122. proj2 = np.zeros((3, 4))
      123. proj1[0:3, 0:3] = np.float32(R1)
      124. proj1[:, 3] = np.float32(T1.T)
      125. proj2[0:3, 0:3] = np.float32(R2)
      126. proj2[:, 3] = np.float32(T2.T)
      127. fk = np.float32(K)
      128. proj1 = np.dot(fk, proj1)
      129. proj2 = np.dot(fk, proj2)
      130. s = cv2.triangulatePoints(proj1, proj2, p1.T, p2.T)
      131. structure = []
      132. for i in range(len(s[0])):
      133. col = s[:, i]
      134. col /= col[3]
      135. structure.append([col[0], col[1], col[2]])
      136. return np.array(structure) # 不为空
      137. ###########################
      138. #将已作出的点云进行融合
      139. ###########################
      140. def fusion_structure(matches, struct_indices, next_struct_indices, structure, next_structure, colors, next_colors):
      141. for i,match in enumerate(matches):
      142. query_idx = match.queryIdx
      143. train_idx = match.trainIdx
      144. struct_idx = struct_indices[query_idx]
      145. if struct_idx >= 0:
      146. next_struct_indices[train_idx] = struct_idx
      147. continue
      148. structure = np.append(structure, [next_structure[i]], axis = 0) # 此处代码会被执行
      149. colors = np.append(colors, [next_colors[i]], axis = 0)
      150. struct_indices[query_idx] = next_struct_indices[train_idx] = len(structure) - 1
      151. return struct_indices, next_struct_indices, structure, colors
      152. #制作图像点以及空间点
      153. def get_objpoints_and_imgpoints(matches, struct_indices, structure, key_points):
      154. object_points = []
      155. image_points = []
      156. #-----------wsy add ---------------------------
      157. print("####################get_objpoints_and_imgpoints:queryIdx & trainIdx:")
      158. #---------------------------------
      159. for match in matches:
      160. query_idx = match.queryIdx
      161. train_idx = match.trainIdx
      162. #---------------wsy add---------------------
      163. print(query_idx," & ",train_idx)
      164. #-----------------wsy fixed-----------------------
      165. # struct_idx = struct_indices[query_idx] # 因为此处得到的都是-1,所以返回为空,但是struct_indices不都是-1,所以还与索引有关
      166. struct_idx = struct_indices[0][query_idx] #
      167. #-------------------------------------------------
      168. if struct_idx < 0:
      169. continue
      170. object_points.append(structure[int(struct_idx)]) # 此处代码未被执行
      171. image_points.append(key_points[train_idx].pt)
      172. print("")
      173. return np.array(object_points), np.array(image_points)
      174. ########################
      175. #bundle adjustment
      176. ########################
      177. # 这部分中,函数get_3dpos是原方法中对某些点的调整,而get_3dpos2是根据笔者的需求进行的修正,即将原本需要修正的点全部删除。
      178. # bundle adjustment请参见https://www.cnblogs.com/zealousness/archive/2018/12/21/10156733.html
      179. def get_3dpos(pos, ob, r, t, K):
      180. dtype = np.float32
      181. def F(x):
      182. p,J = cv2.projectPoints(x.reshape(1, 1, 3), r, t, K, np.array([]))
      183. p = p.reshape(2)
      184. e = ob - p
      185. err = e
      186. return err
      187. res = least_squares(F, pos)
      188. return res.x
      189. def get_3dpos_v1(pos,ob,r,t,K):
      190. p,J = cv2.projectPoints(pos.reshape(1, 1, 3), r, t, K, np.array([]))
      191. p = p.reshape(2)
      192. e = ob - p
      193. if abs(e[0]) > config.x or abs(e[1]) > config.y:
      194. return None
      195. return pos
      196. def bundle_adjustment(rotations, motions, K, correspond_struct_idx, key_points_for_all, structure):
      197. for i in range(len(rotations)):
      198. r, _ = cv2.Rodrigues(rotations[i])
      199. rotations[i] = r
      200. for i in range(len(correspond_struct_idx)):
      201. point3d_ids = correspond_struct_idx[i]
      202. key_points = key_points_for_all[i]
      203. r = rotations[i]
      204. t = motions[i]
      205. for j in range(len(point3d_ids)):
      206. point3d_id = int(point3d_ids[j])
      207. if point3d_id < 0:
      208. continue
      209. new_point = get_3dpos_v1(structure[point3d_id], key_points[j].pt, r, t, K)
      210. structure[point3d_id] = new_point
      211. return structure
      212. #######################
      213. #作图
      214. #######################
      215. # 这里有两种方式作图,其中一个是matplotlib做的,但是第二个是基于mayavi做的,效果上看,fig_v1效果更好。fig_v2是mayavi加颜色的效果。
      216. def fig(structure, colors):
      217. colors /= 255
      218. for i in range(len(colors)):
      219. colors[i, :] = colors[i, :][[2, 1, 0]]
      220. fig = plt.figure()
      221. fig.suptitle('3d')
      222. ax = fig.gca(projection = '3d')
      223. for i in range(len(structure)):
      224. ax.scatter(structure[i, 0], structure[i, 1], structure[i, 2], color = colors[i, :], s = 5)
      225. ax.set_xlabel('x axis')
      226. ax.set_ylabel('y axis')
      227. ax.set_zlabel('z axis')
      228. ax.view_init(elev = 135, azim = 90)
      229. plt.show()
      230. def fig_v1(structure):
      231. mlab.points3d(structure[:, 0], structure[:, 1], structure[:, 2], mode = 'point', name = 'dinosaur')
      232. mlab.show() # 显示了一个灰图,好像是有一些白色的点
      233. a=1
      234. def fig_v2(structure, colors):
      235. for i in range(len(structure)):
      236. # mlab.points3d(structure[i][0], structure[i][1], structure[i][2],
      237. # mode = 'point', name = 'dinosaur', color = colors[i])
      238. mlab.points3d(structure[i][0], structure[i][1], structure[i][2],
      239. mode = 'point', name = 'dinosaur', color = (colors[i][0],colors[i][1],colors[i][2]))
      240. mlab.show()
      241. a=1
      242. def main():
      243. imgdir = config.image_dir # note 图片文件目录
      244. img_names = os.listdir(imgdir)
      245. img_names = sorted(img_names)
      246. for i in range(len(img_names)):
      247. img_names[i] = imgdir + img_names[i]
      248. # img_names = img_names[0:10]
      249. # K是摄像头的参数矩阵
      250. K = config.K
      251. key_points_for_all, descriptor_for_all, colors_for_all = extract_features(img_names)
      252. matches_for_all = match_all_features(descriptor_for_all) # 匹配特征(果然看的每一个资料中的每一句话都是有用的
      253. structure, correspond_struct_idx, colors, rotations, motions = init_structure(K, key_points_for_all, colors_for_all, matches_for_all)
      254. for i in range(1, len(matches_for_all)): # len(matches_for_all) =2,所以其实下方只有一次循环
      255. #---------------wsy fixed-----------------------------
      256. # object_points, image_points = get_objpoints_and_imgpoints(matches_for_all[i], correspond_struct_idx[i], structure, key_points_for_all[i + 1]) # correspnd_struct_idx需要查看赋值的地方
      257. object_points, image_points = get_objpoints_and_imgpoints(matches_for_all[0], correspond_struct_idx, structure, key_points_for_all[i + 1])
      258. # 在python的opencv中solvePnPRansac函数的第一个参数长度需要大于7,否则会报错
      259. #这里对小于7的点集做一个重复填充操作,即用点集中的第一个点补满7个 wsy:还需要考虑点集为空的情况
      260. if len(image_points) < 7:
      261. #------------wsy add------------
      262. if len(image_points)==0:
      263. # image_points=np.insert(image_points,0,0)
      264. # object_points=np.insert(object_points,0,0)
      265. continue
      266. #---------------------------------
      267. while len(image_points) < 7:
      268. object_points = np.append(object_points, [object_points[0]], axis = 0) # 报错:index 0 is out of bounds for axis 0 with size 0
      269. image_points = np.append(image_points, [image_points[0]], axis = 0)
      270. #--------wsy add------------------------------
      271. # image_points.dtype=np.float32
      272. # object_points.dtype=np.float32
      273. #---------------------------------------------
      274. _, r, T, _ = cv2.solvePnPRansac(object_points, image_points, K, np.array([]))
      275. """
      276. 报错:error: (-215:Assertion failed) npoints >= 4 && npoints == std::max(ipoints.checkVector(2, CV_32F), i
      277. """
      278. R, _ = cv2.Rodrigues(r)
      279. rotations.append(R)
      280. motions.append(T)
      281. p1, p2 = get_matched_points(key_points_for_all[i], key_points_for_all[i + 1], matches_for_all[i])
      282. c1, c2 = get_matched_colors(colors_for_all[i], colors_for_all[i + 1], matches_for_all[i])
      283. next_structure = reconstruct(K, rotations[i], motions[i], R, T, p1, p2)
      284. correspond_struct_idx[i], correspond_struct_idx[i + 1], structure, colors = fusion_structure(matches_for_all[i],correspond_struct_idx[i],correspond_struct_idx[i+1],structure,next_structure,colors,c1)
      285. structure = bundle_adjustment(rotations, motions, K, correspond_struct_idx, key_points_for_all, structure)
      286. i = 0
      287. # 由于经过bundle_adjustment的structure,会产生一些空的点(实际代表的意思是已被删除)
      288. # 这里删除那些为空的点
      289. while i < len(structure):
      290. if math.isnan(structure[i][0]):
      291. structure = np.delete(structure, i, 0)
      292. colors = np.delete(colors, i, 0)
      293. i -= 1
      294. i += 1
      295. print(len(structure)) # 4
      296. print(len(motions)) # 3
      297. # np.save('structure.npy', structure)
      298. # np.save('colors.npy', colors)
      299. fig(structure,colors) # 就只是一个简单的3维坐标,有几个点
      300. # fig_v1(structure) # 灰图,有几个白点
      301. fig_v2(structure, colors) # 也是一个灰图,几个白点。
      302. if __name__ == '__main__':
      303. main()
    4. 原始代码来源:adnappp/Sfm-python: 三维重建算法Structure from Motion(Sfm)的python实现 (github.com)
    5. 虽然可以运行,但最终结果和理想不太一样,希望有跑出好的结果的朋友交流一下~

  • 相关阅读:
    Python多线程教程
    doris手动添加分区自动消失的问题
    Android不带电池设备文件系统配置
    SpringFramework:Spring 概述
    Android 获取设备内存和外存
    每日一题: leetcode1726 同积元组
    .NET程序配置文件
    `算法知识` 最大公约数GCD
    使用DIV+CSS技术设计的非遗文化网页与实现制作(web前端网页制作课作业)
    炒冷饭、语雀崩、领会员-我最主观的一段文字
  • 原文地址:https://blog.csdn.net/weixin_45647721/article/details/126869327