2014-03-28 113 views
1

我正在尝试使用SDL播放视频。为此,我使用opencv加载视频并获取帧。然后,我只需要将这些帧转换为SDL_Texture *,然后我就可以将它们绘制在屏幕上。将cv :: Mat转换为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在我的项目的其他部分,它在那里工作。所以问题是:你知道我在代码中进行的转换有什么问题吗?如果没有,是否有更好的方法来做我想做的事情?

回答

3

我明白了!我认为唯一的问题是我不得不使用& 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); 

}

+0

我发现这非常有帮助! – j0h