How to make multiple HWC images into a batch

Hi,
I used to use python version of opencv and now I’m switching to c++.
In python, it is easy to make multiple 3d images into a single 4d array which is a batch. Here is the python code imagining that I have 2 images (224 * 224 * 3).

img0 = cv2.imread("img0.jpg")
img0 = np.array(img0, dtype=np.float32)
img1 = cv2.imread("img1.jpg")
img1 = np.array(img1, dtype=np.float32)
n, h, w, c = 2, 224, 224, 3
images = np.ndarray(shape=(n, h, w, c), dtype=np.float32)
images[0] = img0
images[1] = img1

Now how to convert the above code to c++? I’m really new to c++.
Thanks a lot.

Best,

put your images into a vector<Mat> and use blobFromImages()

Hi @berak
Thanks for the reply.
I found the docs says blobFromImages() will return NCHW Mat but what i need is NHWC. Does this API support NHWC?

1 Like

apologies, misread your post, then.

in this case, you might try like:

int H=224, W=224, C=3, N=???;

int sz[] = {N,H,W,C};
Mat blob(4,sz, CV_32F); // preallocated
for (int n=0; n<N; n++) {
    // get image n, convert to float, resize to W,H
    Mat img =  ....
    // copy destination
    Mat slice(H,W,CV_32FC3, blob.ptr(n));
    img.copyTo(slice);
}

Thanks @berak ,
I’ll have a try of this.

best,