【问题标题】:How to convert Mat to matrix of dlib如何将 Mat 转换为 dlib 的矩阵
【发布时间】:2017-10-26 16:48:06
【问题描述】:

我使用 CNN 创建程序,我需要在其中插入 192 个通道的矩阵。这个矩阵(宽度:32,高度:240,通道:192,类型:uchar)存储在 Mat(OpenCV)中。如何将 Mat 转换为 dlib 矩阵?

std::vector<dlib::matrix<?>> training_data;
std::vector<unsigned long> training_labels;
...
Mat mat = loader.getMat();
? convert ?
training_data.push_back(dlib_matrix);
...
trainer.train(training_data, training_labels);

【问题讨论】:

    标签: opencv matrix data-conversion dlib n-dimensional


    【解决方案1】:

    您可以使用cv_image 将 Mat 转换为 dlib 图像,使用 dlib::toMat 将 dlib 转换为 Mat。

    //Mat to dlib image
    cv_image<bgr_pixel> dlib_img(mat);
    

    编辑:

    据我所知,对于 n 通道 Mats,您必须提供自定义 pixel_traits。例如,对于 5 通道 Mat 图像,您可以这样做:

    namespace dlib{
    struct custom_pixel
        {
            custom_pixel (
            ) {}
    
            custom_pixel (
                unsigned char c1_,
                unsigned char c2_,
                unsigned char c3_,
                unsigned char c4_,
                unsigned char c5_
            ) : c1(c1_), c2(c2_), c3(c3_), c4(c4_), c5(c5_) {}
    
            unsigned char c1;
            unsigned char c2;
            unsigned char c3;
            unsigned char c4;
            unsigned char c5;
        };
    
    template <>
        struct pixel_traits<custom_pixel>
        {
            constexpr static bool rgb  = false;
            constexpr static bool rgb_alpha  = false;
            constexpr static bool grayscale = false;
            constexpr static bool hsi = false;
            constexpr static bool lab = false;
            enum { num = 5};// provide number of channels here
            typedef unsigned char basic_pixel_type; //provide channel depth here
            static basic_pixel_type min() { return 0;}
            static basic_pixel_type max() { return 255;}
            constexpr static bool is_unsigned = true;
            constexpr static bool has_alpha = false;
        };
    }
    

    然后从 Mat 转换为 dlib,反之亦然:

    int main(int argc, char** argv)
    {
        // from opencv to dlib
        Mat mat_img = Mat::zeros(3, 3, CV_8UC(5));
        cv_image<custom_pixel> dlib_img(mat_img);
    
        //from dlib to opencv
        Mat mat_img_new = dlib::toMat(dlib_img);
    }
    

    【讨论】:

    • 上面你写的不适用,因为bgr_pixel是3通道图像,我需要转换为192通道的图像/矩阵。
    • 我尝试了类似的方法,但 dlib 需要下一步。以上用于转换为 cv_image,但我需要转换为 dlib::matrix。当我尝试时: cv_image cimg(mat);matrix dm;assign_image(dm, cimg);这将导致错误:没有匹配函数调用'assign(dlib::big_pixel&, const dlib::big_pixel&)') {assign_pixel_helpers::assign(dest,src); }
    猜你喜欢
    • 2016-12-10
    • 1970-01-01
    • 2015-06-11
    • 2021-01-27
    • 2012-07-17
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多