stitching_detailed.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. """
  2. Stitching sample (advanced)
  3. ===========================
  4. Show how to use Stitcher API from python.
  5. """
  6. # Python 2/3 compatibility
  7. from __future__ import print_function
  8. from types import SimpleNamespace
  9. from collections import OrderedDict
  10. import cv2 as cv
  11. import numpy as np
  12. EXPOS_COMP_CHOICES = OrderedDict()
  13. EXPOS_COMP_CHOICES['gain_blocks'] = cv.detail.ExposureCompensator_GAIN_BLOCKS
  14. EXPOS_COMP_CHOICES['gain'] = cv.detail.ExposureCompensator_GAIN
  15. EXPOS_COMP_CHOICES['channel'] = cv.detail.ExposureCompensator_CHANNELS
  16. EXPOS_COMP_CHOICES['channel_blocks'] = cv.detail.ExposureCompensator_CHANNELS_BLOCKS
  17. EXPOS_COMP_CHOICES['no'] = cv.detail.ExposureCompensator_NO
  18. BA_COST_CHOICES = OrderedDict()
  19. BA_COST_CHOICES['ray'] = cv.detail_BundleAdjusterRay
  20. BA_COST_CHOICES['reproj'] = cv.detail_BundleAdjusterReproj
  21. BA_COST_CHOICES['affine'] = cv.detail_BundleAdjusterAffinePartial
  22. BA_COST_CHOICES['no'] = cv.detail_NoBundleAdjuster
  23. FEATURES_FIND_CHOICES = OrderedDict()
  24. try:
  25. cv.xfeatures2d_SURF.create() # check if the function can be called
  26. FEATURES_FIND_CHOICES['surf'] = cv.xfeatures2d_SURF.create
  27. except (AttributeError, cv.error) as e:
  28. print("SURF not available")
  29. # if SURF not available, ORB is default
  30. FEATURES_FIND_CHOICES['orb'] = cv.ORB.create
  31. try:
  32. FEATURES_FIND_CHOICES['sift'] = cv.xfeatures2d_SIFT.create
  33. except AttributeError:
  34. print("SIFT not available")
  35. try:
  36. FEATURES_FIND_CHOICES['brisk'] = cv.BRISK_create
  37. except AttributeError:
  38. print("BRISK not available")
  39. try:
  40. FEATURES_FIND_CHOICES['akaze'] = cv.AKAZE_create
  41. except AttributeError:
  42. print("AKAZE not available")
  43. SEAM_FIND_CHOICES = OrderedDict()
  44. SEAM_FIND_CHOICES['dp_color'] = cv.detail_DpSeamFinder('COLOR')
  45. SEAM_FIND_CHOICES['dp_colorgrad'] = cv.detail_DpSeamFinder('COLOR_GRAD')
  46. SEAM_FIND_CHOICES['voronoi'] = cv.detail.SeamFinder_createDefault(cv.detail.SeamFinder_VORONOI_SEAM)
  47. SEAM_FIND_CHOICES['no'] = cv.detail.SeamFinder_createDefault(cv.detail.SeamFinder_NO)
  48. ESTIMATOR_CHOICES = OrderedDict()
  49. ESTIMATOR_CHOICES['homography'] = cv.detail_HomographyBasedEstimator
  50. ESTIMATOR_CHOICES['affine'] = cv.detail_AffineBasedEstimator
  51. WARP_CHOICES = (
  52. 'spherical',
  53. 'plane',
  54. 'affine',
  55. 'cylindrical',
  56. 'fisheye',
  57. 'stereographic',
  58. 'compressedPlaneA2B1',
  59. 'compressedPlaneA1.5B1',
  60. 'compressedPlanePortraitA2B1',
  61. 'compressedPlanePortraitA1.5B1',
  62. 'paniniA2B1',
  63. 'paniniA1.5B1',
  64. 'paniniPortraitA2B1',
  65. 'paniniPortraitA1.5B1',
  66. 'mercator',
  67. 'transverseMercator',
  68. )
  69. WAVE_CORRECT_CHOICES = OrderedDict()
  70. WAVE_CORRECT_CHOICES['horiz'] = cv.detail.WAVE_CORRECT_HORIZ
  71. WAVE_CORRECT_CHOICES['no'] = None
  72. WAVE_CORRECT_CHOICES['vert'] = cv.detail.WAVE_CORRECT_VERT
  73. BLEND_CHOICES = ('multiband', 'feather', 'no',)
  74. def get_matcher(args):
  75. try_cuda = args.try_cuda
  76. matcher_type = args.matcher
  77. if args.match_conf is None:
  78. if args.features == 'orb':
  79. match_conf = 0.3
  80. else:
  81. match_conf = 0.65
  82. else:
  83. match_conf = args.match_conf
  84. range_width = args.rangewidth
  85. if matcher_type == "affine":
  86. matcher = cv.detail_AffineBestOf2NearestMatcher(False, try_cuda, match_conf)
  87. elif range_width == -1:
  88. matcher = cv.detail.BestOf2NearestMatcher_create(try_cuda, match_conf)
  89. else:
  90. matcher = cv.detail.BestOf2NearestRangeMatcher_create(range_width, try_cuda, match_conf)
  91. return matcher
  92. def get_compensator(args):
  93. expos_comp_type = EXPOS_COMP_CHOICES[args.expos_comp]
  94. expos_comp_nr_feeds = args.expos_comp_nr_feeds
  95. expos_comp_block_size = args.expos_comp_block_size
  96. # expos_comp_nr_filtering = args.expos_comp_nr_filtering
  97. if expos_comp_type == cv.detail.ExposureCompensator_CHANNELS:
  98. compensator = cv.detail_ChannelsCompensator(expos_comp_nr_feeds)
  99. # compensator.setNrGainsFilteringIterations(expos_comp_nr_filtering)
  100. elif expos_comp_type == cv.detail.ExposureCompensator_CHANNELS_BLOCKS:
  101. compensator = cv.detail_BlocksChannelsCompensator(
  102. expos_comp_block_size, expos_comp_block_size,
  103. expos_comp_nr_feeds
  104. )
  105. # compensator.setNrGainsFilteringIterations(expos_comp_nr_filtering)
  106. else:
  107. compensator = cv.detail.ExposureCompensator_createDefault(expos_comp_type)
  108. return compensator
  109. def main():
  110. args = {
  111. "img_names":["boat5.jpg", "boat2.jpg",
  112. "boat3.jpg", "boat4.jpg",
  113. "boat1.jpg", "boat6.jpg"],
  114. "try_cuda": False,
  115. "work_megapix": 0.6,
  116. "features": "orb",
  117. "matcher": "homography",
  118. "estimator": "homography",
  119. "match_conf": None,
  120. "conf_thresh": 1.0,
  121. "ba": "ray",
  122. "ba_refine_mask": "xxxxx",
  123. "wave_correct": "horiz",
  124. "save_graph": None,
  125. "warp": "spherical",
  126. "seam_megapix": 0.1,
  127. "seam": "dp_color",
  128. "compose_megapix": 3,
  129. "expos_comp": "gain_blocks",
  130. "expos_comp_nr_feeds": 1,
  131. "expos_comp_nr_filtering": 2,
  132. "expos_comp_block_size": 32,
  133. "blend": "multiband",
  134. "blend_strength": 5,
  135. "output": "time_test.jpg",
  136. "timelapse": None,
  137. "rangewidth": -1
  138. }
  139. args = SimpleNamespace(**args)
  140. img_names = args.img_names
  141. work_megapix = args.work_megapix
  142. seam_megapix = args.seam_megapix
  143. compose_megapix = args.compose_megapix
  144. conf_thresh = args.conf_thresh
  145. ba_refine_mask = args.ba_refine_mask
  146. wave_correct = WAVE_CORRECT_CHOICES[args.wave_correct]
  147. if args.save_graph is None:
  148. save_graph = False
  149. else:
  150. save_graph = True
  151. warp_type = args.warp
  152. blend_type = args.blend
  153. blend_strength = args.blend_strength
  154. result_name = args.output
  155. if args.timelapse is not None:
  156. timelapse = True
  157. if args.timelapse == "as_is":
  158. timelapse_type = cv.detail.Timelapser_AS_IS
  159. elif args.timelapse == "crop":
  160. timelapse_type = cv.detail.Timelapser_CROP
  161. else:
  162. print("Bad timelapse method")
  163. exit()
  164. else:
  165. timelapse = False
  166. finder = FEATURES_FIND_CHOICES[args.features]()
  167. seam_work_aspect = 1
  168. full_img_sizes = []
  169. features = []
  170. images = []
  171. is_work_scale_set = False
  172. is_seam_scale_set = False
  173. is_compose_scale_set = False
  174. for name in img_names:
  175. full_img = cv.imread(cv.samples.findFile(name))
  176. if full_img is None:
  177. print("Cannot read image ", name)
  178. exit()
  179. full_img_sizes.append((full_img.shape[1], full_img.shape[0]))
  180. if work_megapix < 0:
  181. img = full_img
  182. work_scale = 1
  183. is_work_scale_set = True
  184. else:
  185. if is_work_scale_set is False:
  186. work_scale = min(1.0, np.sqrt(work_megapix * 1e6 / (full_img.shape[0] * full_img.shape[1])))
  187. is_work_scale_set = True
  188. img = cv.resize(src=full_img, dsize=None, fx=work_scale, fy=work_scale, interpolation=cv.INTER_LINEAR_EXACT)
  189. if is_seam_scale_set is False:
  190. seam_scale = min(1.0, np.sqrt(seam_megapix * 1e6 / (full_img.shape[0] * full_img.shape[1])))
  191. seam_work_aspect = seam_scale / work_scale
  192. is_seam_scale_set = True
  193. img_feat = cv.detail.computeImageFeatures2(finder, img)
  194. features.append(img_feat)
  195. img = cv.resize(src=full_img, dsize=None, fx=seam_scale, fy=seam_scale, interpolation=cv.INTER_LINEAR_EXACT)
  196. images.append(img)
  197. matcher = get_matcher(args)
  198. p = matcher.apply2(features)
  199. matcher.collectGarbage()
  200. if save_graph:
  201. with open(args.save_graph, 'w') as fh:
  202. fh.write(cv.detail.matchesGraphAsString(img_names, p, conf_thresh))
  203. indices = cv.detail.leaveBiggestComponent(features, p, conf_thresh)
  204. img_subset = []
  205. img_names_subset = []
  206. full_img_sizes_subset = []
  207. for i in range(len(indices)):
  208. img_names_subset.append(img_names[indices[i, 0]])
  209. img_subset.append(images[indices[i, 0]])
  210. full_img_sizes_subset.append(full_img_sizes[indices[i, 0]])
  211. images = img_subset
  212. img_names = img_names_subset
  213. full_img_sizes = full_img_sizes_subset
  214. num_images = len(img_names)
  215. if num_images < 2:
  216. print("Need more images")
  217. exit()
  218. estimator = ESTIMATOR_CHOICES[args.estimator]()
  219. b, cameras = estimator.apply(features, p, None)
  220. if not b:
  221. print("Homography estimation failed.")
  222. exit()
  223. for cam in cameras:
  224. cam.R = cam.R.astype(np.float32)
  225. adjuster = BA_COST_CHOICES[args.ba]()
  226. adjuster.setConfThresh(1)
  227. refine_mask = np.zeros((3, 3), np.uint8)
  228. if ba_refine_mask[0] == 'x':
  229. refine_mask[0, 0] = 1
  230. if ba_refine_mask[1] == 'x':
  231. refine_mask[0, 1] = 1
  232. if ba_refine_mask[2] == 'x':
  233. refine_mask[0, 2] = 1
  234. if ba_refine_mask[3] == 'x':
  235. refine_mask[1, 1] = 1
  236. if ba_refine_mask[4] == 'x':
  237. refine_mask[1, 2] = 1
  238. adjuster.setRefinementMask(refine_mask)
  239. b, cameras = adjuster.apply(features, p, cameras)
  240. if not b:
  241. print("Camera parameters adjusting failed.")
  242. exit()
  243. focals = []
  244. for cam in cameras:
  245. focals.append(cam.focal)
  246. focals.sort()
  247. if len(focals) % 2 == 1:
  248. warped_image_scale = focals[len(focals) // 2]
  249. else:
  250. warped_image_scale = (focals[len(focals) // 2] + focals[len(focals) // 2 - 1]) / 2
  251. if wave_correct is not None:
  252. rmats = []
  253. for cam in cameras:
  254. rmats.append(np.copy(cam.R))
  255. rmats = cv.detail.waveCorrect(rmats, wave_correct)
  256. for idx, cam in enumerate(cameras):
  257. cam.R = rmats[idx]
  258. corners = []
  259. masks_warped = []
  260. images_warped = []
  261. sizes = []
  262. masks = []
  263. for i in range(0, num_images):
  264. um = cv.UMat(255 * np.ones((images[i].shape[0], images[i].shape[1]), np.uint8))
  265. masks.append(um)
  266. warper = cv.PyRotationWarper(warp_type, warped_image_scale * seam_work_aspect) # warper could be nullptr?
  267. for idx in range(0, num_images):
  268. K = cameras[idx].K().astype(np.float32)
  269. swa = seam_work_aspect
  270. K[0, 0] *= swa
  271. K[0, 2] *= swa
  272. K[1, 1] *= swa
  273. K[1, 2] *= swa
  274. corner, image_wp = warper.warp(images[idx], K, cameras[idx].R, cv.INTER_LINEAR, cv.BORDER_REFLECT)
  275. corners.append(corner)
  276. sizes.append((image_wp.shape[1], image_wp.shape[0]))
  277. images_warped.append(image_wp)
  278. p, mask_wp = warper.warp(masks[idx], K, cameras[idx].R, cv.INTER_NEAREST, cv.BORDER_CONSTANT)
  279. masks_warped.append(mask_wp.get())
  280. images_warped_f = []
  281. for img in images_warped:
  282. imgf = img.astype(np.float32)
  283. images_warped_f.append(imgf)
  284. compensator = get_compensator(args)
  285. compensator.feed(corners=corners, images=images_warped, masks=masks_warped)
  286. seam_finder = SEAM_FIND_CHOICES[args.seam]
  287. masks_warped = seam_finder.find(images_warped_f, corners, masks_warped)
  288. compose_scale = 1
  289. corners = []
  290. sizes = []
  291. blender = None
  292. timelapser = None
  293. # https://github.com/opencv/opencv/blob/4.x/samples/cpp/stitching_detailed.cpp#L725 ?
  294. for idx, name in enumerate(img_names):
  295. full_img = cv.imread(name)
  296. if not is_compose_scale_set:
  297. if compose_megapix > 0:
  298. compose_scale = min(1.0, np.sqrt(compose_megapix * 1e6 / (full_img.shape[0] * full_img.shape[1])))
  299. is_compose_scale_set = True
  300. compose_work_aspect = compose_scale / work_scale
  301. warped_image_scale *= compose_work_aspect
  302. warper = cv.PyRotationWarper(warp_type, warped_image_scale)
  303. for i in range(0, len(img_names)):
  304. cameras[i].focal *= compose_work_aspect
  305. cameras[i].ppx *= compose_work_aspect
  306. cameras[i].ppy *= compose_work_aspect
  307. sz = (int(round(full_img_sizes[i][0] * compose_scale)),
  308. int(round(full_img_sizes[i][1] * compose_scale)))
  309. K = cameras[i].K().astype(np.float32)
  310. roi = warper.warpRoi(sz, K, cameras[i].R)
  311. corners.append(roi[0:2])
  312. sizes.append(roi[2:4])
  313. if abs(compose_scale - 1) > 1e-1:
  314. img = cv.resize(src=full_img, dsize=None, fx=compose_scale, fy=compose_scale,
  315. interpolation=cv.INTER_LINEAR_EXACT)
  316. else:
  317. img = full_img
  318. _img_size = (img.shape[1], img.shape[0])
  319. K = cameras[idx].K().astype(np.float32)
  320. corner, image_warped = warper.warp(img, K, cameras[idx].R, cv.INTER_LINEAR, cv.BORDER_REFLECT)
  321. mask = 255 * np.ones((img.shape[0], img.shape[1]), np.uint8)
  322. p, mask_warped = warper.warp(mask, K, cameras[idx].R, cv.INTER_NEAREST, cv.BORDER_CONSTANT)
  323. compensator.apply(idx, corners[idx], image_warped, mask_warped)
  324. image_warped_s = image_warped.astype(np.int16)
  325. dilated_mask = cv.dilate(masks_warped[idx], None)
  326. seam_mask = cv.resize(dilated_mask, (mask_warped.shape[1], mask_warped.shape[0]), 0, 0, cv.INTER_LINEAR_EXACT)
  327. mask_warped = cv.bitwise_and(seam_mask, mask_warped)
  328. if blender is None and not timelapse:
  329. blender = cv.detail.Blender_createDefault(cv.detail.Blender_NO)
  330. dst_sz = cv.detail.resultRoi(corners=corners, sizes=sizes)
  331. blend_width = np.sqrt(dst_sz[2] * dst_sz[3]) * blend_strength / 100
  332. if blend_width < 1:
  333. blender = cv.detail.Blender_createDefault(cv.detail.Blender_NO)
  334. elif blend_type == "multiband":
  335. blender = cv.detail_MultiBandBlender()
  336. blender.setNumBands((np.log(blend_width) / np.log(2.) - 1.).astype(np.int64))
  337. elif blend_type == "feather":
  338. blender = cv.detail_FeatherBlender()
  339. blender.setSharpness(1. / blend_width)
  340. blender.prepare(dst_sz)
  341. elif timelapser is None and timelapse:
  342. timelapser = cv.detail.Timelapser_createDefault(timelapse_type)
  343. timelapser.initialize(corners, sizes)
  344. if timelapse:
  345. ma_tones = np.ones((image_warped_s.shape[0], image_warped_s.shape[1]), np.uint8)
  346. timelapser.process(image_warped_s, ma_tones, corners[idx])
  347. pos_s = img_names[idx].rfind("/")
  348. if pos_s == -1:
  349. fixed_file_name = "fixed_" + img_names[idx]
  350. else:
  351. fixed_file_name = img_names[idx][:pos_s + 1] + "fixed_" + img_names[idx][pos_s + 1:]
  352. cv.imwrite(fixed_file_name, timelapser.getDst())
  353. else:
  354. blender.feed(cv.UMat(image_warped_s), mask_warped, corners[idx])
  355. if not timelapse:
  356. result = None
  357. result_mask = None
  358. result, result_mask = blender.blend(result, result_mask)
  359. # cv.imwrite(result_name, result)
  360. return result
  361. # zoom_x = 600.0 / result.shape[1]
  362. # dst = cv.normalize(src=result, dst=None, alpha=255., norm_type=cv.NORM_MINMAX, dtype=cv.CV_8U)
  363. # dst = cv.resize(dst, dsize=None, fx=zoom_x, fy=zoom_x)
  364. # cv.imshow(result_name, dst)
  365. # cv.waitKey()
  366. if __name__ == '__main__':
  367. import tracemalloc
  368. import time
  369. tracemalloc.start()
  370. start = time.time()
  371. result = main()
  372. current, peak = tracemalloc.get_traced_memory()
  373. print(f"Current memory usage is {current / 10**6}MB; Peak was {peak / 10**6}MB")
  374. tracemalloc.stop()
  375. end = time.time()
  376. print(end - start)