Skip to content

Commit

Permalink
Bumped version
Browse files Browse the repository at this point in the history
Bumped the version to 1.3.1
Removed the remap function from USBCamera
USBCamera now only undistorts using cv.undistort()

Signed-off-by: Alex Pereira <[email protected]>
  • Loading branch information
Mario13546 committed Jun 22, 2023
1 parent ee01034 commit 1deb64d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 54 deletions.
14 changes: 7 additions & 7 deletions frc_apriltags/apriltags.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, size: int = 6) -> None:
self.comms = NetworkCommunications()

# Creates a pupil apriltags detector
self.detector = pupil_apriltags.Detector(families = "tag16h5", nthreads = 10, quad_decimate = 1.0, quad_sigma = 0.0, refine_edges = 2.0, decode_sharpening = 1.00)
self.detector = pupil_apriltags.Detector(families = "tag16h5")

# Variables
self.tagSize = Units.inchesToMeters(size)
Expand All @@ -38,9 +38,9 @@ def __init__(self, size: int = 6) -> None:

def detectTags(self, stream, camera_matrix, vizualization: int = 0):
"""
Detects AprilTags in a stream using pupil_apriltags.
Detects AprilTags in a stream using ``pupil_apriltags``.
:param stream: The images generated by reading a VideoCapture.
:param stream: The images generated by reading a ``VideoCapture``.
:param camera_matrix: The camera's intrinsic calibration matrix.
:param vizualization: 0 - Highlight, 1 - Highlight + Boxes, 2 - Highlight + Axes, 3 - Highlight + Boxes + Axes.
:return: The detection result.
Expand All @@ -55,14 +55,14 @@ def detectTags(self, stream, camera_matrix, vizualization: int = 0):
# Define the intrinsic parameters of the camera
intrinsic_properties = (camera_matrix[0, 0], camera_matrix[1, 1], camera_matrix[0, 2], camera_matrix[1, 2]) # fx, fy, cx, cy

# Detect the AprilTags in the image with Pupil Apriltags
detections = self.detector.detect(gray, estimate_tag_pose = True, camera_params = intrinsic_properties, tag_size = self.tagSize)
# Detect the AprilTags in the image with pupil_apriltags
detections = self.detector.detect(img = gray, estimate_tag_pose = True, camera_params = intrinsic_properties, tag_size = self.tagSize)

# Variables to use in detections
results = []
maxError = 1e-3
maxHamming = 1
minConfidence = 30
minConfidence = 50

# Variables to use in sorting the data
best = None
Expand Down Expand Up @@ -141,7 +141,7 @@ def getPose3D(self, poseMatrix = None):
"""
Calculates a WPILib ``Pose3d`` from the PupilApriltags matrix.
:param poseMatrix: A 3x4 ``numpy.ndarray()``.
:param poseMatrix: A 3x4 ``numpy.ndarray``.
:return: A ``Pose3d`` object.
"""
# Variables
Expand Down
56 changes: 10 additions & 46 deletions frc_apriltags/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,45 +110,6 @@ def calibrateCamera(self, dirPath: str):
# Get results
ret, self.camMatrix, self.camdistortion, rvecs, tvecs = self.calibrate.calibrateCamera()

def undistort(self):
"""
Undistorts an image using cv.undistort().
:return: The undistorted stream.
"""
# Creates a cameraMatrix
newCameraMatrix, roi = cv.getOptimalNewCameraMatrix(self.camMatrix, self.camdistortion, self.resolution, 1, self.resolution)

# Undistorts the image
undistortedStream = cv.undistort(self.getStream(), self.camMatrix, self.camdistortion, None, newCameraMatrix)

# Crops the image
x, y, w, h = roi
undistortedStream = undistortedStream[y:y+h, x:x+w]

return undistortedStream

def rectify(self):
"""
Undistorts an image using cv.remap().
:return: The undistorted stream.
"""
# Creates a cameraMatrix
newCameraMatrix, roi = cv.getOptimalNewCameraMatrix(self.camMatrix, self.camdistortion, self.resolution, 1, self.resolution)

# Unpacks the ROI data
x, y, w, h = roi

# Undistorts the image
mapx, mapy = cv.initUndistortRectifyMap(self.camMatrix, self.camdistortion, None, newCameraMatrix, (w,h), 5)
undistortedStream = cv.remap(self.getStream(), mapx, mapy, cv.INTER_LINEAR)

# Crop the image
undistortedStream = undistortedStream[y:y+h, x:x+w]

return undistortedStream

def getStream(self):
"""
Gets the stream from this camera's capture.
Expand All @@ -160,18 +121,21 @@ def getStream(self):

return self.stream

def getUndistortedStream(self, algorithm: int = 1):
def getUndistortedStream(self):
"""
Gets the undistorted stream from this camera's capture.
:param algorithm: 0 to use ``cv.undistort()``, 1 to use ``cv.remap()``
:return: The undistorted stream.
"""
# Undistorts the stream
if (algorithm == 0):
self.stream = self.undistort()
elif (algorithm == 1):
self.stream = self.rectify()
# Creates a cameraMatrix
newCameraMatrix, roi = cv.getOptimalNewCameraMatrix(self.camMatrix, self.camdistortion, self.resolution, 1, self.resolution)

# Undistorts the image
undistortedStream = cv.undistort(self.getStream(), self.camMatrix, self.camdistortion, None, newCameraMatrix)

# Crops the image
x, y, w, h = roi
self.undistortedStream = undistortedStream[y:y+h, x:x+w]

return self.stream

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ name = "frc-apriltags" # Required
#
# For a discussion on single-sourcing the version, see
# https://packaging.python.org/guides/single-sourcing-package-version/
version = "1.3.0" # Required
version = "1.3.1" # Required

# This is a one-line description or tagline of what your project does. This
# corresponds to the "Summary" metadata field:
Expand Down

0 comments on commit 1deb64d

Please sign in to comment.