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

Author Topic: Problem with using webcam - slow copying to image  (Read 4724 times)

0 Members and 1 Guest are viewing this topic.

kulesz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Problem with using webcam - slow copying to image
« on: March 20, 2011, 10:54:08 am »
Hi,

I'm trying to use SFML with a webcam library (videoInput), but copying data (pixels) from camera buffer to image seems to be very slow - the video is no more than 5fps :-/ The same code written in Allegro runs few times faster. Is it some way to fasten it up? I'm using SFML v.1.6.

Code:

Code: [Select]

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    // Create the main rendering window
App.Create(sf::VideoMode(800, 550, 32), "SFML Graphics");
cam1.Init(640,480);

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear();
cam1.Draw(&App);
        // Display window contents on screen
        App.Display();
App.SetFramerateLimit(60);
    }

    return EXIT_SUCCESS;
}


And webcam functions:
Code: [Select]

int CCamera::Init(int width, int height)
{
if (!VI.setupDevice(device, width, height)) return 1;
VI.setIdealFramerate(device, 25);
w = VI.getWidth(device);
h = VI.getHeight(device);
size = VI.getSize(device);
buffer = new unsigned char[size];
buffer2= new unsigned char[w*h*4];
  image.Create(w, h, sf::Color(230, 230, 230));
image.SetSmooth(false);
sprite.SetImage(image);
return 0;
}


void CCamera::Draw(sf::RenderWindow * app)
{

if(VI.isFrameNew(device))
{
//Here I take the buffer of RGB values
VI.getPixels(device, buffer, true, true);
int z=0;
//Transform it into ARGB
for (int u=0; u<w*h*3; u+=3)
{
buffer2[z]=buffer[u];
buffer2[z+1]=buffer[u+1];
buffer2[z+2]=buffer[u+2];
buffer2[z+3]=255;
z+=4;
}
// And this is the problem - loading seems to be so slow
image.LoadFromPixels(w,h,buffer2);
}

app->Draw(sprite);
}

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Problem with using webcam - slow copying to image
« Reply #1 on: March 20, 2011, 11:41:41 am »
How do you know it's because of LoadFromPixels() ?
Want to play movies in your SFML application? Check out sfeMovie!

kulesz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Problem with using webcam - slow copying to image
« Reply #2 on: March 20, 2011, 11:48:44 am »
Copying from buffer to image is the only thing I'm doing here - the same program on Allegro, with changed just a few lines - works faster. Nothing else in the program doesn't slow down the capture, only altering the width and height parameters of LoadFromPixels changes the loading speed.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with using webcam - slow copying to image
« Reply #3 on: March 20, 2011, 11:48:45 am »
The transform loop may be slow as well. Try to remove it and see if the FPS increases (you'll see incorrect things on screen but it's just to measure performances).
Laurent Gomila - SFML developer

kulesz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Problem with using webcam - slow copying to image
« Reply #4 on: March 20, 2011, 11:52:10 am »
Just tried it - it's doesn't affect performance at all :-/

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Problem with using webcam - slow copying to image
« Reply #5 on: March 20, 2011, 12:08:55 pm »
Well.. I'm using LoadFromPixels() for 2048x872 images at 25 FPS thus I don't think this is really the problem.

I don't know whether this could be because of drivers not being up to date. What's your graphics card?
Want to play movies in your SFML application? Check out sfeMovie!

kulesz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Problem with using webcam - slow copying to image
« Reply #6 on: March 20, 2011, 05:48:43 pm »
It's a Samsung N140 netbook, so it's actually Intel 945GSE chipset. I know it's weak, but it's my requirement for the application to run on such a graphic card.

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Problem with using webcam - slow copying to image
« Reply #7 on: March 20, 2011, 06:49:05 pm »
I dunno what to tell you. I'm using an Intel GMA X3100 graphics card, which isn't that good too. But it's working well. It may also depends on your CPU (which one are you using?).

Did you try to update the graphics drivers? Do you get acceptable performances with other OpenGL apps?
Want to play movies in your SFML application? Check out sfeMovie!

 

anything