OpenCV dnn blob issues

Hi Community,
I’m working on a project, where I’m trying to convert my image into openCV blob using dnn library of openCV, but I’m getting unusual blob from the function.

#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>

int main(){
    cv::Mat img = cv::imread("/home/vimal/Edge_ai/Vstream/includes/test.jpeg"); 

    if(img.channels() == 3)
        std::cout << "channels are good" << std::endl; 
    cv::Mat blob; 
    cv::dnn::blobFromImage(img, blob, 1./255., cv::Size(640, 640), cv::Scalar(), true, false);

    std::cout << blob.channels() << std::endl; 
    std::cout << blob.rows << std::endl; 
    std::cout << blob.cols << std::endl; 

    return 0;


but i’m getting the output as,

channels are good




kindly help me in this

rows & cols only make sense for 2 dim Mats.
blobs are multidimensional, and have those members set to -1

please use dims' and size` members (w/o braces !) to inspect:

std::cout << blob.dims << std::endl;
std::cout << blob.size << std::endl;

@berak thanks a lot, it’s perfect now

I’ve got another error followed by that

    cv::Mat modelInput = format2sq(img);

    cv::Mat blob;
    cv::dnn::blobFromImage(modelInput, blob, 1.0/255.0, cv::Size(640, 640), cv::Scalar(), true, false);

    std::vector<cv::Mat> outputs;
    net.forward(outputs, net.getUnconnectedOutLayersNames());

    return blob;

C++ exception with description "OpenCV(4.8.0-dev) /home/vimal/opencv/modules/dnn/src/net.cpp:104: error: (-215:Assertion failed) !empty() in function ‘forward’
" thrown in the test body.

can anyplease help me out here ? @berak

your net is empty / invalid.

how (on earth …) did you construct that ?

you MUST read in a pretrained model to use the dnn’s forward()