OpenCVFITS: A C++ single header file library that helps save and read images to/from FITS files

Recently I have been writing software designed to take imagery for astronomy applications, and it turns out astronomy types love FITS. FITS is great, but the C libraries available are not. If you live in Python land, astropy does a good job at hiding the ugliness from you. But if you live in C/C++ land, you are in for some pain.

With this being said, I created OpenCVFITS. It is pretty easy to work with, and gives people using OpenCV a way to read and write multiple images from a single FITS file. It also provides some functionality to writing metadata to each image in a flexible way.

Any comments, suggestions, and updates are very welcome!

Comments 2

  • Nice library!
    I tried it out on some FITS images from AstroDMX, and it works.

    There are some problems, though.

    The images contain 3 channels, red, green blue, and your library only extracts the first channel.

    So I made some quick modifications, all inside the function “getNextImage”, here:

    1. modified the declaration of naxes: long naxes[3]; – (3 instead of 2)
    2. further down: fits_get_img_size(this->file, 3, naxes, &status); – get all 3 naxes
    3. the switch statement to get the size: data_size = naxes[0] * naxes[1] * naxes[2] – make sure the sizes are multiplied by the number of channels also
    4. Personal issue, I don’t like malloc/free, so I changed the bit that allocates a buffer to this:
    vector imageData;
    imageData.resize(data_size);

    // Read the image data
    ret =
    fits_read_img(this->file, datatype, 1, naxes[0] * naxes[1] * naxes[2], NULL, &imageData[0], NULL, &status);

    Now, unfortunately, the function may return multiple cv::Mat structures. I create them like this:

    for (int i = 0; i < naxes[2]; i++)
    {
    channels.push_back(cv::Mat(naxes[1], naxes[0], openCVType, &imageData[i * naxes[1] * naxes[0]]).clone());
    }

    so I changed the declaration of the function to:

    bool getNextImage(deque &channels)

    Quick and dirty, and now I get all 3 channels from the images. Then I can combine them with OpenCV etc.

    Feel free to copy or ignore this.

    • Hi Anders,

      Thanks so much for sharing this. I will probably implement RGB support eventually. I’ll let you know if/when I do.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

➤

Discover more from DaleGia

Subscribe now to keep reading and get access to the full archive.

Continue reading