SFML community forums

Help => Graphics => Topic started by: axelA on April 15, 2018, 06:43:43 pm

Title: Getting camera images
Post by: axelA on April 15, 2018, 06:43:43 pm
Hi ! Id like to know if its possible to get a camera images in real time ?
I mean, not just taking a picture and show it, but show "everything the camera sees" ?
Thanks !  :)
Title: Re: Getting camera images
Post by: sjaustirni on April 15, 2018, 06:57:16 pm
Hello! Welcome on the SFML forums!

I am not sure whether I understand you correctly, are you trying to screenshot what is in the sf::View (https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1View.php)?
Or are you talking about real-world camera, trying to stream camera output into an SFML application?
Title: Re: Getting camera images
Post by: axelA on April 15, 2018, 06:59:26 pm
Hi ! Thanks for your answer.
Im talking about a real life camera.
I try to do somethig like when you capture a picture, you can actualy see what you'll shot.
Title: Re: Getting camera images
Post by: sjaustirni on April 15, 2018, 07:10:41 pm
Well, assuming you have access to the camera output pixels, it is a matter of loading those pixels to sf::Texture via loadFromMemory() (https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Texture.php#a2c4adb19dd4cbee0a588eeb85e52a249) then rendering it to the screen.

That being said, the taken picture data should be easy to obtain, but I don't think you can get the "camera view" without actually recording. Even if you do record, you'll have to pray that the decoding library you chose can stream files (because SFML does not contain any video decoders).
Title: Re: Getting camera images
Post by: axelA on April 15, 2018, 09:07:39 pm
Well, it seems pretty hard to do...
But thanks for your help !
Title: Re: Getting camera images
Post by: dabbertorres on April 15, 2018, 09:33:58 pm
This isn't the easiest thing to find (good) results for, so I'll give you some pointers.

You've got a few options.

After that, it's a matter of converting the captured image to SFML types, as sjaustirni said.

Hope this points you in the right direction.
Title: Re: Getting camera images
Post by: axelA on April 16, 2018, 05:33:37 pm
Thanks for your answer, Im actualy seing OpenCV and i try to covert a Mat structure to a sfRectangleShape.
(Taking the pixel of the first one into the second one)
I hope its possible ...
Title: Re: Getting camera images
Post by: axelA on April 17, 2018, 10:46:53 am
Hi !
After a lot of researsh i suceeded, heres the code :
#include "stdafx.h"
#include <SFML\Graphics.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
        VideoCapture stream1(0);
        if (!stream1.isOpened()) {
                cout << "Camera failed to open" << endl;
        }
        sf::RenderWindow window(sf::VideoMode(1920, 1080, 32), "???");
        sf::RectangleShape rect(sf::Vector2f{1920, 1080});
        sf::Texture texture;
        sf::Event event;
        Mat sfml_rgba_frame;
        Mat cameraFrame;
        texture.loadFromFile("images/bomb.png");
        rect.setTexture(&texture, false);

        window.setFramerateLimit(30);
        while (window.isOpen()) {
                stream1.read(cameraFrame);
                cvtColor(cameraFrame, sfml_rgba_frame, CV_BGR2RGBA);
                texture.create(sfml_rgba_frame.cols, sfml_rgba_frame.rows);
                texture.update(reinterpret_cast<sf::Uint8*>(sfml_rgba_frame.ptr()));
                rect.setTexture(&texture);
                while (window.pollEvent(event)) {
                        if (event.type == sf::Event::EventType::Closed)
                                window.close();
                }
                window.clear();
                window.draw(rect);
                window.display();
        }
    return 0;
}
 
The part about converting cv::mat to sf::Texture was found here :
https://fr.sfml-dev.org/forums/index.php?topic=19621.0
Thanks for your help.