how to use CV_CAP_PROP_FOURCC?

13,889

Solution 1

Get a double* and convert that:

double f = cvGetCaptureProperty(image, CV_CAP_PROP_FOURCC);
char* fourcc = (char*) (&f); // reinterpret_cast

However, this OpenCV highgui tutorial suggests the following with the C++ interface:

int ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC));

// Transform from int to char via Bitwise operators
char EXT[] = {(char)(ex & 0XFF),(char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24),0};

Of course, instead of the get method, you'd use cvGetCaptureProperty.

Solution 2

You can use the following function:

// http://mkonrad.net/2014/06/24/cvvideocamera-vs-native-ios-camera-apis.html
void fourCCStringFromCode(int code, char fourCC[5]) {
    for (int i = 0; i < 4; i++) {
        fourCC[3 - i] = code >> (i * 8);
    }
    fourCC[4] = '\0';
}

For instance:

    double f = cvGetCaptureProperty(capture, CV_CAP_PROP_FOURCC);
    printf("fourcc=%d\n", f); // outputs fourcc=6
    char fourcc[5];
    fourCCStringFromCode((int)f, fourcc);
    printf("fourcc=%s\n", fourcc); // outputs fourcc=DIVX
Share:
13,889
CroCo
Author by

CroCo

Updated on June 25, 2022

Comments

  • CroCo
    CroCo almost 2 years

    It seems simple but I couldn't get it to work. I want to print out the format of an image by using

    cvGetCaptureProperty(image, CV_CAP_PROP_FOURCC)
    

    where image is an object of CvCapture struct. This function returns double. I did convert it to char* so that I can print out the format but it didn't work. Any suggestions?

  • CroCo
    CroCo about 10 years
    I did that but I got nothing. double f = cvGetCaptureProperty(image, CV_CAP_PROP_FOURCC); char* fourcc = (char*)(&f); std::cout << "\nFormat : " << fourcc[0] << fourcc[1] << std::endl; I got nothing.
  • chappjc
    chappjc about 10 years
    @CroCo Huh, well you could follow the example of the highgui tutorial (see update) and cast the double to int, and transform to char with a series of bitwise operations.
  • chappjc
    chappjc about 10 years
    @CroCo Also you could try cout << "format: " << EXT << endl; with the null-terminated version created by all the bitwise ops.
  • CroCo
    CroCo about 10 years
    But why do I need to use bitwise? Is there a straightforward method to do that? Also, I've tried to used the method in openCV's documentation however If I do access to EXT[5], it gives me data. Why this happens? Doesn't it have only four characters?
  • chappjc
    chappjc about 10 years
    @CroCo A C string is null-terminated, meaning the last byte is '\0'. Don't access it directly, as it just signals the end of a "string". (Ignore it, as it is just used so that cout knows when to stop.)
  • CroCo
    CroCo about 10 years
    I know that but what I'm getting is real data not just garbage. Why this happens?
  • chappjc
    chappjc about 10 years
    Because indexing starts with 0. The 1st element in EXT is EXT[0], 5th element in EXT is EXT[4]. EXT[5] is out of the array. I don't know what you mean by real data vs. garbage however.
  • CroCo
    CroCo about 10 years
    I know EXT[5] is out of the range but I tried to access to it, I got a character. I was expecting to get a garbage value.
  • chappjc
    chappjc about 10 years
    @CroCo Why access it? It could be any junk in memory.