Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Getting camera images  (Read 4654 times)

0 Members and 1 Guest are viewing this topic.

axelA

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Getting camera images
« 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 !  :)

sjaustirni

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
Re: Getting camera images
« Reply #1 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?
Or are you talking about real-world camera, trying to stream camera output into an SFML application?

axelA

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Getting camera images
« Reply #2 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.

sjaustirni

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
Re: Getting camera images
« Reply #3 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() 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).

axelA

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Getting camera images
« Reply #4 on: April 15, 2018, 09:07:39 pm »
Well, it seems pretty hard to do...
But thanks for your help !

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: Getting camera images
« Reply #5 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.
  • ffmpeg (or some other program) - create a subprocess using the operating system API calling ffmpeg with parameters to the camera. You can set the subprocess to write to memory you own.
  • libffmpeg - the library version of ffmpeg
  • OpenCV - the library has image capturing functionality. A bit overkill if you just want to capture images (ie: not use the computer vision functionality), but the API is easy.
  • libuvc
  • If you're on Linux, you can use the v4l2 (Video for Linux 2) API. It's fairly straightforward, just expect to be doing a lot of C-style code (the equivalent-ish APIs on Windows are DirectShow or Windows Image Acquisition, iirc)
  • If it's a specific/specialized device, the manufacturer might have an official API/method for capturing images (ie: Raspberry Pi Camera, Intel RealSense, etc)

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.

axelA

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Getting camera images
« Reply #6 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 ...

axelA

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Getting camera images
« Reply #7 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.

 

anything