Dlib: How to transform the type of cv::Mat to matrix<rgb_pixel>?

Created on 28 Aug 2016  Â·  8Comments  Â·  Source: davisking/dlib

Hi, the dnn_imagenet_ex.cpp example in line 141 load image as a matrix. I want to read the image by OpenCV, and I faced the problem to transform the type of cv::Mat to matrix. Anyone who knows how to deal with the problem?

Most helpful comment

cv_image<bgr_pixel> image(mat);
matrix<rgb_pixel> matrix;
assign_image(matrix, image);

All 8 comments

@davisking Does not work.

../../src/Detector.cpp:266:59: required from here ../../src/dlib/dnn/core.h:2277:65: error: ‘const class dlib::cv_image<dlib::rgb_pixel>’ has no member named ‘begin’ std::vector<output_label_type> results(std::distance(data.begin(), data.end())); ^ ../../src/dlib/dnn/core.h:2277:65: error: ‘const class dlib::cv_image<dlib::rgb_pixel>’ has no member named ‘end’ ../../src/dlib/dnn/core.h:2279:33: error: ‘const class dlib::cv_image<dlib::rgb_pixel>’ has no member named ‘begin’ auto i = data.begin(); ^

dlib::matrix<dlib::rgb_pixel> img(dlib::mat(dlib::cv_image<dlib::rgb_pixel>(cvRgbImage)));

works but it is long way to go.

cv_image<bgr_pixel> image(mat);
matrix<rgb_pixel> matrix;
assign_image(matrix, image);

******** FATAL ERROR DETECTED ********

Error detected at line 36.
Error detected in file /home/server/dlib/dlib/../dlib/opencv/cv_image.h.
Error detected in function dlib::cv_image::cv_image(cv::Mat) [with pixel_type = dlib::bgr_pixel].

Failing expression was img.depth() == cv::DataType::basic_pixel_type>::depth && img.channels() == pixel_traits::num.
The pixel type you gave doesn't match pixel used by the open cv Mat object.
img.depth(): 0
img.cv::DataType::basic_pixel_type>::depth: 0
img.channels(): 1
img.pixel_traits::num: 3


Aborted (core dumped)

There is a double copy in the following code mentioned above. Can we form a matrix object from open cv mat(in rgb already) to dlib matrix without incurring the memory copy here in assign_image?

This situation is similar to the way CV Mat objects can be created by setting the data pointer and the row & col detalils directly without incurring data copy.

cv_image image(mat);
matrix matrix;
assign_image(matrix, image);

There is only one memory copy in this code:

cv_image<bgr_pixel> image(open_cv_mat);
matrix<rgb_pixel> matrix;
assign_image(matrix, image);  // memory copy happens here and only here

If you want to make a matrix that aliases the memory of mat then do this:

cv_image<bgr_pixel> image(open_cv_mat);
some_function_that_takes_dlib_matrix(mat(image))

dlib::mat() converts basically anything into a dlib matrix expression that aliases the memory of whatever you pass it. It does not copy anything.

Yo're correct about one memory copy. I didnt post our code where there is a copy from our data structur to cv::mat first and then an assign_image as indicated above.

I will check your suggestion and post the results.

Thanks.

It gives error dlib::cv_image not a member of dlib

Was this page helpful?
0 / 5 - 0 ratings