gpt4 book ai didi

c++ - 将 cv::Mat 转换为 SDL_Texture

转载 作者:太空狗 更新时间:2023-10-29 20:40:47 28 4
gpt4 key购买 nike

我正在尝试使用 SDL 播放视频。为此,我使用 opencv 加载视频并获取帧。然后我只需要将这些帧转换为 SDL_Texture*,然后我就可以在屏幕上绘制它们了。

那是我的问题,我正在将其转换为 SDL_Surface*,但随后转换为 SDL_Texture 失败,我不确定原因。这是我的代码:

void Cutscene::play()
{
this->onLoop();
this->onRender();

while(!frameMat.empty())
{
this->onLoop();
this->onRender();
}
}

void Cutscene::onLoop()
{
video >> frameMat;

convertCV_MatToSDL_Texture();
}

void Cutscene::onRender()
{
Image::onDraw(GameEngine::getInstance()->getRenderer(), frameTexture);

}

void Cutscene::convertCV_MatToSDL_Texture()
{
IplImage opencvimg2 = (IplImage)frameMat;
IplImage* opencvimg = &opencvimg2;

//Convert to SDL_Surface
frameSurface = SDL_CreateRGBSurfaceFrom((void*)opencvimg->imageData,
opencvimg->width, opencvimg->height,
opencvimg->depth*opencvimg->nChannels,
opencvimg->widthStep,
0xff0000, 0x00ff00, 0x0000ff, 0);

if(frameSurface == NULL)
{
SDL_Log("Couldn't convert Mat to Surface.");
return;
}

//Convert to SDL_Texture
frameTexture = SDL_CreateTextureFromSurface(
GameEngine::getInstance()->getRenderer(), frameSurface);
if(frameTexture == NULL)
{
SDL_Log("Couldn't convert Mat(converted to surface) to Texture."); //<- ERROR!!
return;
}

//cvReleaseImage(&opencvimg);
//MEMORY LEAK?? opencvimg opencvimg2
}

我在我项目的其他部分使用了这个函数 SDL_CreateTextureFromSurface 并且它在那里工作。所以问题是:你知道我在代码中进行的转换有什么问题吗?如果没有,是否有更好的方法来完成我想做的事情?

最佳答案

我成功了!我认为唯一的问题是我必须使用 &frameMat 而不是 frameMat。如果有人感兴趣,这是我的代码:

SDL_Texture* Cutscene::convertCV_MatToSDL_Texture(const cv::Mat &matImg)
{
IplImage opencvimg2 = (IplImage)matImg;
IplImage* opencvimg = &opencvimg2;

//Convert to SDL_Surface
frameSurface = SDL_CreateRGBSurfaceFrom(
(void*)opencvimg->imageData,
opencvimg->width, opencvimg->height,
opencvimg->depth*opencvimg->nChannels,
opencvimg->widthStep,
0xff0000, 0x00ff00, 0x0000ff, 0);

if(frameSurface == NULL)
{
SDL_Log("Couldn't convert Mat to Surface.");
return NULL;
}

//Convert to SDL_Texture
frameTexture = SDL_CreateTextureFromSurface(
GameEngine::getInstance()->getRenderer(), frameSurface);
if(frameTexture == NULL)
{
SDL_Log("Couldn't convert Mat(converted to surface) to Texture.");
return NULL;
}
else
{
SDL_Log("SUCCESS conversion");
return frameTexture;
}

cvReleaseImage(&opencvimg);

关于c++ - 将 cv::Mat 转换为 SDL_Texture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22702630/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com