流程
源代码
Python
PythonGitHub
1import depthai as dai
2import cv2
3import numpy as np
4import time
5
6def draw_rotated_rectangle(frame, center, size, angle, color, thickness=2):
7 """
8 Draws a rotated rectangle on the given frame.
9
10 Args:
11 frame (numpy.ndarray): The image/frame to draw on.
12 center (tuple): The (x, y) coordinates of the rectangle's center.
13 size (tuple): The (width, height) of the rectangle.
14 angle (float): The rotation angle of the rectangle in degrees (counter-clockwise).
15 color (tuple): The color of the rectangle in BGR format (e.g., (0, 255, 0) for green).
16 thickness (int): The thickness of the rectangle edges. Default is 2.
17 """
18 # Create a rotated rectangle
19 rect = ((center[0], center[1]), (size[0], size[1]), angle)
20
21 # Get the four vertices of the rotated rectangle
22 box = cv2.boxPoints(rect)
23 box = np.intp(box) # Convert to integer coordinates
24
25 # Draw the rectangle on the frame
26 cv2.polylines(frame, [box], isClosed=True, color=color, thickness=thickness)
27
28def processDepthFrame(depthFrame: dai.ImgFrame):
29 return dai.utility.colorizeDepthFrame(depthFrame, colormap=cv2.COLORMAP_HOT).getCvFrame()
30
31with dai.Pipeline() as pipeline:
32 color = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_A)
33 monoLeft = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B)
34 monoRight = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C)
35 stereo = pipeline.create(dai.node.StereoDepth)
36
37 stereo.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.DEFAULT)
38 # stereo.setDepthAlign(dai.CameraBoardSocket.CAM_A)
39 # stereo.setOutputSize(640, 400)
40
41 colorCamOut = color.requestOutput((640, 480))
42
43 monoLeftOut = monoLeft.requestOutput((640, 480))
44 monoRightOut = monoRight.requestOutput((640, 480))
45
46 monoLeftOut.link(stereo.left)
47 monoRightOut.link(stereo.right)
48
49 colorOut = colorCamOut.createOutputQueue()
50 rightOut = monoRightOut.createOutputQueue()
51 stereoOut = stereo.depth.createOutputQueue()
52
53 pipeline.start()
54 lastPrintTime = 0.0
55 while pipeline.isRunning():
56 colorFrame = colorOut.get()
57 stereoFrame = stereoOut.get()
58
59 assert colorFrame.validateTransformations()
60 assert stereoFrame.validateTransformations()
61
62 clr = colorFrame.getCvFrame()
63 depth = processDepthFrame(stereoFrame)
64
65 rect = dai.RotatedRect(dai.Point2f(300, 200), dai.Size2f(200, 100), 10)
66 remappedRect = colorFrame.getTransformation().remapRectTo(stereoFrame.getTransformation(), rect)
67
68 now = time.monotonic()
69 if now - lastPrintTime >= 1.0:
70 print(f"Original rect x: {rect.center.x} y: {rect.center.y} width: {rect.size.width} height: {rect.size.height} angle: {rect.angle}")
71 print(f"Remapped rect x: {remappedRect.center.x} y: {remappedRect.center.y} width: {remappedRect.size.width} height: {remappedRect.size.height} angle: {remappedRect.angle}")
72 lastPrintTime = now
73
74 draw_rotated_rectangle(clr, (rect.center.x, rect.center.y), (rect.size.width, rect.size.height), rect.angle, (255, 0, 0))
75 draw_rotated_rectangle(depth, (remappedRect.center.x, remappedRect.center.y), (remappedRect.size.width, remappedRect.size.height), remappedRect.angle, (255, 0, 0))
76
77 cv2.imshow("color", clr)
78 cv2.imshow("depth", depth)
79
80 if cv2.waitKey(1) == ord('q'):
81 break
82 pipeline.stop()需要帮助?
请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。