2015-02-02 4 views
2

OpenCV 매트를 SFML 이미지로 포맷하는 방법이 있습니까? 아니면 SFML 창에 매트를 표시하는 다른 방법이 있습니까? 매트 배열을 uchar 배열로 변환하려고 시도했지만 결과가 검은 색 화면입니다.OpenCV 매트에서 SFML 이미지

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(); 
    } 

답변

0

이 잘 나를 작동합니다

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을 계속 재 할당하므로 검은 색 화면이 나타납니다.