$darkmode
404 Not Found

404 Not Found


nginx
OpenCV 4.11.0
Open Source Computer Vision
BEGIN_CUSTOM_MATHJAX // END_CUSTOM_MATHJAX
samples/cpp/tutorial_code/imgcodecs/animations.cpp

An example to show usage of cv::imreadanimation and cv::imwriteanimation functions. Check the corresponding tutorial for more details

#include <iostream>
using namespace cv;
int main( int argc, const char** argv )
{
std::string filename = argc > 1 ? argv[1] : "animated_image.webp";
if (argc == 1)
{
Animation animation_to_save;
Mat image(128, 256, CV_8UC4, Scalar(150, 150, 150, 255));
int duration = 200;
for (int i = 0; i < 10; ++i) {
animation_to_save.frames.push_back(image.clone());
putText(animation_to_save.frames[i], format("Frame %d", i), Point(30, 80), FONT_HERSHEY_SIMPLEX, 1.5, Scalar(255, 100, 0, 255), 2);
animation_to_save.durations.push_back(duration);
}
imwriteanimation("animated_image.webp", animation_to_save, { IMWRITE_WEBP_QUALITY, 100 });
}
Animation animation;
bool success = imreadanimation(filename, animation);
if (!success) {
std::cerr << "Failed to load animation frames\n";
return -1;
}
while (true)
for (size_t i = 0; i < animation.frames.size(); ++i) {
imshow("Animation", animation.frames[i]);
int key_code = waitKey(animation.durations[i]); // Delay between frames
if (key_code == 27)
exit(0);
}
return 0;
}
Point2i Point
Definition: modules/core/include/opencv2/core/types.hpp:209
Scalar_< double > Scalar
Definition: modules/core/include/opencv2/core/types.hpp:709
#define CV_8UC4
Definition: core/include/opencv2/core/hal/interface.h:91
String format(const char *fmt,...)
Returns a text string formatted using the printf-like expression.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
@ IMWRITE_WEBP_QUALITY
For WEBP, it can be a quality from 1 to 100 (the higher is the better). By default (without any param...
Definition: imgcodecs.hpp:102
CV_EXPORTS_W bool imreadanimation(const String &filename, CV_OUT Animation &animation, int start=0, int count=INT16_MAX)
Loads frames from an animated image file into an Animation structure.
CV_EXPORTS_W bool imwriteanimation(const String &filename, const Animation &animation, const std::vector< int > &params=std::vector< int >())
Saves an Animation to a specified file.
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
Draws a text string.
@ FONT_HERSHEY_SIMPLEX
normal size sans-serif font
Definition: imgproc/include/opencv2/imgproc.hpp:901
int main(int argc, char *argv[])
Definition: highgui_qt.cpp:3
Definition: core/include/opencv2/core.hpp:107