opencv图片格式转换cv::mat与*BYTE相互转换

1.cv::Mat转BYTE*
void Mat_BYTE(Mat matImg, BYTE*& bImg)
{
int nTD = matImg.channels() * 8;
int nHeight = matImg.rows;
int nWidth = matImg.cols;
int nBytes = nHeight * nWidth * nTD / 8;
if(bImg!=NULL)
delete[] bImg;
bImg= new BYTE[nBytes];
memcpy(bImg, matImg.data, nBytes);
}
2.BYTE转cv::Mat
bool BYTE_Mat(BYTE
bImg, int nHeight, int nWidth, int nTD, Mat& resImg)//nHeight,nWidth为图像的高和宽,nTD为通道
{
if(bImg == NULL) {return false; }
int nByte = nHeight* nWidth* nTD/ 8;
if(nTD==8)
{
resImg= Mat::zeros(nHeight, nWidth, CV_8UC1 );
}
else
{
resImg= Mat::zeros(nHeight, nWidth, CV_8UC3);
}
memcpy(resImg.data, bImg, nByte);
reture true;
}

你可能感兴趣的:(C++,opencv,opencv,人工智能)