转自:https://blog.csdn.net/guyuealian/article/details/77482549
在Dlib库中图像存储是使用array2d类型,而在OpenCV是使用Mat类型,Dlib中提供了#include <dlib/opencv.h>,可实现dlib array2d与 OpenCV Mat的互转。其中toMat对象可将dlib的图像转为OpenCV的Mat类型,而cv_image对象可将OpenCV的Mat类型转为dlib类型的图像。详见官网:http://dlib.net/imaging.html#rgb_pixel
(1)dlib载入灰度图像:
-
dlib::array2d< unsigned char> img_gray; //使用dlib载入灰度图像
-
dlib::load_image(img_gray, "test_image.jpg");
-
-
-
cv::Mat img = dlib::toMat(img_gray); //灰度图
若要将OpenCV Mat转为dlib,可以这样:
-
-
-
cv::Mat img = cv::imread( "test_image.jpg")
-
dlib::cv_image< unsigned char> dlib_img(img); // only stores pointer, no deep copy
(2)使用dlib载入彩色的RGB图像:
-
dlib::array2d<dlib::rgb_pixel> img_rgb; //使用dlib载入彩色的RGB图像
-
dlib::load_image(img_rgb, "test_image.jpg");
cv::Mat img = dlib::toMat(img_rgb);
转换后,图像显示会出现严重的颜色失真现象,如下图所示,左图是dlib显示的img_rgb图像,右图是OpenCV显示的img图像
后来官网查了一下,原来dlib有多种图像类型;http://dlib.net/imaging.html
- RGB
- There are two RGB pixel types in dlib,
- The
- The
- The
- Any built in scalar type may be used as a grayscale pixel type. For example, unsigned char, int, double, etc.
-
dlib::array2d<dlib::bgr_pixel> img_bgr; //使用dlib载入彩色的RGB图像
-
dlib::load_image(img_bgr, "test_image.jpg");
-
cv::Mat img = dlib::toMat(img_bgr);