opencv Mat和Dlib变量类型转换

匆匆过客 提交于 2020-08-20 07:33:27

Mat转matrix<dlib::rgb_pixel>

cv::Mat frame = cv::imread("test.jpg");
matrix<dlib::rgb_pixel> img;
assign_image(img,cv_image<rgb_pixel>(frame));

array2d<dlib::bgr_pixel>转Mat
方法一:

dlib::array2d<dlib::bgr_pixel> img_bgr;//使用dlib载入彩色的RGB图像      
dlib::load_image(img_bgr, "test.jpg");   
cv::Mat img = dlib::toMat(img_bgr);

方法二:

cv::Mat temp_Roi;
temp_Roi=imread("test.jpg");
array2d< bgr_pixel> img(temp_Roi.rows, temp_Roi.cols);
for (int i = 0; i < temp_Roi.rows; i++)
{
    for (int j = 0; j < temp_Roi.cols; j++)
    {
        img[i][j].blue = temp_Roi.at< cv::Vec3b>(i, j)[0];
        img[i][j].green = temp_Roi.at< cv::Vec3b>(i, j)[1];
        img[i][j].red = temp_Roi.at< cv::Vec3b>(i, j)[2];
    }
}

Mat转cv_image<rgb_pixel>

cv::Mat img = cv::imread("test.jpg")  
dlib::cv_image<rgb_pixel> dlib_img(img); 

Mat转cv_image<bgr_pixel>

cv::Mat temp = cv::imread("test.jpg");
dlib::cv_image<bgr_pixel> cimg(temp);

Opencv的rect转dlib的rect

dlib::rectangle dlibRect((long)opencvRect.tl().x, (long)opencvRect.tl().y, (long)opencvRect.br().x - 1, (long)opencvRect.br().y - 1);

Mat转dlib array2d

#include "dlib/opencv/cv_image.h"
#include <opencv2/opencv.hpp>  
// RGB彩色图,RGB类型有 rgb_pixel和bgr_pixel两种,dlib要想不失真的转到OpenCV中,应该使用bgr_pixel,因为OpenCV中的颜色也是以“B,G,R”通道顺序排列的。  
cv::Mat ori_mat = cv::imread("test.jpg");
dlib::array2d<dlib::bgr_pixel> dlib_image;  
dlib::assign_image(dlib_image, dlib::cv_image<dlib::bgr_pixel>(ori_mat));  
// 灰度图
cv::Mat photo_gray = cv::imread("gray.jpg");  
dlib::array2d<unsigned char> dlibImageGray;  
dlib::assign_image(dlibImageGray, dlib::cv_image<unsigned char>(photo_gray));

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!