Ask Your Question
0

How to convert SFML image to OpenCV Mat in c++?

asked 2016-09-27 23:14:34 -0600

Suraksha gravatar image

updated 2016-09-28 01:16:59 -0600

Below code is used to convert an OpenCV Mat image into SFML image. How to convert SFML image back to Mat image?

 cv::Mat frameBGR, frameBGRA;   
 sf::Image image;    
 cv::cvtColor(frameBGR,frameRGBA,
 cv::COLOR_BGR2RGBA);    
 image.create(frameRGBA.cols,
 frameRGBA.rows, frameRGBA.ptr());

If texture or sprite can't be converted to OpenCV Mat,then is it possible to convert sf::Image to OpenGL and convert it back to cv::Mat

    sf::Texture texture;
    sf::Event event;
    sf::Sprite sprite;
    texture.loadFromImage(image);
    sprite.setTexture(texture);

Update: I referred this post, but still was not able to convert.

    void* buffer = ℑ 
    cv::Mat image_mat = cv::Mat(cvSize(500, 500),CV_8UC4, buffer, cv::Mat::AUTO_STEP);  
    cvtColor(image_mat, converted,CV_RGBA2BGR);

It crashes at below line of code (when i display image )

imshow("img", converted);
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-09-28 01:37:26 -0600

berak gravatar image

if you'd care to read the SFML docs (instead of cheesy SO posts), you'd have found it has getPixelsPtr () and getSize(), so it would be:

sf::image img = ...
sf::Vector2u siz = img.getSize();
uchar *pixels = img.getPixelsPtr();
cv::Mat mat(Size(siz.x, siz.y), CV_8UC4, pixels);

in your case, no, you cannot simply pass the address of the sf::image object !

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-09-27 23:14:34 -0600

Seen: 969 times

Last updated: Sep 28 '16