opencv dlib 图像格式类的转换

dlib opencv各种图像类的转换

IplImage, CvMat, Mat 的关系 相互转换

https://docs.opencv.org/3.1.0/d3/d63/classcv_1_1Mat.html


    Ptr<IplImage> iplimg(cvLoadImage(imagename.c_str())); // Ptr<T> is safe ref-counting pointer class
    if(!iplimg)
    {
        fprintf(stderr, "Can not load image %s\n", imagename.c_str());
        return -1;
    }
    Mat img = cv::cvarrToMat(iplimg); // cv::Mat replaces the CvMat and IplImage, but it's easy to convert
    // between the old and the new data structures (by default, only the header
    // is converted, while the data is shared)


BYTE pBuffer[65536];  //用于保存图像 大小是width*height*3


std::vector<BYTE> pFaceBuffs;

pFaceBuffs.clear();
pFaceBuffs.reserve(len);

//  改成Array  memcopy  memmove 会快很多
for (size_t i = 0; i<len; i++)
{
	pFaceBuffs.push_back(pBuffer[i]);//BMP 准确应该是38862
}

Mat faceIDImg = imdecode(Mat(pFaceBuffs), IMREAD_COLOR);
if (faceIDImg.data == nullptr) { continue; }

m_nIDPhotoWidth = faceIDImg.cols;//身份证默认Width 102
m_nIDPhotoHeight = faceIDImg.rows;//身份证默认Height 126

//vector 转mat

Mat (const std::vector< _Tp > &vec, bool copyData=false)
 
template<typename _Tp , int n>
 	Mat (const Vec< _Tp, n > &vec, bool copyData=true)
 
template<typename _Tp , int m, int n>
 	Mat (const Matx< _Tp, m, n > &mtx, bool copyData=true)
 
template<typename _Tp >
 	Mat (const Point_< _Tp > &pt, bool copyData=true)
 
template<typename _Tp >
 	Mat (const Point3_< _Tp > &pt, bool copyData=true)
 
template<typename _Tp > 

cv::Mat img;

struct rgb_pixel
{
	//WHAT THIS OBJECT REPRESENTS
	//This is a simple struct that represents an RGB colored graphical pixel.
	rgb_pixel(
	) {}

	rgb_pixel(
		unsigned char red_,
		unsigned char green_,
		unsigned char blue_
	) : red(red_), green(green_), blue(blue_) {}

	unsigned char red;
	unsigned char green;
	unsigned char blue;
};

图片格式
640*480
1.若 用rgb_pixel

[0,0]r  [0,0]g  [0,0]b ........[0,640]r  [0,640 ]g  [0,640]b
.
.
.
[480,0]r  [480,0 ]g  [480,0]b ........[480,640]r  [480,640 ]g  [480,640]b

2.如果rgb 各占用一个 640*480*3

r        g      b                        r              g             b
[0,0] [0,1] [0,2] ................[0,3*640] [0,3*640+1] [0,3*640+2] 
.
.
.
[480,0] [480,1] [480,2] ................[480,3*640] [480,3*640+1] [480,3*640+2] 
  1. 一维数组 最常用 img.data();也是这样排列

r    g   b                             r                     g                      b
[0],[1],[2]............... [640*480*3+0],[640*480*3 +1],[640*480*3+2]

4.cv::Mat 存储方式

 img.data();
  1. BYTE[][] RGB 图片转换为 array2d<rgb_pixel> 再转换为 matrix<rgb_pixel>
INT32 Convert2Array2d(BYTE** r, BYTE** g, BYTE** b, UINT32 imgWidth, UINT32 imgHeight,.......)
{
        dlib::array2d<rgb_pixel> img;
	img.set_size(imgWidth, imgHeight);//row 行* col 列

	// loop over all the rows
	for (UINT32 i = 0; i < imgWidth; i++)
	{
		// loop over all the columns
		for (UINT32 j = 0; i < imgHeight; i++)
		{
			img[i][j] = rgb_pixel(r[i][j], g[i][j], b[i][j]);
		}
	}

	matrix<rgb_pixel> videoFrame; 
	assign_image(videoFrame, img);
}

2.BYTE[][] RGB 图片直接转换为 matrix<rgb_pixel>



	matrix<rgb_pixel> img; 
        img.set_size(imgHeight, imgWidth);//设置好row 行  ,  col 列,就可以for 循环赋值了

	// loop over all the rows
	for (UINT32 i = 0; i < imgWidth; i++)
	{
		// loop over all the columns
		for (UINT32 j = 0; i < imgHeight; i++)
		{
			img(i,j) = rgb_pixel(r[i][j], g[i][j], b[i][j]);
		}
	}

  1. CV::Mat 转换为 dlib::matrixdlib::rgb_pixel

//加入opencv的mat 转换要 载入opencv lib
//#include <dlib/opencv/to_open_cv.h>
//#include <dlib/opencv/cv_image.h>


	dlib::matrix<dlib::rgb_pixel> imgFrame;
	assign_image(imgFrame, dlib::cv_image<dlib::rgb_pixel>(videoFrame));  此处videoFrame是cv::Mat格式
	


        cv::Mat cvMatImage;
	cap >> cvMatImage;//从摄像头获取 单帧 图像

        //cv::Mat 转array2d<dlib::rgb_pixel>
	dlib::array2d<bgr_pixel> dlibImage;
	dlib::assign_image(dlibImage, dlib::cv_image<bgr_pixel>(cvMatImage));// opencv 默认BGR图像顺序

        //灰色(单色) 处理   array2d<dlib::rgb_pixel> 转 dlib::matrix<dlib::rgb_pixel>
	dlib::array2d<unsigned char> dlibImageGray;
	dlib::assign_image(imgFrame, dlib::cv_image<unsigned char>(cvMatImageGray));

4.vectir<vectordlib::rgb_pixel> 转 dlib::matrixdlib::rgb_pixel

std::vector<std::vector<rgb_pixel>> imageFrame, UINT32 imgWidth, UINT32 imgHeight,




	matrix<rgb_pixel> videoFrame;
	videoFrame.set_size(imgHeight, imgWidth);//设置好行列,就可以for 循环赋值了

	// loop over all the rows
	for (UINT32 i = 0; i < imgWidth; i++) //imageFrame.size()
	{
		// loop over all the columns
		for (UINT32 j = 0; i < imgHeight; i++)//imageFrame[i].size()
		{
			videoFrame(i,j) = imageFrame[i][j];  //videoFrame(0, 0) = rgb_pixel(r[0][0], g[0][0], b[0][0]); 
		}
	}


  1. dlib::matrixdlib::rgb_pixel 转 vectir<vectordlib::rgb_pixel>

       std::vector<std::array<float, FEATURE_SIZE>>  face_Feature_descriptors

	std::vector<matrix<float, 0, 1>> vecMatrixFeature;
        {.......... // vecMatrixFeature  get value  .......... }

        
        face_Feature_descriptors.reserve(vecMatrixFeature.size());


	/*
	for (INT32 i = 0; i < vecMatrixFeature.size(); i++)
	{
		for (long c = 0; c < FEATURE_SIZE; ++c)//number columns 列,width
		{
			 face_Feature_descriptors[i][c]= vecMatrixFeature[i](0, c);

			 //face_Feature_descriptors.push_back(arrayFaceFeature);
		}
	}
	*/
	


	for (matrix<float, 0, 1> matrixFeature : vecMatrixFeature)
	{
		std::array<float, FEATURE_SIZE> arrayFaceFeature;
		for (long c = 0; c < FEATURE_SIZE; ++c)//number columns 列,width
		{
			arrayFaceFeature[c] = matrixFeature(0, c);
		}

		face_Feature_descriptors.push_back(arrayFaceFeature);
	}



  1. vector<vector<vector<rgb_pixel>>> 转 vector<matrix<rgb_pixel>>
vector<vector<vector<rgb_pixel>>> faces_chip 


vector<matrix<rgb_pixel>> vecMatrixFacesChip;

	//matrix<rgb_pixel> matrixFacesChip
	for(std::vector<std::vector<rgb_pixel>> face_chip  : faces_chip)
	{
		matrix<rgb_pixel> matrixFacesChip;
		for (UINT32 r = 0; r < face_chip.size(); r++)
		{
			for (UINT32 c = 0; c < faces_chip[r].size(); c++)
			{
				if (c == 0)
				{
					matrixFacesChip.set_size(face_chip.size(), faces_chip[r].size());
				}

				matrixFacesChip(r, c) = face_chip[r][c];
				//vecMatrixFacesChip[i]/
				vecMatrixFacesChip.push_back(move(matrixFacesChip));
			}
		}
	}

如果效率低,就改成 结构体 指针

strcut
{
BYTE** R;
BYTE** G;
BYTE** B;
}

posted @ 2023-11-04 18:17  scott_h  阅读(144)  评论(0编辑  收藏  举报