CV-CUDA
CV-CUDA copied to clipboard
how can i get ROI from nvcv.ImageBatchVarShape
how can i get ROI from nvcv.ImageBatchVarShape, like: x_roi = x[y1:x1, y1+height, x1 + width], or x[y1:x1, y1+height, x1 + width] = x_roi...
Hi @lokvke, Thank you very much for your interest in CVCUDA!
Could you share some information on your use-case? Eg. would you like to get a ROI of the same size from an ImageBatchVarShape or would it be a different size per ImageBatchVarShape element?
Hi @lokvke, Thank you very much for your interest in CVCUDA!
Could you share some information on your use-case? Eg. would you like to get a ROI of the same size from an ImageBatchVarShape or would it be a different size per ImageBatchVarShape element?
for example, i want to put the small_imgs into large_imgs' position (100, 100)
large_imgs = nvcv.ImageBatchVarShape(100) # shape: (100, 1280, 720, 3)
small_imgs = nvcv.ImageBatchVarShape(100) # shape: (100, 120, 120, 3)
x, y = (100, 100) # (x, y)
width, height = 120, 120
large_imgs[y:y+height, x:x+width] = small_imgs
Hi @lokvke,
then you could you the padandstack operator, which gets as an input an ImageBatchVarSahpe and outputs a Tensor of the cropped size. Below is a minimal working example
import cupy as cp
import numpy as np
import cvcuda
#dimensions
N = 100
height = 1280
width = 720
channels = 3
crop_height = 120
crop_width = 120
left = 100
top = 100
# create imagebatchvarshape
d_imgs = cvcuda.ImageBatchVarShape(N)
d_imgs.pushback([
cvcuda.as_image(cp.array(np.random.rand(height, width, channels)))
for _ in range(N)
])
# create output tensor
d_outs = cvcuda.Tensor(
(N, crop_height, crop_width, channels),
np.float32,
"NHWC",
)
top_list = [-top] * N # negative values do crop and positive do pad
left_list = [-left] * N # negative values do crop and positive do pad
# device side
d_top = cvcuda.as_tensor(
cp.array(top_list, dtype=np.int32).reshape(1, 1, len(top_list), 1), "NHWC")
d_left = cvcuda.as_tensor(
cp.array(left_list, dtype=np.int32).reshape(1, 1, len(left_list), 1), "NHWC")
cvcuda.padandstack_into(dst=d_outs, src=d_imgs, top=d_top, left=d_left)
Hi @lokvke,
then you could you the padandstack operator, which gets as an input an ImageBatchVarSahpe and outputs a Tensor of the cropped size. Below is a minimal working example
import cupy as cp import numpy as np import cvcuda #dimensions N = 100 height = 1280 width = 720 channels = 3 crop_height = 120 crop_width = 120 left = 100 top = 100 # create imagebatchvarshape d_imgs = cvcuda.ImageBatchVarShape(N) d_imgs.pushback([ cvcuda.as_image(cp.array(np.random.rand(height, width, channels))) for _ in range(N) ]) # create output tensor d_outs = cvcuda.Tensor( (N, crop_height, crop_width, channels), np.float32, "NHWC", ) top_list = [-top] * N # negative values do crop and positive do pad left_list = [-left] * N # negative values do crop and positive do pad # device side d_top = cvcuda.as_tensor( cp.array(top_list, dtype=np.int32).reshape(1, 1, len(top_list), 1), "NHWC") d_left = cvcuda.as_tensor( cp.array(left_list, dtype=np.int32).reshape(1, 1, len(left_list), 1), "NHWC") cvcuda.padandstack_into(dst=d_outs, src=d_imgs, top=d_top, left=d_left)
Thank you for your reply, the code is about to crop a small roi from the source images. What if i want to paste small tensors or ImageBatchVarSahpe to the source ImageBatchVarSahpe, which operator can help?