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!
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.