Software Stack
DepthAI

ON THIS PAGE

  • ImageManip remap
  • Setup
  • Pipeline
  • Source code

ImageManip remap

This examples shows an application that processes a camera stream through two image manipulation nodes with 90-degree rotations and different output sizes, displaying the original and manipulated frames with synchronized rotated rectangles, using DepthAI's pipeline and transformation handling.

Setup

This example requires the DepthAI v3 API, see installation instructions.

Pipeline

Source code

Python
C++

Python

Python
GitHub
1import depthai as dai
2import cv2
3import numpy as np
4
5def draw_rotated_rectangle(frame, center, size, angle, color, thickness=2):
6    """
7    Draws a rotated rectangle on the given frame.
8
9    Args:
10        frame (numpy.ndarray): The image/frame to draw on.
11        center (tuple): The (x, y) coordinates of the rectangle's center.
12        size (tuple): The (width, height) of the rectangle.
13        angle (float): The rotation angle of the rectangle in degrees (counter-clockwise).
14        color (tuple): The color of the rectangle in BGR format (e.g., (0, 255, 0) for green).
15        thickness (int): The thickness of the rectangle edges. Default is 2.
16    """
17    # Create a rotated rectangle
18    rect = ((center[0], center[1]), (size[0], size[1]), angle)
19
20    # Get the four vertices of the rotated rectangle
21    box = cv2.boxPoints(rect)
22    box = np.intp(box)  # Convert to integer coordinates
23
24    # Draw the rectangle on the frame
25    cv2.polylines(frame, [box], isClosed=True, color=color, thickness=thickness)
26
27with dai.Pipeline() as pipeline:
28    cam = pipeline.create(dai.node.Camera).build()
29    camOut = cam.requestOutput((640, 400), dai.ImgFrame.Type.BGR888i, fps = 30.0)
30    manip1 = pipeline.create(dai.node.ImageManip)
31    manip2 = pipeline.create(dai.node.ImageManip)
32
33    camOut.link(manip1.inputImage)
34    manip1.out.link(manip2.inputImage)
35
36    manip1.initialConfig.addRotateDeg(90)
37    manip1.initialConfig.setOutputSize(200, 320)
38
39    manip2.initialConfig.addRotateDeg(90)
40    manip2.initialConfig.setOutputSize(320, 200)
41    manip2.setRunOnHost(True)
42
43    outQcam = camOut.createOutputQueue()
44    outQ1 = manip1.out.createOutputQueue()
45    outQ2 = manip2.out.createOutputQueue()
46
47    pipeline.start()
48
49    while True:
50        camFrame: dai.ImgFrame = outQcam.get()
51        manip1Frame: dai.ImgFrame = outQ1.get()
52        manip2Frame: dai.ImgFrame = outQ2.get()
53
54        camCv = camFrame.getCvFrame()
55        manip1Cv = manip1Frame.getCvFrame()
56        manip2Cv = manip2Frame.getCvFrame()
57
58        rect2 = dai.RotatedRect(dai.Rect(dai.Point2f(100, 100), dai.Point2f(200, 150)), 0)
59        rect1 = manip2Frame.getTransformation().remapRectTo(manip1Frame.getTransformation(), rect2)
60        rectcam = manip1Frame.getTransformation().remapRectTo(camFrame.getTransformation(), rect1)
61
62        draw_rotated_rectangle(manip2Cv, (rect2.center.x, rect2.center.y), (rect2.size.width, rect2.size.height), rect2.angle, (255, 0, 0))
63        draw_rotated_rectangle(manip1Cv, (rect1.center.x, rect1.center.y), (rect1.size.width, rect1.size.height), rect1.angle, (255, 0, 0))
64        draw_rotated_rectangle(camCv, (rectcam.center.x, rectcam.center.y), (rectcam.size.width, rectcam.size.height), rectcam.angle, (255, 0, 0))
65
66        cv2.imshow("cam", camCv)
67        cv2.imshow("manip1", manip1Cv)
68        cv2.imshow("manip2", manip2Cv)
69        if cv2.waitKey(1) == ord('q'):
70            break

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.