将cv::Mat转换为SDL_Texture

4

我正在尝试使用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并且它在那里可以正常工作。所以问题是:你知道我在代码中所做的转换有什么问题吗?如果不知道,有没有更好的方法来完成我想要做的事情?

2个回答

7

我成功了!我认为唯一的问题在于我需要使用&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);

}


我觉得这非常有帮助! - j0h

4

以下是另一种不使用 IplImage 的方法:

cv::Mat m ...;
// I'm using SDL_TEXTUREACCESS_STREAMING because it's for a video player, you should
// pick whatever suits you most: https://wiki.libsdl.org/SDL_TextureAccess
// remember to pick the right SDL_PIXELFORMAT_* !
SDL_Texture* tex = SDL_CreateTexture(
        ren, SDL_PIXELFORMAT_BGR24, SDL_TEXTUREACCESS_STREAMING, m.cols,
        m.rows);
SDL_UpdateTexture(tex, NULL, (void*)m.data, m.step1());

// do stuff with texture
SDL_RenderClear(...);
SDL_RenderCopy(...);
SDL_RenderPresent(...);
// cleanup (only after you're done displaying. you can repeatedly call UpdateTexture without destroying it)
SDL_DestroyTexture(tex)

我更喜欢这种方法而不是创建表面的方法,因为你不需要释放表面,并且它更加灵活(例如,可以轻易地更新纹理,而不是创建/销毁)。我还要注意的是,我无法将这些方法组合起来:例如使用SDL_CreateRGBSurfaceFrom创建纹理,然后稍后更新它。这会导致灰色条纹和图像混乱。


什么是“m”?什么是“text”?什么是“ren”?你节省的时间值得吗? - Michal
除此之外,感谢您的帮助。 - Michal
m 是指你手头拥有的 cv::Mat,毕竟这个问题是关于转换它,所以我假设你已经有了它。虽然文本不在那里,但我在代码片段中定义了 tex,它就是问题所要求的 SDL_Texture。诚然,我没有明确指定 ren,它是一个 SDL_Renderer,但为了使用 SDL,你需要其中之一,所以我把它留了下来。你的评论有点刻薄,但很搞笑,让我捧腹大笑,但其他人可能会感到不快。我肯定节省了相当多的时间 ;) - Asad-ullah Khan
1
抱歉。只是“tex”和“ren”对我来说太多了,甚至一开始我以为“tex”是“text”,但在实现过程中,我意识到它实际上是一种“纹理”。 - Michal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接