camera_adjuster.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from collections import OrderedDict
  2. import cv2 as cv
  3. import numpy as np
  4. from .stitching_error import StitchingError
  5. class CameraAdjuster:
  6. """https://docs.opencv.org/4.x/d5/d56/classcv_1_1detail_1_1BundleAdjusterBase.html""" # noqa
  7. CAMERA_ADJUSTER_CHOICES = OrderedDict()
  8. CAMERA_ADJUSTER_CHOICES['ray'] = cv.detail_BundleAdjusterRay
  9. CAMERA_ADJUSTER_CHOICES['reproj'] = cv.detail_BundleAdjusterReproj
  10. CAMERA_ADJUSTER_CHOICES['affine'] = cv.detail_BundleAdjusterAffinePartial
  11. CAMERA_ADJUSTER_CHOICES['no'] = cv.detail_NoBundleAdjuster
  12. DEFAULT_CAMERA_ADJUSTER = list(CAMERA_ADJUSTER_CHOICES.keys())[0]
  13. DEFAULT_REFINEMENT_MASK = "xxxxx"
  14. def __init__(self,
  15. adjuster=DEFAULT_CAMERA_ADJUSTER,
  16. refinement_mask=DEFAULT_REFINEMENT_MASK):
  17. self.adjuster = CameraAdjuster.CAMERA_ADJUSTER_CHOICES[adjuster]()
  18. self.set_refinement_mask(refinement_mask)
  19. self.adjuster.setConfThresh(1)
  20. def set_refinement_mask(self, refinement_mask):
  21. mask_matrix = np.zeros((3, 3), np.uint8)
  22. if refinement_mask[0] == 'x':
  23. mask_matrix[0, 0] = 1
  24. if refinement_mask[1] == 'x':
  25. mask_matrix[0, 1] = 1
  26. if refinement_mask[2] == 'x':
  27. mask_matrix[0, 2] = 1
  28. if refinement_mask[3] == 'x':
  29. mask_matrix[1, 1] = 1
  30. if refinement_mask[4] == 'x':
  31. mask_matrix[1, 2] = 1
  32. self.adjuster.setRefinementMask(mask_matrix)
  33. def adjust(self, features, pairwise_matches, estimated_cameras):
  34. b, cameras = self.adjuster.apply(features,
  35. pairwise_matches,
  36. estimated_cameras)
  37. if not b:
  38. raise StitchingError("Camera parameters adjusting failed.")
  39. return cameras