在写相机接口的时候,经常需要把byte*类型转成OpenCV mat类型进行图像运算,下面给出两者互相转换的函数
Byte *->Mat
bool ByteToMat(BYTE* pImg, int nH, int nW, int nChannel, cv::Mat& out_img)
{
if (pImg == nullptr)
{
return false;
}
int nByte = nH * nW * nChannel / 8;
int nType = nChannel == 8 ? CV_8UC1 : CV_8UC3;
out_img = cv::Mat::zeros(nH, nW, nType);
memcpy(out_img.data, pImg, nByte);
return true;
}
Mat->Byte *
bool MatToByte(cv::Mat img, BYTE*& pImg)
{
int nChannel = img.channels() * 8;
int nHeight = img.rows;
int nWidth = img.cols;
int nBytes = nHeight * nWidth * nChannel / 8;
if (pImg)
delete[] pImg;
pImg = new BYTE[nBytes];
memcpy(pImg, img.data, nBytes);
return true;
}
原文链接: https://www.cnblogs.com/MorganMa/p/14046422.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/205490
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!