Jayabalaji V
Jayabalaji V

Reputation: 79

How to convert mat to array2d<rgb_pixel>?

I created dll for the dlib face landmark code there used array2d to get the image, but i like to read an image using Mat and to convert to array2d because dlib supports only array2d. Can any one say how to convert mat to array2d ??

Upvotes: 4

Views: 14079

Answers (3)

mohaghighat
mohaghighat

Reputation: 1343

Convert a cv::Mat image to dlib::array2d:

In case of a BGR image, you can follow this:

dlib::array2d<dlib::bgr_pixel> dlibImage;
dlib::assign_image(dlibImage, dlib::cv_image<dlib::bgr_pixel>(cvMatImage));

And, if you have a grayscale image, simply use <unsigned char> instead of <bgr_pixel>:

dlib::array2d<unsigned char> dlibImageGray;
dlib::assign_image(dlibImageGray, dlib::cv_image<unsigned char>(cvMatImageGray));

Upvotes: 8

user5751425
user5751425

Reputation:

Firstly convert the Mat to cv_image of dlib. Then using the assign_image() of dlib you can convert the cv_image to array2d.

Upvotes: 2

berak
berak

Reputation: 39796

#include "opencv2/core/core_c.h" // shame, but needed for using dlib
#include <dlib/image_processing.h>
#include <dlib/opencv/cv_image.h>

dlib::shape_predictor sp;
dlib::deserialize(path_to_landmark_model) >> sp;

cv::Rect r;
cv::Mat I;
dlib::rectangle rec(r.x, r.y, r.x+r.width, r.y+r.height);
dlib::full_object_detection shape = sp(dlib::cv_image<uchar>(I), rec);

Upvotes: 1

Related Questions