Question

I'm doing a project which involves taking a live camera feed and displaying it on a window for the user.

As the camera image is the wrong way round by default, I'm flipping it using cvFlip (so the computer screen is like a mirror) like so:

while (true) 
{   
    IplImage currentImage = grabber.grab();
    cvFlip(currentImage,currentImage, 1);

    // Image then displayed here on the window. 
}

This works fine most of the time. However, for a lot of users (mostly on faster PCs), the camera feed flickers violently. Basically an unflipped image is displayed, then a flipped image, then unflipped, over and over.

So I then changed things a bit to detect the problem...

while (true) 
{   
    IplImage currentImage = grabber.grab();
    IplImage flippedImage = null;
    cvFlip(currentImage,flippedImage, 1); // l-r = 90_degrees_steps_anti_clockwise
    if(flippedImage == null)
    {
        System.out.println("The flipped image is null");
        continue;
    }
    else
    {
        System.out.println("The flipped image isn't null");
        continue;
    }
}

The flipped image appears to always return null. Why? What am I doing wrong? This is driving me crazy.

If this is an issue with cvFlip(), what other ways are there to flip an IplImage?

Thanks to anyone who helps!

Était-ce utile?

La solution

You need to initialise the flipped image with an empty image rather than NULL before you can store a result in it. Also, you should only create the image once and then re-use the memory for more efficiency. So a better way to do this would be something like below (untested):

IplImage current = null;
IplImage flipped = null;

while (true) {
  current = grabber.grab();

  // Initialise the flipped image once the source image information
  // becomes available for the first time.
  if (flipped == null) {
    flipped = cvCreateImage(
      current.cvSize(), current.depth(), current.nChannels()
    );
  }

  cvFlip(current, flipped, 1);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top