How to convert an OpenCV IplImage to an SDL_Surface?

只谈情不闲聊 提交于 2019-12-04 05:35:35

Ok, I got it working!

I think I was confused by the fact that an OpenCV depth of 8 means a pixel has 8 bits per channel, so in a 3-channel image, a pixel has 24 bits. So when converting that to the SDL meaning of depth, we get 8 * 3 = 24 bits.

The image was 24 bits after all, which SDL supports. So converting the image to SDL is as simple as:

SDL_Surface *surface = SDL_CreateRGBSurfaceFrom((void*)opencvimg->imageData,
                opencvimg->width,
                opencvimg->height,
                opencvimg->depth*opencvimg->nChannels,
                opencvimg->widthStep,
                0xff0000, 0x00ff00, 0x0000ff, 0
                );
return surface;

Sorry for the confusion, I hope this helps anybody searching for the same answer.

Other links of interest: http://www.libsdl.org/cgi/docwiki.cgi/Pixel_Access
And the complete subroutine at: http://paster.dazjorz.com/?p=3714

First of all: Thanks!!

Second: It works perfectly with 3 Channel images but I want to display a Single-Channel-IplImage

so there we go:

SDL_Surface *single_channel_ipl_to_surface (IplImage *opencvimg)
{
    SDL_Surface *surface = SDL_CreateRGBSurfaceFrom((void*)opencvimg->imageData,
                           opencvimg->width,
                           opencvimg->height,
                           opencvimg->depth*opencvimg->nChannels,
                           opencvimg->widthStep,
                           0xffffff, 0xffffff, 0xffffff,0);
    return surface;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!