From 133e218484e5a748550e4adbef624120848dc827 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Sun, 24 May 2026 14:59:27 +0300 Subject: [PATCH] Unified imreadXXX functions flags. --- .../imgcodecs/include/opencv2/imgcodecs.hpp | 18 ++++++++++-------- modules/imgcodecs/test/test_animation.cpp | 2 +- modules/imgcodecs/test/test_exif.cpp | 4 ++-- modules/imgcodecs/test/test_tiff.cpp | 2 +- modules/python/test/test_imread.py | 4 ++-- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/modules/imgcodecs/include/opencv2/imgcodecs.hpp b/modules/imgcodecs/include/opencv2/imgcodecs.hpp index 21b4c7461c..bc20a5713b 100644 --- a/modules/imgcodecs/include/opencv2/imgcodecs.hpp +++ b/modules/imgcodecs/include/opencv2/imgcodecs.hpp @@ -404,22 +404,23 @@ embedded in the file (such as EXIF, XMP, etc.), depending on file format support @param filename Name of the file to be loaded. @param metadataTypes Output vector with types of metadata chunks returned in metadata, see ImageMetadataType. @param metadata Output vector of vectors or vector of matrices to store the retrieved metadata. -@param flags Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR. +@param flags Flag that can take values of cv::ImreadModes. @return The loaded image as a cv::Mat object. If the image cannot be read, the function returns an empty matrix. */ CV_EXPORTS_W Mat imreadWithMetadata( const String& filename, CV_OUT std::vector& metadataTypes, - OutputArrayOfArrays metadata, int flags = IMREAD_ANYCOLOR); + OutputArrayOfArrays metadata, int flags); /** @brief Loads a multi-page image from a file. The function imreadmulti loads a multi-page image from the specified file into a vector of Mat objects. @param filename Name of file to be loaded. @param mats A vector of Mat objects holding each page. -@param flags Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR. +@param flags Flag that can take values of cv::ImreadModes, default with cv::IMREAD_COLOR_BGR. +@note The default flags value was changed from cv::IMREAD_ANYCOLOR to cv::IMREAD_COLOR_BGR for unification. @sa cv::imread */ -CV_EXPORTS_W bool imreadmulti(const String& filename, CV_OUT std::vector& mats, int flags = IMREAD_ANYCOLOR); +CV_EXPORTS_W bool imreadmulti(const String& filename, CV_OUT std::vector& mats, int flags = IMREAD_COLOR_BGR); /** @brief Loads images of a multi-page image from a file. @@ -503,10 +504,11 @@ CV_EXPORTS_W bool imencodeanimation(const String& ext, const Animation& animatio The function imcount returns the number of pages in a multi-page image (e.g. TIFF), the number of frames in an animation (e.g. AVIF), and 1 otherwise. If the image cannot be decoded, 0 is returned. @param filename Name of file to be loaded. -@param flags Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR. +@param flags Flag that can take values of cv::ImreadModes, default with cv::IMREAD_COLOR_BGR. +@note The default flags value was changed from cv::IMREAD_ANYCOLOR to cv::IMREAD_COLOR_BGR for unification. @todo when cv::IMREAD_LOAD_GDAL flag used the return value will be 0 or 1 because OpenCV's GDAL decoder doesn't support multi-page reading yet. */ -CV_EXPORTS_W size_t imcount(const String& filename, int flags = IMREAD_ANYCOLOR); +CV_EXPORTS_W size_t imcount(const String& filename, int flags = IMREAD_COLOR_BGR); /** @brief Saves an image to a specified file. @@ -620,12 +622,12 @@ See cv::imread for the list of supported formats and flags description. @param buf Input array or vector of bytes containing the encoded image data. @param metadataTypes Output vector with types of metadata chucks returned in metadata, see cv::ImageMetadataType @param metadata Output vector of vectors or vector of matrices to store the retrieved metadata -@param flags Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR. +@param flags Flag that can take values of cv::ImreadModes. @return The decoded image as a cv::Mat object. If decoding fails, the function returns an empty matrix. */ CV_EXPORTS_W Mat imdecodeWithMetadata( InputArray buf, CV_OUT std::vector& metadataTypes, - OutputArrayOfArrays metadata, int flags = IMREAD_ANYCOLOR ); + OutputArrayOfArrays metadata, int flags ); /** @overload @param buf Input array or vector of bytes. diff --git a/modules/imgcodecs/test/test_animation.cpp b/modules/imgcodecs/test/test_animation.cpp index 2d45132ffd..a9e61bb3a5 100644 --- a/modules/imgcodecs/test/test_animation.cpp +++ b/modules/imgcodecs/test/test_animation.cpp @@ -482,7 +482,7 @@ TEST(Imgcodecs_APNG, imwritemulti_gray) string output = cv::tempfile(".png"); EXPECT_TRUE(imwrite(output, s_animation.frames)); vector read_frames; - EXPECT_TRUE(imreadmulti(output, read_frames)); + EXPECT_TRUE(imreadmulti(output, read_frames, cv::IMREAD_ANYCOLOR)); EXPECT_EQ(1, read_frames[0].channels()); read_frames.clear(); EXPECT_TRUE(imreadmulti(output, read_frames, IMREAD_UNCHANGED)); diff --git a/modules/imgcodecs/test/test_exif.cpp b/modules/imgcodecs/test/test_exif.cpp index ebc849e9e6..d176b4594b 100644 --- a/modules/imgcodecs/test/test_exif.cpp +++ b/modules/imgcodecs/test/test_exif.cpp @@ -593,11 +593,11 @@ TEST_P(ReadExif_Sanity, Check) std::vector metadata_types, metadata_types2; std::vector > metadata, metadata2; - Mat img = imreadWithMetadata(filename, metadata_types, metadata); + Mat img = imreadWithMetadata(filename, metadata_types, metadata, cv::IMREAD_ANYCOLOR); std::vector compressed; imencodeWithMetadata(".jpg", img, metadata_types, metadata, compressed); - img = imdecodeWithMetadata(compressed, metadata_types2, metadata2); + img = imdecodeWithMetadata(compressed, metadata_types2, metadata2, cv::IMREAD_ANYCOLOR); EXPECT_EQ(metadata_types, metadata_types2); EXPECT_EQ(metadata, metadata2); diff --git a/modules/imgcodecs/test/test_tiff.cpp b/modules/imgcodecs/test/test_tiff.cpp index 8afd5ec5f2..c9f21884a1 100644 --- a/modules/imgcodecs/test/test_tiff.cpp +++ b/modules/imgcodecs/test/test_tiff.cpp @@ -1102,7 +1102,7 @@ TEST(Imgcodecs_Tiff_Modes, write_multipage) ASSERT_TRUE(res); vector read_pages; - imreadmulti(tmp_filename, read_pages); + imreadmulti(tmp_filename, read_pages, cv::IMREAD_ANYCOLOR); for (size_t i = 0; i < page_count; i++) { EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), read_pages[i], pages[i]); diff --git a/modules/python/test/test_imread.py b/modules/python/test/test_imread.py index 471c786acc..89cbf065e0 100644 --- a/modules/python/test/test_imread.py +++ b/modules/python/test/test_imread.py @@ -24,13 +24,13 @@ class imread_test(NewOpenCVTests): def test_imread_with_meta(self): path = self.extraTestDataPath + '/highgui/readwrite/testExifOrientation_1.jpg' - img, meta_types, meta_data = cv.imreadWithMetadata(path) + img, meta_types, meta_data = cv.imreadWithMetadata(path, flags=cv.IMREAD_ANYCOLOR) self.assertTrue(img is not None) self.assertTrue(meta_types is not None) self.assertTrue(meta_data is not None) path = self.extraTestDataPath + '/highgui/readwrite/testExifOrientation_1.png' - img, meta_types, meta_data = cv.imreadWithMetadata(path) + img, meta_types, meta_data = cv.imreadWithMetadata(path, flags=cv.IMREAD_ANYCOLOR) self.assertTrue(img is not None) self.assertTrue(meta_types is not None) self.assertTrue(meta_data is not None)