1
Graphics / Re: Getting camera images
« on: April 17, 2018, 10:46:53 am »
Hi !
After a lot of researsh i suceeded, heres the code :
https://fr.sfml-dev.org/forums/index.php?topic=19621.0
Thanks for your help.
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 :#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;
}
https://fr.sfml-dev.org/forums/index.php?topic=19621.0
Thanks for your help.