How to use blobFromImagesWithParams

My code

    img = imread(samples::findFile("C:/data/VOC2012/JPEGImages/2007_002227.jpg"));
    Image2BlobParams paramTF2cedn;
    paramTF2cedn.datalayout = DNN_LAYOUT_NHWC;
    paramTF2cedn.ddepth = CV_32F;
    paramTF2cedn.mean = (128, 128, 128);
    paramTF2cedn.scalefactor = 1 / 128.;
    paramTF2cedn.size = Size(224, 224);
    paramTF2cedn.swapRB = false;
    paramTF2cedn.paddingmode = DNN_PMODE_NULL;
    Mat my_inp = dnn::blobFromImagesWithParams(img, paramTF2cedn);

Now prepare inference


    my_net.setInput(my_inp);
    Mat my_ouput2 = my_net.forward();

Now inference takes too much time…
I check my_inp

At line

blobshape is

|-||blobShape|{ size=4 }|std::vector<int,std::allocator<int>>|
|||[capacity]|4|unsigned __int64|
|+||[allocator]|allocator|std::_Compressed_pair<std::allocator<int>,std::_Vector_val<std::_Simple_types<int>>,1>|
|||[0]|333|int|
|||[1]|224|int|
|||[2]|224|int|
|||[3]|3|int|
|+||[Raw View]|{_Mypair=allocator }|std::vector<int,std::allocator<int>>|

Why 333? I have got only one image and image size 500x333 x 3

333x224x224x3 = 50 125 824
500x800x3 = 1 200 000

hi there,

what is this ? kindly explain.

also, for a single image it should be blobFromImageWithParams() (not Images), i guess, no ?
as of now, there might be an additional (vector) dimension missing in your input

1 Like

blob shape is 333 x 224 x 224 x 3
my image is 500 x 800 x 3

I expect my blobshape is 1 x 224 x 224 x 3
Inference is too long of course forward there is 333 inference

this is working

    int sz[] = { 1,224,224, 3 }
    img.convertTo(imgf, CV_32F);
    imgf = (imgf - 128) / 128;
    Mat blob(4, sz,  CV_32F, imgf.ptr(0));
    my_net.setInput(blob);
    if (my_net.empty())
        cout << "Oops";
    Mat my_ouput1 = my_net.forward(), imgOut1;

and blobshape is 1 x 224 x 224 x 3

1 Like

nice to hear, that the ‘simple’ solution works here !

what about

Mat my_inp = dnn::blobFromImagesWithParams(vector<Mat>(1,img), paramTF2cedn);

?

You are right i made a mistake
I use dnn::blobFromImagesWithParams instead of dnn::blobFromImageWithParams

may be some check type must be done in blobFromImagesWithParams

I try a PR with

if (images_.kind() != _InputArray::STD_VECTOR_MAT && images_.kind() != _InputArray::STD_ARRAY_MAT &&
    images_.kind() != _InputArray::STD_VECTOR_VECTOR) {
    String error_message = "The data is expected as InputArray::STD_VECTOR_MAT (a std::vector<Mat>) or _InputArray::STD_VECTOR_VECTOR (a std::vector< std::vector<...> >).";
    CV_Error(Error::StsBadArg, error_message);
}
3 Likes

I think this is a good idea. Missing just a single “s” in function name leads to silently changed behaviour. Cannot think up a usecase when using blobFromImages may accept a single Mat as a container for multiple images.

2 Likes