Is there an OpenCV idiom for converting arbitrary input images to a desired format?

asked 2013-09-23 14:13:30 -0600

wpd gravatar image

Setup: Suppose I want to read images from arbitrary cameras or input files and don't know ahead of time if the images will be greyscale, color, 8 bits/pixel, 16 bits/pixel, etc...

I used to think that I could call get(CV_CAP_FRAME_FORMAT) to determine the fixed frame format for the images returned by my VideoCapture() object, but I have since learned that I cannot count on that feature being implemented, even for fixed format .bmp files.

Is there some common OpenCV idiom for determining the input format of a camera (or file) and for dynamically transforming that to the format required by downstream processing?

For example, suppose I have a single channel grayscale image (CV_8UC1) that I need to convert to RGB because my downstream algorithm assumes/requires RGB (or vice versa). I can call cvtColor() to do the conversion, but I must not do that if the input data stream is already an RGB stream. How do OpenCV experts typically handle this situation?

I could do something like this:

while (true) {
  cap >> image;
  switch(image.type())
  case CV_8UC3:
    // This is what my downstream algorithm expects
    // (Actually, I can't tell if the image is in RGB or BGR format
    // here.  That's probably yet another problem all together).
    break;

  case CV_8UC1:
    Mat converted;
    cv.cvtColor(image, converted, CV_GRAY2RGB);
    image = converted;
    break;

  ...

Thanks.

--wpd

edit retag flag offensive close merge delete

Comments

AFAIK if you just use the ´cap >> image´ then you will get a BGR stream out of your VideoCapture object. The next step will be your own choice. If you know that it is a grayscale stream, just split the channels and grab the first one, since the intensities will be the same for all channels. All results returned from the videoCapture will be in BGR format, even if your input is RGB. You could do a conversion, but just keep working with that knowledge and everything will be fine. Hope this makes sense...

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-24 03:37:29 -0600 )edit

Thanks for the reply. I didn't realize that I will always get a BGR stream out of my VideoCapture object. That's a pretty key piece of information. Do you know how can I go about getting that added to the documentation?

wpd gravatar imagewpd ( 2013-09-24 08:27:26 -0600 )edit

Actually what you could do is visit the How to contribute page. It has an explanation of how to create pull requests to the github fork for adding extra's. You would be looking at the rst files in the module folders, which contains the documents of all functionality.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-24 08:43:39 -0600 )edit
1

Thanks. I'll go do that (he says hopefully and enthusiastically). It's (literally) the least I could do after running into this.

wpd gravatar imagewpd ( 2013-09-24 10:09:22 -0600 )edit