Copy RenderTexture to Image

点点圈 提交于 2019-12-11 05:07:44

问题


I'm trying to copy sf::RenderTexture (with sf::Image & sf::Text on it), to Opencv Mat object. We can convert an sf::Image to cv::Mat (Opencv) by

sf::Image img;
cv::Size size(img.getSize().x, img.getSize().y);    
cv::Mat mat(size,CV_8UC4, (void*)img.getPixelsPtr(), cv::Mat::AUTO_STEP);
cv::cvtColor(mat, mat, cv::COLOR_RGBA2BGRA);

as far as I know, in SFML we cannot add text directly to sf::Image but we have to use RenderWindow or RenderTexture for that. I guess only workaround is to load objects in following order

sf::Image --> sf::Texture --> sf::Sprite -->sf::RenderTexture

sf::Text --> sf::RenderTexture

Finally Copy RenderTexture to sf::Image then convert it to cv::Mat
I suppose it will take long time to copy sf::RenderTexture back to sf::Image.

any other workaround for this? like using sf::Image::loadFromMemory();

P.S: Some references here & here


回答1:


No, there isn't really an alternative when it comes to SFML. Copying from VRAM to RAM isn't very fast, but should still be able to perform at a okay-ish framerate. Maybe you should just give it a try and see whether the performance does really impact your scenario.

The issue here is that SFML operates in the GPU space, so text rendering is made for rendering with your GPU similar thing with sprite, but what you actually seem to want is CPU rendering of text and sprites.

If you only translate sprites (no scaling/rotating) it should be relatively easy to build such functionality on top of sf::Image.

For text rendering you might want to use freetype directly and not move the font data to the GPU.



来源:https://stackoverflow.com/questions/40232041/copy-rendertexture-to-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!