返回> 网站首页
Opencv的Mat、以及SDL图像数据转换
yoours2024-07-31 19:28:49
简介一边听听音乐,一边写写文章。
一、cv::Mat转SDL_Surface
SDL_Surface* MatToSurface(const cv::Mat& mat)
{
Uint32 sdlFormat = 0;
int channel = mat.channels();
switch (mat.type())
{
case CV_8UC3:
sdlFormat = SDL_PIXELFORMAT_BGR24;
break;
case CV_8UC4:
sdlFormat = SDL_PIXELFORMAT_BGRA32;
break;
default:
sdlFormat = SDL_PIXELFORMAT_UNKNOWN;
break;
}
if (sdlFormat == SDL_PIXELFORMAT_UNKNOWN)
return nullptr;
SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom((void*)mat.data, mat.cols, mat.rows, 0, mat.step, sdlFormat);
return surface;
}
二、字符串转SDL_Surface
SDL_Surface* DataArrToSurface(unsigned char* imageData, unsigned __int64 imageSize)
{
SDL_RWops* rwops = SDL_RWFromMem(imageData, imageSize);
if (rwops)
{
SDL_Surface* surface = IMG_Load_RW(rwops, 1);
return surface;
}
return nullptr;
}
三、字节数组转cv:Mat
void DataArrToMat(unsigned char* imageData, unsigned __int64 imageSize, cv::Mat& image)
{
std::vector<uchar> picVec((uchar*)imageData, (uchar*)imageData + imageSize);
image = cv::imdecode(cv::Mat(picVec), cv::IMREAD_COLOR);
}
四、cv::Mat转字节数组
bool MatToDataArr(cv::Mat img, std::vector<uchar>& vecBuf)
{
return cv::imencode(".jpg", img, vecBuf);
}