• DepthAI-v2
  • How can I crop, then resize that cropped area (scale it bigger)

I'm using ImageManip's setCropRect() to move my images start to a specified area, then using setResize to ensure its 450x450

I need to expand that 450x450 to 640x640 for my neural net. Anything I've tried just sets the image to 640x640 by increasing the crop (not by taking the existing 450x450 crop and expanding it to fill). I don't want to pick up things on the side that increasing the crop to 640x640 brings

jakaskerl How can I resize the first one with the second manip?

In my current code, the second imageManip (resize_rgb) is simply resizing the original image. Is there a way I can pass the output of the first image manip (crop_rgb) to resize_rgb?

My code:

from pathlib import Path
import depthai
import cv2
import numpy as np
import sys

pipeline = depthai.Pipeline()
cam_rgb = pipeline.createColorCamera()
cam_rgb.setPreviewSize(1920, 1080)  
cam_rgb.setInterleaved(False)
maxFrameSize = cam_rgb.getPreviewHeight() * cam_rgb.getPreviewWidth() * 3

crop_rgb = pipeline.createImageManip()
crop_rgb.initialConfig.setCropRect(325/1080, 367/1920, 1, 1)
crop_rgb.initialConfig.setResize(450, 450)
cam_rgb.preview.link(crop_rgb.inputImage)

xout_cropped = pipeline.createXLinkOut()
xout_cropped.setStreamName("cropped")
crop_rgb.out.link(xout_cropped.input)
crop_rgb.setMaxOutputFrameSize(maxFrameSize)

resize_rgb = pipeline.createImageManip()
resize_rgb.initialConfig.setResize(640, 640)
cam_rgb.preview.link(resize_rgb.inputImage)

xout_resized = pipeline.createXLinkOut()
xout_resized.setStreamName("resized")
resize_rgb.out.link(xout_resized.input)
resize_rgb.setMaxOutputFrameSize(maxFrameSize)

device = depthai.Device(pipeline)
q_resize = device.getOutputQueue("resized")
q_cropped = device.getOutputQueue("cropped") 

while True:
    in_cropped = q_cropped.tryGet()
    in_resize = q_resize.tryGet()

    if in_cropped is not None:
        stream = in_cropped.getCvFrame()
        if stream is not None:
            cv2.imshow("cropped", stream)
    
    if in_resize is not None:
        frame = in_resize.getCvFrame()
        if frame is not None:
            cv2.imshow("resized", frame)

    if cv2.waitKey(1) == ord('q'):
            break

Follow up: I figured out how to do this but have a new problem

My resized image keeps flickering between what I was previously seeing (a 640x640 of the original full size frame) and what I currently want (640x640 of the area I cropped and resized)

Any ideas why? The code:

from pathlib import Path
import depthai
import cv2
import numpy as np
import sys

pipeline = depthai.Pipeline()
cam_rgb = pipeline.createColorCamera()
cam_rgb.setPreviewSize(1920, 1080)  
cam_rgb.setInterleaved(False)
maxFrameSize = cam_rgb.getPreviewHeight() * cam_rgb.getPreviewWidth() * 3

crop_rgb = pipeline.createImageManip()
crop_rgb.initialConfig.setCropRect(325/1080, 367/1920, 1, 1)
crop_rgb.initialConfig.setResize(450, 450)
cam_rgb.preview.link(crop_rgb.inputImage)

resize_rgb = pipeline.createImageManip()
resize_rgb.initialConfig.setResize(640, 640)
cam_rgb.preview.link(resize_rgb.inputImage)

xout_cropped = pipeline.createXLinkOut()
xout_cropped.setStreamName("cropped")
crop_rgb.out.link(xout_cropped.input)
crop_rgb.out.link(resize_rgb.inputImage)
crop_rgb.setMaxOutputFrameSize(maxFrameSize)

xout_resized = pipeline.createXLinkOut()
xout_resized.setStreamName("resized")
resize_rgb.out.link(xout_resized.input)
resize_rgb.setMaxOutputFrameSize(maxFrameSize)

device = depthai.Device(pipeline)
q_resize = device.getOutputQueue("resized")
q_cropped = device.getOutputQueue("cropped") 

while True:
    in_cropped = q_cropped.tryGet()
    in_resize = q_resize.tryGet()

    if in_cropped is not None:
        stream = in_cropped.getCvFrame()
        if stream is not None:
            cv2.imshow("cropped", stream)
    
    if in_resize is not None:
        frame = in_resize.getCvFrame()
        if frame is not None:
            cv2.imshow("resized", frame)

    if cv2.waitKey(1) == ord('q'):
            break

Follow up to this message for anyone reading this - I removed cam_rgb.preview.link(resize_rgb.inputImage) and this removed the flicker issue.

My updated code:

import depthai
import cv2

pipeline = depthai.Pipeline()
cam_rgb = pipeline.createColorCamera()
cam_rgb.setPreviewSize(1920, 1080)  
cam_rgb.setInterleaved(False)
maxFrameSize = cam_rgb.getPreviewHeight() * cam_rgb.getPreviewWidth() * 3

crop_rgb = pipeline.createImageManip()
crop_rgb.initialConfig.setCropRect(325/1080, 367/1920, 1, 1)
crop_rgb.initialConfig.setResize(450, 450)
cam_rgb.preview.link(crop_rgb.inputImage)

resize_rgb = pipeline.createImageManip()
resize_rgb.initialConfig.setResize(640, 640)

xout_cropped = pipeline.createXLinkOut()
xout_cropped.setStreamName("cropped")
crop_rgb.out.link(xout_cropped.input)
crop_rgb.out.link(resize_rgb.inputImage)
crop_rgb.setMaxOutputFrameSize(maxFrameSize)

xout_resized = pipeline.createXLinkOut()
xout_resized.setStreamName("resized")
resize_rgb.out.link(xout_resized.input)
resize_rgb.setMaxOutputFrameSize(maxFrameSize)

device = depthai.Device(pipeline)
q_resize = device.getOutputQueue("resized")
q_cropped = device.getOutputQueue("cropped")

while True:
    in_cropped = q_cropped.tryGet()
    in_resize = q_resize.tryGet()

    if in_cropped is not None:
        stream = in_cropped.getCvFrame()
        if stream is not None:
            cv2.imshow("cropped", stream)
    
    if in_resize is not None:
        frame = in_resize.getCvFrame()
        if frame is not None:
            cv2.imshow("resized", frame)

    if cv2.waitKey(1) == ord('q'):
            break