[Feature] Encode and decode batches as independent image buffers
Problem
cv::imencode/cv::imdecode and their Python bindings process one independent image buffer per call. Dataset pipelines and augmentations that compress each image in a batch as JPEG or WebP must loop in Python and dispatch the codec once per image.
imencodemulti and imdecodemulti solve a different problem: they store or read multiple pages or frames in one container. They do not return one independent encoded buffer per input image, and formats such as JPEG do not support the required multi-page container.
Requested API
Add native batch functions that encode independent images and decode independent buffers in one public call. One possible C++ contract is:
bool imencodeBatch(
const String& ext,
InputArrayOfArrays images,
std::vector<std::vector<uchar>>& buffers,
const std::vector<int>& params = {}
);
bool imdecodeBatch(
const std::vector<std::vector<uchar>>& buffers,
OutputArrayOfArrays images,
int flags = IMREAD_COLOR_BGR
);The Python binding could expose:
ok, buffers = cv2.imencodeBatch(
".jpg",
images,
[cv2.IMWRITE_JPEG_QUALITY, 90],
)
decoded = cv2.imdecodeBatch(buffers, cv2.IMREAD_UNCHANGED)The implementation may process images independently and parallelize internally. The public API must avoid a Python-level loop and return one buffer or one decoded image per input item in the original order.
Correctness requirements
- JPEG, PNG, and WebP at minimum;
- grayscale, three-channel color, and codec-supported alpha behavior;
- shared parameters for the whole batch, with a clear extension path for per-image quality;
- deterministic output equivalent to independent calls using the same codec build and parameters;
- one failure is reported with its batch index instead of silently shortening the result;
- empty-batch behavior is defined;
- mixed input sizes are supported because independent codecs do not require homogeneous images.
Performance acceptance
Compare the batch call with independent imencode/imdecode calls under the same codec and OpenCV thread settings. Report 256×256, 512×512, and 1024×1024 images; batch sizes 2, 4, 8, and 16; JPEG and WebP; and representative quality levels.
Downstream demand
AlbumentationsX simulates compression artifacts by encoding and decoding every image. Its batch fallback currently loops in Python. albumentations-team/AlbumentationsX#51 tracks the missing native JPEG/WebP batch primitive.
Source: opencv/opencv