# How to make multiple HWC images into a batch

**URL:** <https://forum.opencv.org/t/how-to-make-multiple-hwc-images-into-a-batch/4669>\
**Category:** C++\
**Created:** [August 9, 2021, 3:23am UTC](https://forum.opencv.org/t/how-to-make-multiple-hwc-images-into-a-batch/4669 "2021-08-09T03:23:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zihaog](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@zihaog](https://forum.opencv.org/u/zihaog)\
**Post date:** [August 9, 2021, 3:23am UTC](https://forum.opencv.org/t/how-to-make-multiple-hwc-images-into-a-batch/4669/1 "2021-08-09T03:23:18Z")

</div>

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).

```auto
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,

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 9, 2021, 6:23am UTC](https://forum.opencv.org/t/how-to-make-multiple-hwc-images-into-a-batch/4669/2 "2021-08-09T06:23:55Z")

</div>

put your images into a `vector<Mat>` and use [blobFromImages()](https://docs.opencv.org/master/d6/d0f/group__dnn.html#ga0b7b7c3c530b747ef738178835e1e70f)

---

<div class="post-metadata">

**Author:** ![zihaog](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@zihaog](https://forum.opencv.org/u/zihaog)\
**Post date:** [August 9, 2021, 7:19am UTC](https://forum.opencv.org/t/how-to-make-multiple-hwc-images-into-a-batch/4669/3 "2021-08-09T07:19:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 9, 2021, 7:35am UTC](https://forum.opencv.org/t/how-to-make-multiple-hwc-images-into-a-batch/4669/4 "2021-08-09T07:35:14Z")

</div>

> [@zihaog](#):
>
> blobFromImages() will return NCHW Mat but what i need is NHWC.

apologies, misread your post, then.

in this case, you might try like:

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

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![zihaog](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@zihaog](https://forum.opencv.org/u/zihaog)\
**Post date:** [August 9, 2021, 7:38am UTC](https://forum.opencv.org/t/how-to-make-multiple-hwc-images-into-a-batch/4669/5 "2021-08-09T07:38:26Z")

</div>

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

best,
