Convert GIF images to mat in opencv using freeimage

Source: Internet
Author: User

There is a version for converting GIF to iplimg on the Internet, but I am used to the C ++ interface, so I wrote a version for converting to mat. The code is relatively simple.

1. Read files

 1     int MatFun::gif2Mat(char* data, size_t dataSize, vector<Mat>& gifImgs, Mat& singleImg) 2     { 3         /* initialise 4         ->open memory 5         ->getImageType 6         ->load bitmaps 7         ->bitmaps to Mat 8         ->free all resource 9         */10         // condition1: data is not null11         if (NULL == data || dataSize == 0)12             return -1;13         FreeImage_Initialise();14         FIMEMORY* memory = FreeImage_OpenMemory((BYTE*)data, dataSize);15         if (NULL==memory)16         {17             FreeImage_DeInitialise();   //condition 2: memory is not null 18         }19 20         FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(memory);21         if (FIF_UNKNOWN == fif)    //condition 3: type of image should be know22         {23             fprintf(stderr, "unknown type of image");24             if (NULL != memory) FreeImage_CloseMemory(memory);25             FreeImage_DeInitialise();26             return -1;27         }28         else if (FIF_GIF != fif)  //condition 4: type of image should be GIF29         {30             if (NULL != memory) FreeImage_CloseMemory(memory);31             FreeImage_DeInitialise();32             return 0;33         }34 35         FIMULTIBITMAP * fiBmp = FreeImage_LoadMultiBitmapFromMemory(fif, memory, GIF_DEFAULT);36         if (NULL==fiBmp)37         {38             if (NULL != memory) FreeImage_CloseMemory(memory);   //condition 5: bitmaps is not null39             FreeImage_DeInitialise();40             return -1;41         }42 43         int num = FreeImage_GetPageCount(fiBmp);44 45         for (int i = 0; i < num; i++)46         {47             if (i==0 || i==2)48             {49                 FIBITMAP *mfibmp = FreeImage_LockPage(fiBmp, i);50                 if (mfibmp)     51                 {52                     Mat dst = bitMap2Mat(mfibmp, fif);53                     FreeImage_UnlockPage(fiBmp, mfibmp, false);54                     if (dst.empty())55                     {56                         if (NULL != memory) FreeImage_CloseMemory(memory);57                         if (NULL != fiBmp) FreeImage_CloseMultiBitmap(fiBmp, GIF_DEFAULT);58                         FreeImage_DeInitialise();59                         return -1;60                     }61                     gifImgs.push_back(dst);62                     dst.release();63                 }64             }65         }66 67         if (NULL != memory)68             FreeImage_CloseMemory(memory);69         if (NULL != fiBmp)70             FreeImage_CloseMultiBitmap(fiBmp);71         FreeImage_DeInitialise();72 73         return 0;74     }

In fact, this part of the function is very simple. Most of the Code is checked, and the initialization, opening the memory, and reading bitmap in the memory are performed.

Ii. Converting GIF to mat

Mat matfun: bitmap2mat (fiber map * fiber MP, const free_image_format & FIF) {If (null = fiber MP | fif_gif! = FIF) return MAT (); int width = freeimage_getwidth (fiber MP); int Height = freeimage_getheight (fiber); byte intensity; byte * pintensity = & intensity; if (freeimage_getbpp (fiber )! = 8) maid = freeimage_convertto8bits (fiber MP); // must be converted to 8bit rgbquad * pixels = new rgbquad; pixels = freeimage_getpalette (fiber MP); mat img =: mat: zeros (height, width, cv_8uc3); uchar * P; For (INT I = 0; I 

Specifically, you can convert byte pixels into the pixel values in the mat. Note that the mat of the RGB image contains a pixel value that contains three channels. The code can be well understood by knowing this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.