Skip to content

Implementation of imdecodeanimation and imencodeanimation#26964

Closed
futz12 wants to merge 1 commit intoopencv:4.xfrom
futz12:4.x
Closed

Implementation of imdecodeanimation and imencodeanimation#26964
futz12 wants to merge 1 commit intoopencv:4.xfrom
futz12:4.x

Conversation

@futz12
Copy link
Copy Markdown

@futz12 futz12 commented Feb 23, 2025

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

Supports animation's encode and decode functions to facilitate users to obtain image data without writing out the image.

Below is the sample code:

int main() {
    cv::Animation animation;
    animation.loop_count = 0;

    // random frames

    for (int i = 0; i < 10; i++) {
        cv::Mat frame(100, 100, CV_8UC3, cv::Scalar(rand() % 256, rand() % 256, rand() % 256));
        animation.frames.push_back(frame);
        animation.durations.push_back(100);
    }

    std::vector<uchar> buf;
    cv::imencodeanimation(".gif", animation, buf);

    std::cout << "buf size: " << buf.size() << std::endl;

    // write to test.gif

    std::ofstream ofs("test.gif", std::ios::binary);
    ofs.write(reinterpret_cast<const char*>(buf.data()), buf.size());
    ofs.close();

    // read from test.gif

    std::ifstream ifs("test.gif", std::ios::binary);
    std::vector<uchar> buf2((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
    ifs.close();

    cv::Animation animation2;
    cv::imdecodeanimation(buf2, animation2);

    std::cout << "animation2 loop_count: " << animation2.loop_count << std::endl;
    std::cout << "animation2 frames size: " << animation2.frames.size() << std::endl;

    return 0;
}

The program will write the following pictures in the running directory:

(Please do not click if you have photosensitive epilepsy)
https://github.com/user-attachments/assets/cc2aedee-8a5a-40c3-8829-7ecd63d20987

and output the following content on the console:

buf size: 21781
animation2 loop_count: 0
animation2 frames size: 10

@asmorkalov
Copy link
Copy Markdown
Contributor

cc @sturkmen72

@asmorkalov asmorkalov added feature category: imgcodecs pr: needs test New functionality requires minimal tests set labels Feb 24, 2025
@asmorkalov
Copy link
Copy Markdown
Contributor

Please add accuracy test for the new functions.

@futz12
Copy link
Copy Markdown
Author

futz12 commented Feb 24, 2025

Please add accuracy test for the new functions.

This is just a wrapper of the old API, and there is no difference in accuracy

@sturkmen72
Copy link
Copy Markdown
Contributor

@futz12
question 1 : if we really need this functionality ( what is use case scenario )
question 2 : what is the best way to add the functionality to avoid massive code duplication

cc @vrabaud

@futz12
Copy link
Copy Markdown
Author

futz12 commented Feb 24, 2025

@futz12 question 1 : if we really need this functionality ( what is use case scenario ) question 2 : what is the best way to add the functionality to avoid massive code duplication

cc @vrabaud

for first question:
Yes, if someone workflow involves dynamic image sequences and requires in-memory processing rather than disk-base I/O. Here is why:

  1. OpenCV`s imencode/imdecode are designed for single-frame image.
  2. imwriteanimation/imreadanimation would likely lack flexibility for in-memory workflows.

for second question:

  1. The existing imencode and imwrite themselves are also highly similar, so I don't see the need to add more complex code to improve the code reuse of imencodeanimation and imwriteanimation.
  2. OutputArray and InputArray are not compatible with Animation type, making it difficult to integrate the code into imencode/indecode.

@asmorkalov
Copy link
Copy Markdown
Contributor


Run cd /home/ci/opencv
modules/imgcodecs/src/loadsave.cpp:917: trailing whitespace.
+bool imdecodeanimation(InputArray buf, CV_OUT Animation& animation, int start, int count) 
modules/imgcodecs/src/loadsave.cpp:1132: trailing whitespace.
+bool imencodeanimation(const String& ext, const Animation& animation, CV_OUT std::vector<uchar>& buf, const std::vector<int>& params) 

@asmorkalov asmorkalov added this to the 4.12.0 milestone Feb 25, 2025
@asmorkalov
Copy link
Copy Markdown
Contributor

This is just a wrapper of the old API, and there is no difference in accuracy

Please add simple test that file read and in-memory encoding/decoding produces the same result. It's needed to cover API, check stupid typos and to have some basis for refactoring, if any.

@asmorkalov asmorkalov self-assigned this Feb 25, 2025
@asmorkalov
Copy link
Copy Markdown
Contributor

@futz12 Friendly reminder about test.

@asmorkalov
Copy link
Copy Markdown
Contributor

Replaced by #27013

@asmorkalov asmorkalov closed this Mar 7, 2025
asmorkalov added a commit that referenced this pull request Mar 12, 2025
Test for in-memory animation encoding and decoding #27013
 
Tests for #26964

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: imgcodecs feature pr: needs test New functionality requires minimal tests set

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants