0

I've been searching on an answer but all I can find is the inverse of what I need (Mat to FIBITMAP). I have a short code that loads images as FIBITMAP objects. I need FreeImage because OpenCV doesn't allow me to load pfm and some other extension files. What I'm trying to do is convert the FIBTMAP image into an OpenCV Mat so that I can use this in a much longer code I already have. How do I do?

pedro
  • 417
  • 2
  • 7
  • 25
  • post the code of the inverse (Mat to FIBITMAP) – Miki Aug 04 '15 at 16:35
  • Thank you for answering. The code was on some other posts here on StackOverflow. I think this (http://stackoverflow.com/questions/4927049/copy-opencv-iplimage-or-mat-into-freeimage-fibitmap) is the more complete one! – pedro Aug 05 '15 at 09:03

1 Answers1

6

The funtion FI2MAT will perform the conversion from FreeImage to OpenCV Mat.

I cannot thoroughly test each type of FreeImage format right now, but it should work as expected. Let me know if there are some issues with some formats.

Remember that not all types may be rendered correctly by imshow, so you may need to convert the image before.

Here the code with an example:

#include <FreeImage.h>
#include <opencv2\opencv.hpp>
using namespace cv;

void FI2MAT(FIBITMAP* src, Mat& dst)
{
    //FIT_BITMAP    //standard image : 1 - , 4 - , 8 - , 16 - , 24 - , 32 - bit
    //FIT_UINT16    //array of unsigned short : unsigned 16 - bit
    //FIT_INT16     //array of short : signed 16 - bit
    //FIT_UINT32    //array of unsigned long : unsigned 32 - bit
    //FIT_INT32     //array of long : signed 32 - bit
    //FIT_FLOAT     //array of float : 32 - bit IEEE floating point
    //FIT_DOUBLE    //array of double : 64 - bit IEEE floating point
    //FIT_COMPLEX   //array of FICOMPLEX : 2 x 64 - bit IEEE floating point
    //FIT_RGB16     //48 - bit RGB image : 3 x 16 - bit
    //FIT_RGBA16    //64 - bit RGBA image : 4 x 16 - bit
    //FIT_RGBF      //96 - bit RGB float image : 3 x 32 - bit IEEE floating point
    //FIT_RGBAF     //128 - bit RGBA float image : 4 x 32 - bit IEEE floating point

    int bpp = FreeImage_GetBPP(src);
    FREE_IMAGE_TYPE fit = FreeImage_GetImageType(src);

    int cv_type = -1;
    int cv_cvt = -1;

    switch (fit)
    {
    case FIT_UINT16: cv_type = DataType<ushort>::type; break;
    case FIT_INT16: cv_type = DataType<short>::type; break;
    case FIT_UINT32: cv_type = DataType<unsigned>::type; break;
    case FIT_INT32: cv_type = DataType<int>::type; break;
    case FIT_FLOAT: cv_type = DataType<float>::type; break;
    case FIT_DOUBLE: cv_type = DataType<double>::type; break;
    case FIT_COMPLEX: cv_type = DataType<Complex<double>>::type; break;
    case FIT_RGB16: cv_type = DataType<Vec<ushort, 3>>::type; cv_cvt = COLOR_RGB2BGR; break;
    case FIT_RGBA16: cv_type = DataType<Vec<ushort, 4>>::type; cv_cvt = COLOR_RGBA2BGRA; break;
    case FIT_RGBF: cv_type = DataType<Vec<float, 3>>::type; cv_cvt = COLOR_RGB2BGR; break;
    case FIT_RGBAF: cv_type = DataType<Vec<float, 4>>::type; cv_cvt = COLOR_RGBA2BGRA; break;
    case FIT_BITMAP:
        switch (bpp) {
        case 8: cv_type = DataType<Vec<uchar, 1>>::type; break;
        case 16: cv_type = DataType<Vec<uchar, 2>>::type; break;
        case 24: cv_type = DataType<Vec<uchar, 3>>::type; break;
        case 32: cv_type = DataType<Vec<uchar, 4>>::type; break;
        default:
            // 1, 4 // Unsupported natively
            cv_type = -1;
        }
        break;
    default:
        // FIT_UNKNOWN // unknown type
        dst = Mat(); // return empty Mat
        return;
    }

    int width = FreeImage_GetWidth(src);
    int height = FreeImage_GetHeight(src);
    int step = FreeImage_GetPitch(src);

    if (cv_type >= 0) {
        dst = Mat(height, width, cv_type, FreeImage_GetBits(src), step);
        if (cv_cvt > 0)
        {
            cvtColor(dst, dst, cv_cvt);
        }
    }
    else {

        vector<uchar> lut;
        int n = pow(2, bpp);
        for (int i = 0; i < n; ++i)
        {
            lut.push_back(static_cast<uchar>((255 / (n - 1))*i));
        }

        FIBITMAP* palletized = FreeImage_ConvertTo8Bits(src);
        BYTE* data = FreeImage_GetBits(src);
        for (int r = 0; r < height; ++r) {
            for (int c = 0; c < width; ++c) {
                dst.at<uchar>(r, c) = saturate_cast<uchar>(lut[data[r*step + c]]);
            }
        }
    }

    flip(dst, dst, 0);
}

int main()
{
    FreeImage_Initialise();
    FREE_IMAGE_FORMAT format = FreeImage_GetFileType("path_to_image", 0);
    FIBITMAP* fi_image = FreeImage_Load(format, "path_to_image");

    Mat cv_img;
    FI2MAT(fi_image, cv_img);

    return 0;
}
Miki
  • 40,887
  • 13
  • 123
  • 202
  • First of all, thank you very much! I thought I solved the problem with the cv::Mat Img(...) line of code you can finde here: http://stackoverflow.com/questions/31866822/c-freeimageopencv-16bit-image-get-distorted ..but unfortunatly, it misconvert 16bpc images so as I have a free second I'm totally going to try your method! Thank you again!! – pedro Aug 07 '15 at 14:06
  • @pedro Yeah, if you already know which type you are loading, then it's pretty simple. In general, you need something like this. I also posted an answer to your other post that solve the issue you've been having. Note that you can flip the image once it's converted to Mat (as I'm doing here), instead of flipping it as FreeImage with rotate and flip. . – Miki Aug 07 '15 at 14:45
  • Yes, I'm going to upvote it :D Unfortunatly, now instead of ruining the image it just get black :( And about the depth, I don't know it before loading the image. I'm focusing on 16bpc because with 8bpc I don't have problems.. – pedro Aug 07 '15 at 14:59
  • (I can't upvote beacuse I don't have enough reputation.. actually, I can't do anything because of reputation -.-) – pedro Aug 07 '15 at 15:00
  • @pedro don't worry about that. You are seeing it as black with imshow? – Miki Aug 07 '15 at 15:01
  • No, I don't use imshow. I apply an algorithm and then save it. The error is probably in the algo because, when I save it right after the conversion, the image is fine.. The problem is I can't understand why: the algo calculate the ratio between some pixels, so it shouldn't be a problem because 16/16 = 8/8.. – pedro Aug 07 '15 at 15:12
  • @pedro well, just for completeness I updated you other post. Can't tell right now, as I don't have enough information. You can post a new question regarding this issue. – Miki Aug 07 '15 at 15:15
  • Ok, I've made some testing. The problem is the, during his process, the algo convert the source Mat to a new format with this line of code [ source.convertTo(inputSource, CV_64FC3); ]. When the image is 8bpc, source and inputSource looks the same. When the image is 16bpc, source looks like the orignal one (and that's ok) while inputSource is in white tones. So the problem is in this convetion.. do you have any idea? P.s. I green-checked your answer, either because of all the helping, either because it actually solved the original problem :) – pedro Aug 07 '15 at 17:40
  • @pedro thanks! When the image is CV_32F or CV_64F, the values should be in [0,1] to be visible as expected. Every values greater than 1 is represented as white (also when you save it). So it may be caused by something like this. However, I recommend you to post another question with your code, otherwise I can only guess. – Miki Aug 07 '15 at 17:50
  • I opened the new thread :) http://stackoverflow.com/questions/31886952/opencv-image-conversion-goes-wrong – pedro Aug 07 '15 at 21:32
  • Thanks for this awesome post, but while trying it now with openCV4 I get quite few of these errors : ```error C2039: 'type': is not a member of 'cv::DataType'``` Any hints? happens on others too > ``` case FIT_RGB16: cv_type = DataType>::type; cv_cvt = COLOR_RGB2BGR; break; ``` – Dariusz Nov 15 '19 at 06:07
  • @Dariusz can't look in depth now... it should be `work_type` instead of `type` – Miki Nov 15 '19 at 09:14
  • @Miki sadly that does not work either, tried other ones too > `value_type, channel_type, vec_type`, just get 'cv::DataType>::channel_type': illegal use of this type as an expression` I'm lost o.O. We are trying to generate type here right? like jpg, would be CV_U8C3 for example ? I supposed I could hard code them here? – Dariusz Nov 24 '19 at 10:02