opencv read gif c++

#include <opencv2/opencv.hpp>

int main() {
    // Step 1: Specify the path to the GIF file
    std::string gifPath = "path/to/your/file.gif";

    // Step 2: Create an instance of the cv::VideoCapture class
    cv::VideoCapture capture(gifPath);

    // Step 3: Check if the GIF file is opened successfully
    if (!capture.isOpened()) {
        std::cerr << "Error opening the GIF file." << std::endl;
        return -1;
    }

    // Step 4: Read frames from the GIF file in a loop
    cv::Mat frame;
    while (capture.read(frame)) {
        // Step 5: Perform operations on the read frame (e.g., display or process)

        // Example: Display the frame
        cv::imshow("GIF Frame", frame);

        // Step 6: Set a delay and handle key events (optional)
        int delay = 33; // Delay in milliseconds (for approximately 30 fps)
        int key = cv::waitKey(delay);

        // Break the loop if the 'ESC' key is pressed
        if (key == 27)
            break;
    }

    // Step 7: Release the VideoCapture object
    capture.release();

    // Step 8: Close any open windows (optional)
    cv::destroyAllWindows();

    return 0;
}