OpenCV4 打开 gif (FreeImage)

准备

FreeImage 库,自己编译或下载

这里只提供 macOS 版的静态库 点击这里下载。密码: p2lb。

代码:

现在仅支持读取 CV_8UC3 / CV8UC4 的 gif 图。

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

void show_gif2(const char* path, int interval_ms) {
    // path: 文件路径
    // interval ms:每张图片显示间隔
    
    FreeImage_Initialise();
    
    // 获取文件名,然后作为窗口名(没必要)
    char fn[strlen(path)];
    const char *ptr = strrchr(path, '/');
    sprintf(fn, "%s", ptr+1);
    String title(fn);
    namedWindow(title);
    
    // load 
    FIMULTIBITMAP* fimb = FreeImage_OpenMultiBitmap(FIF_GIF, path,
                                                    0, 1, 0, GIF_PLAYBACK);
    // frame count
    int image_count = FreeImage_GetPageCount(fimb);
    if (!image_count)
        return;
    // pre define size
    int width = 0, height = 0; 
    // other buffer variables
    int mat_type = 0;
    int channels = 0;
    RGBQUAD ptrPalette;
    Mat mat;
    
    for (int cur_frame = 0; cur_frame < image_count; cur_frame++) {
        FIBITMAP* fib_frame = FreeImage_LockPage(fimb, cur_frame);
        
        if (cur_frame == 0) {
            uint min_bits = FreeImage_GetBPP(fib_frame);
            // 现在只适配单个颜色8位,RGB 或 RGBA 的 mat,因此 3*8 或 4*8 位:
            assert(min_bits == 32 || min_bits == 24);
            
            FREE_IMAGE_COLOR_TYPE color_type = FreeImage_GetColorType(fib_frame);
            width = FreeImage_GetWidth(fib_frame);
            height = FreeImage_GetHeight(fib_frame);
            
            if (color_type == FIC_RGBALPHA) {
                mat_type = CV_8UC4;
                channels = 4;
            } else if (color_type == FIC_RGB) {
                mat_type = CV_8UC3;
                channels = 3;
            } else {
                assert(false); // 只适配 RGB 和 RGBA
            }
            mat = Mat(height, width, mat_type);
        }
        
        int base;
        for (int i = 0; i < height; i++) {
            // FreeImage_GetPixel 从左下角开始,因此需要翻转 y 轴
            uchar* ptrImgDataPerLine = mat.data + (height - i - 1)*mat.step;
            for(int j = 0; j < width; j++) {
                FreeImage_GetPixelColor(fib_frame, j, i, &ptrPalette);
                base = channels*j;
                ptrImgDataPerLine[base] = ptrPalette.rgbBlue;
                ptrImgDataPerLine[base+1] = ptrPalette.rgbGreen;
                ptrImgDataPerLine[base+2] = ptrPalette.rgbRed;
                if (channels == 4)
                    ptrImgDataPerLine[base+3] = ptrPalette.rgbReserved;
            }
        }
        imshow(title, mat);
        FreeImage_UnlockPage(fimb, fib_frame, 1);
        waitKey(interval_ms);
    }
    FreeImage_DeInitialise();
} 

其他

可以稍微修改一下代码,即可实现获得 mat 数组或通过回调操作每一帧。

若要在 Debian 11 上使用 OpenCV 4,您需要确保正确设置了 pkg-config。可以按照以下步骤操作: 1. 安装 OpenCV 4 使用以下命令安装 OpenCV 4: ``` sudo apt-get update sudo apt-get install libopencv-dev ``` 2. 设置 pkg-config 确保在 `/usr/lib/pkgconfig/` 目录下有一个名为 `opencv4.pc` 的文件。如果该文件不存在,则可以手动创建它。使用以下命令创建该文件: ``` sudo touch /usr/lib/pkgconfig/opencv4.pc ``` 编辑该文件,并将以下内容添加到其中: ``` prefix=/usr exec_prefix=${prefix} libdir=${exec_prefix}/lib includedir_old=${prefix}/include includedir_new=${prefix}/include/opencv4 includedir=${includedir_new} Name: OpenCV Description: Open Source Computer Vision Library Version: 4.5.4 Libs: -L${libdir} -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_bgsegm -lopencv_dnn_objdetect -lopencv_dpm -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_surface_matching -lopencv_videoio -lopencv_dnn_superres -lopencv_videostab -lopencv_aruco -lopencv_dnn -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn_modern -lopencv_face -lopencv_plot -lopencv_ide -lopencv_synthia -lopencv_dnn -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_imgcodecs -lopencv_video -lopencv_ml -lopencv_highgui -lopencv_core Cflags: -I${includedir} -I${includedir}/opencv -I${includedir}/opencv2 -I${includedir}/opencv2/face -I${includedir}/opencv2/rgbd -I${includedir}/opencv2/saliency -I${includedir}/opencv2/sfm -I${includedir}/opencv2/stereo -I${includedir}/opencv2/structured_light -I${includedir}/opencv2/superres -I${includedir}/opencv2/tracking -I${includedir}/opencv2/xfeatures2d -I${includedir}/opencv2/ximgproc ``` 保存并关闭文件。 现在,当您使用 OpenCV 4 编译代码时,您应该可以使用 pkg-config 来获得必要的编译选项和库路径。例如,您可以使用以下命令来获取编译选项: ``` pkg-config --cflags opencv4 ``` 或者,您可以使用以下命令来获取库路径: ``` pkg-config --libs opencv4 ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值