How pass RGB image Mat to keras(ONNX) model with channels_last data format?

assuming, you got the image from imread(),
if your network input requires 3 channel input – leave it as-is.
if it needs 4, wrap another Mat header around the data, like:

img.convertTo(img, CV_32F, 1.0/255);

int sz[] = {1, img.rows, img.cols, img.channels()};
Mat blob(4, sz, CV_32F, img.data);

just avoid the dnn::imageToBlob() function,
which will split the image planes to NCHW format !

1 Like