编辑:OAK中国
首发:oakchina.cn
喜欢的话,请多多👍⭐️✍
▌前言
Hello,大家好,这里是OAK中国,我是助手君。
我们都知道OAK相机是板载AI+CV的深度相机,能够做3D测距,而大家经常好奇的问题就是:OAK相机如何获取3D坐标?
具体的方式你可以参考这个示例
import math
import numpy as np
import depthai as dai
class HostSpatialsCalc:
# We need device object to get calibration data
def __init__(self, device):
calibData = device.readCalibration()
# Required information for calculating spatial coordinates on the host
self.monoHFOV = np.deg2rad(calibData.getFov(dai.CameraBoardSocket.LEFT))
# Values
self.DELTA = 5
self.THRESH_LOW = 200 # 20cm
self.THRESH_HIGH = 30000 # 30m
def setLowerThreshold(self, threshold_low):
self.THRESH_LOW = threshold_low
def setUpperThreshold(self, threshold_low):
self.THRESH_HIGH = threshold_low
def setDeltaRoi(self, delta):
self.DELTA = delta
def _check_input(self, roi, frame): # Check if input is ROI or point. If point, convert to ROI
if len(roi) == 4: return roi
if len(roi) != 2: raise ValueError("You have to pass either ROI (4 values) or point (2 values)!")
# Limit the point so ROI won't be outside the frame
self.DELTA = 5 # Take 10x10 depth pixels around point for depth averaging
x = min(max(roi[0], self.DELTA), frame.shape[1] - self.DELTA)
y = min(max(roi[1], self.DELTA), frame.shape[0] - self.DELTA)
return (x-self.DELTA,y-self.DELTA,x+self.DELTA,y+self.DELTA)
def _calc_angle(self, frame, offset):
return math.atan(math.tan(self.monoHFOV / 2.0) * offset / (frame.shape[1] / 2.0))
# roi has to be list of ints
def calc_spatials(self, depthFrame, roi, averaging_method=np.mean):
roi = self._check_input(roi, depthFrame) # If point was passed, convert it to ROI
xmin, ymin, xmax, ymax = roi
# Calculate the average depth in the ROI.
depthROI = depthFrame[ymin:ymax, xmin:xmax]
inRange = (self.THRESH_LOW <= depthROI) & (depthROI <= self.THRESH_HIGH)
averageDepth = averaging_method(depthROI[inRange])
centroid = { # Get centroid of the ROI
'x': int((xmax + xmin) / 2),
'y': int((ymax + ymin) / 2)
}
midW = int(depthFrame.shape[1] / 2) # middle of the depth img width
midH = int(depthFrame.shape[0] / 2) # middle of the depth img height
bb_x_pos = centroid['x'] - midW
bb_y_pos = centroid['y'] - midH
angle_x = self._calc_angle(depthFrame, bb_x_pos)
angle_y = self._calc_angle(depthFrame, bb_y_pos)
spatials = {
'z': averageDepth,
'x': averageDepth * math.tan(angle_x),
'y': -averageDepth * math.tan(angle_y)
}
return spatials, centroid
其他相关的示例:
- StereoDepth node API
- SpatialLocationCalculator node api
- SpatialLocationCalculator
- RGB & TinyYolo with spatial data
- RGB & MobilenetSSD with spatial data
- Mono & MobilenetSSD with spatial data
▌参考资料
https://docs.oakchina.cn/en/latest/
https://www.oakchina.cn/selection-guide/
OAK中国
| OpenCV AI Kit在中国区的官方代理商和技术服务商
| 追踪AI技术和产品新动态
戳「+关注」获取最新资讯↗↗