Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image align examples fix #1087

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/ImageAlign/depth_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def getFps(self):
device = dai.Device()

calibrationHandler = device.readCalibration()
rgbIntrinsics = calibrationHandler.getCameraIntrinsics(RGB_SOCKET, int(1920 / ISP_SCALE), int(1080 / ISP_SCALE))
rgbDistortion = calibrationHandler.getDistortionCoefficients(RGB_SOCKET)
distortionModel = calibrationHandler.getDistortionModel(RGB_SOCKET)
if distortionModel != dai.CameraModel.Perspective:
Expand Down Expand Up @@ -160,6 +159,8 @@ def updateBlendWeights(percentRgb):
if frameDepth is not None:
cvFrame = frameRgb.getCvFrame()

rgbIntrinsics = calibrationHandler.getCameraIntrinsics(RGB_SOCKET, int(cvFrame.shape[1]), int(cvFrame.shape[0]))

# Undistort the rgb frame
cvFrameUndistorted = cv2.undistort(
cvFrame,
Expand All @@ -172,7 +173,7 @@ def updateBlendWeights(percentRgb):
cv2.imshow("Depth aligned", alignedDepthColorized)

blended = cv2.addWeighted(
cvFrame, rgbWeight, alignedDepthColorized, depthWeight, 0
cvFrameUndistorted, rgbWeight, alignedDepthColorized, depthWeight, 0
)
cv2.putText(
blended,
Expand Down
26 changes: 22 additions & 4 deletions examples/ImageAlign/image_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cv2
import depthai as dai
from datetime import timedelta
import numpy as np

# This is an interactive example that shows how two frame sources without depth information.
FPS = 30.0
Expand All @@ -14,7 +15,16 @@
COLOR_RESOLUTION = dai.ColorCameraProperties.SensorResolution.THE_1080_P
LEFT_RIGHT_RESOLUTION = dai.MonoCameraProperties.SensorResolution.THE_720_P

ISP_SCALE = 3

device = dai.Device()

calibrationHandler = device.readCalibration()
rgbDistortion = calibrationHandler.getDistortionCoefficients(RGB_SOCKET)
distortionModel = calibrationHandler.getDistortionModel(RGB_SOCKET)
if distortionModel != dai.CameraModel.Perspective:
raise RuntimeError("Unsupported distortion model for RGB camera. This example supports only Perspective model.")

pipeline = dai.Pipeline()

# Define sources and outputs
Expand All @@ -32,7 +42,7 @@
camRgb.setBoardSocket(RGB_SOCKET)
camRgb.setResolution(COLOR_RESOLUTION)
camRgb.setFps(FPS)
camRgb.setIspScale(1, 3)
camRgb.setIspScale(1, ISP_SCALE)

out.setStreamName("out")

Expand Down Expand Up @@ -109,12 +119,20 @@ def updateDepthPlane(depth):
# Colorize the aligned depth
leftCv = leftAligned.getCvFrame()

rgbIntrinsics = calibrationHandler.getCameraIntrinsics(RGB_SOCKET, int(frameRgbCv.shape[1]), int(frameRgbCv.shape[0]))

cvFrameUndistorted = cv2.undistort(
frameRgbCv,
np.array(rgbIntrinsics),
np.array(rgbDistortion),
)

if len(leftCv.shape) == 2:
leftCv = cv2.cvtColor(leftCv, cv2.COLOR_GRAY2BGR)
if leftCv.shape != frameRgbCv.shape:
leftCv = cv2.resize(leftCv, (frameRgbCv.shape[1], frameRgbCv.shape[0]))
if leftCv.shape != cvFrameUndistorted.shape:
leftCv = cv2.resize(leftCv, (cvFrameUndistorted.shape[1], cvFrameUndistorted.shape[0]))

blended = cv2.addWeighted(frameRgbCv, rgbWeight, leftCv, leftWeight, 0)
blended = cv2.addWeighted(cvFrameUndistorted, rgbWeight, leftCv, leftWeight, 0)
cv2.imshow(windowName, blended)

key = cv2.waitKey(1)
Expand Down
20 changes: 18 additions & 2 deletions examples/ImageAlign/thermal_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def getFps(self):
raise RuntimeError("No thermal camera found!")


ISP_SCALE = 3

calibrationHandler = device.readCalibration()
rgbDistortion = calibrationHandler.getDistortionCoefficients(RGB_SOCKET)
distortionModel = calibrationHandler.getDistortionModel(RGB_SOCKET)
if distortionModel != dai.CameraModel.Perspective:
raise RuntimeError("Unsupported distortion model for RGB camera. This example supports only Perspective model.")

pipeline = dai.Pipeline()

# Define sources and outputs
Expand All @@ -57,7 +65,7 @@ def getFps(self):
camRgb.setBoardSocket(RGB_SOCKET)
camRgb.setResolution(COLOR_RESOLUTION)
camRgb.setFps(FPS)
camRgb.setIspScale(1,3)
camRgb.setIspScale(1,ISP_SCALE)

out.setStreamName("out")

Expand Down Expand Up @@ -132,6 +140,14 @@ def updateDepthPlane(depth):
frameRgbCv = frameRgb.getCvFrame()
fpsCounter.tick()

rgbIntrinsics = calibrationHandler.getCameraIntrinsics(RGB_SOCKET, int(frameRgbCv.shape[1]), int(frameRgbCv.shape[0]))

cvFrameUndistorted = cv2.undistort(
frameRgbCv,
np.array(rgbIntrinsics),
np.array(rgbDistortion),
)

# Colorize the aligned depth
thermalFrame = thermalAligned.getCvFrame().astype(np.float32)
# Create a mask for nan values
Expand All @@ -143,7 +159,7 @@ def updateDepthPlane(depth):
# Apply the mask back with black pixels (0)
colormappedFrame[mask] = 0

blended = cv2.addWeighted(frameRgbCv, rgbWeight, colormappedFrame, thermalWeight, 0)
blended = cv2.addWeighted(cvFrameUndistorted, rgbWeight, colormappedFrame, thermalWeight, 0)

cv2.putText(
blended,
Expand Down
23 changes: 20 additions & 3 deletions examples/ImageAlign/tof_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,18 @@ def getFps(self):
return (len(self.frameTimes) - 1) / (self.frameTimes[-1] - self.frameTimes[0])


ISP_SCALE = 2

device = dai.Device()

calibrationHandler = device.readCalibration()
rgbDistortion = calibrationHandler.getDistortionCoefficients(RGB_SOCKET)
distortionModel = calibrationHandler.getDistortionModel(RGB_SOCKET)
if distortionModel != dai.CameraModel.Perspective:
raise RuntimeError("Unsupported distortion model for RGB camera. This example supports only Perspective model.")

pipeline = dai.Pipeline()

# Define sources and outputs
camRgb = pipeline.create(dai.node.ColorCamera)
tof = pipeline.create(dai.node.ToF)
Expand All @@ -48,7 +58,7 @@ def getFps(self):
camRgb.setBoardSocket(RGB_SOCKET)
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_800_P)
camRgb.setFps(FPS)
camRgb.setIspScale(1, 2)
camRgb.setIspScale(1, ISP_SCALE)

out.setStreamName("out")

Expand Down Expand Up @@ -109,7 +119,8 @@ def updateBlendWeights(percentRgb):


# Connect to device and start pipeline
with dai.Device(pipeline) as device:
with device:
device.startPipeline(pipeline)
queue = device.getOutputQueue("out", 8, False)

# Configure windows; trackbar adjusts blending ratio of rgb/depth
Expand Down Expand Up @@ -138,6 +149,12 @@ def updateBlendWeights(percentRgb):
# Blend when both received
if frameDepth is not None:
cvFrame = frameRgb.getCvFrame()
rgbIntrinsics = calibrationHandler.getCameraIntrinsics(RGB_SOCKET, int(cvFrame.shape[1]), int(cvFrame.shape[0]))
cvFrameUndistorted = cv2.undistort(
cvFrame,
np.array(rgbIntrinsics),
np.array(rgbDistortion),
)
# Colorize the aligned depth
alignedDepthColorized = colorizeDepth(frameDepth.getFrame())
# Resize depth to match the rgb frame
Expand All @@ -153,7 +170,7 @@ def updateBlendWeights(percentRgb):
cv2.imshow("depth", alignedDepthColorized)

blended = cv2.addWeighted(
cvFrame, rgbWeight, alignedDepthColorized, depthWeight, 0
cvFrameUndistorted, rgbWeight, alignedDepthColorized, depthWeight, 0
)
cv2.imshow(rgbDepthWindowName, blended)

Expand Down