OpenCV 垫到 SFML 图像
OpenCV mat to SFML image
有没有办法将 OpenCV 垫格式化为 SFML 图像。或者有没有其他方法可以在 SFML window 中显示垫子?
我尝试将mat数组转换为uchar数组,但结果是黑屏。
cv::VideoCapture cap(0); // open the video file for reading
sf::Event event;
sf::Image image;
sf::Texture texture;
sf::Sprite sprite;
sprite.setTexture(texture);
cv::Mat frame;
sf::RenderWindow window(sf::VideoMode(1200, 900),"my Window");
cap >> frame;
uchar* camData = new uchar[frame.total()*4];
cv::Mat continuousRGBA(frame.size(), CV_8UC4, camData);
cv::cvtColor(frame, continuousRGBA, CV_BGR2RGBA, 4);
image.create(frame.cols,frame.rows,camData);
texture.loadFromImage(image);
while (window.isOpen()){
cap >> frame;
cv::cvtColor(frame, continuousRGBA, CV_BGR2RGBA, 4);
image.create(frame.cols,frame.rows,camData);
texture.loadFromImage(image);
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(sprite);
window.display();
}
这很适合我:
cv::VideoCapture cap(0); // open the video file for reading
if(!cap.isOpened())
{
return 0;
}
sf::RenderWindow window(sf::VideoMode(1200, 900), "RenderWindow");
cv::Mat frameRGB, frameRGBA;
sf::Image image;
sf::Texture texture;
sf::Event event;
sf::Sprite sprite;
while (window.isOpen())
{
cap >> frameRGB;
if(frameRGB.empty())
{
break;
}
cv::cvtColor(frameRGB, frameRGBA, cv::COLOR_BGR2RGBA);
image.create(frameRGBA.cols, frameRGBA.rows, frameRGBA.ptr());
if (!texture.loadFromImage(image))
{
break;
}
sprite.setTexture(texture);
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(sprite);
window.display();
}
管理 camData
时出现问题。您在循环外部分配内存,但 cv::cvtColor()
会一遍又一遍地重新分配 continuousRGBA
,这就是您看到黑屏的原因。
有没有办法将 OpenCV 垫格式化为 SFML 图像。或者有没有其他方法可以在 SFML window 中显示垫子? 我尝试将mat数组转换为uchar数组,但结果是黑屏。
cv::VideoCapture cap(0); // open the video file for reading
sf::Event event;
sf::Image image;
sf::Texture texture;
sf::Sprite sprite;
sprite.setTexture(texture);
cv::Mat frame;
sf::RenderWindow window(sf::VideoMode(1200, 900),"my Window");
cap >> frame;
uchar* camData = new uchar[frame.total()*4];
cv::Mat continuousRGBA(frame.size(), CV_8UC4, camData);
cv::cvtColor(frame, continuousRGBA, CV_BGR2RGBA, 4);
image.create(frame.cols,frame.rows,camData);
texture.loadFromImage(image);
while (window.isOpen()){
cap >> frame;
cv::cvtColor(frame, continuousRGBA, CV_BGR2RGBA, 4);
image.create(frame.cols,frame.rows,camData);
texture.loadFromImage(image);
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(sprite);
window.display();
}
这很适合我:
cv::VideoCapture cap(0); // open the video file for reading
if(!cap.isOpened())
{
return 0;
}
sf::RenderWindow window(sf::VideoMode(1200, 900), "RenderWindow");
cv::Mat frameRGB, frameRGBA;
sf::Image image;
sf::Texture texture;
sf::Event event;
sf::Sprite sprite;
while (window.isOpen())
{
cap >> frameRGB;
if(frameRGB.empty())
{
break;
}
cv::cvtColor(frameRGB, frameRGBA, cv::COLOR_BGR2RGBA);
image.create(frameRGBA.cols, frameRGBA.rows, frameRGBA.ptr());
if (!texture.loadFromImage(image))
{
break;
}
sprite.setTexture(texture);
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(sprite);
window.display();
}
管理 camData
时出现问题。您在循环外部分配内存,但 cv::cvtColor()
会一遍又一遍地重新分配 continuousRGBA
,这就是您看到黑屏的原因。