Skip to content

imgcodecs: Add rgb flag for imread and imdecode#25809

Merged
asmorkalov merged 6 commits intoopencv:4.xfrom
zihaomu:imread_rgb_flag
Jul 3, 2024
Merged

imgcodecs: Add rgb flag for imread and imdecode#25809
asmorkalov merged 6 commits intoopencv:4.xfrom
zihaomu:imread_rgb_flag

Conversation

@zihaomu
Copy link
Copy Markdown
Member

@zihaomu zihaomu commented Jun 24, 2024

Try to imread images by RGB to save R-B swapping costs.

How to use it?

img_rgb = cv2.imread("PATH", IMREAD_COLOR_RGB) # OpenCV decode the image by RGB format.

TODO

  • Fix the broken code
  • Add imread rgb test
  • Speed test of rgb mode.

Performance test

file name IMREAD_COLOR IMREAD_COLOR_RGB
jpg01 284 ms 277 ms
jpg02 376 ms 366 ms
png01 62 ms 60 ms
Png02 97 ms 94 ms

Test with image_test.zip

string img_path = "/Users/mzh/work/data/image_test/png02.png";
int loop = 20;

TickMeter t;

double t0 = 10000;
for (int i = 0; i < loop; i++)
{
    t.reset();
    t.start();
    img_bgr = imread(img_path, IMREAD_COLOR);
    t.stop();

    if (t.getTimeMilli() < t0) t0 = t.getTimeMilli();
}

std::cout<<"bgr time = "<<t0<<std::endl;

t0 = 10000;
for (int i = 0; i < loop; i++)
{
    t.reset();
    t.start();
    img_rgb = imread(img_path, IMREAD_COLOR_RGB);
    t.stop();
    if (t.getTimeMilli() < t0) t0 = t.getTimeMilli();
}
std::cout<<"rgb time = "<<t0<<std::endl;

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

@zihaomu zihaomu marked this pull request as ready for review June 26, 2024 05:49
@asmorkalov
Copy link
Copy Markdown
Contributor

I propose to extend existing perf tests with RGB case. Just to be sure that the new mode does not introduce significant degradation.

Comment on lines +86 to +93
img = imread(root + "readwrite/color_palette_alpha.png", IMREAD_COLOR_RGB);
ASSERT_FALSE(img.empty());
ASSERT_TRUE(img.channels() == 3);

// pixel is red in RGB
EXPECT_EQ(img.at<Vec3b>(0, 0), Vec3b(255, 0, 0));
EXPECT_EQ(img.at<Vec3b>(0, 1), Vec3b(255, 0, 0));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to add test with RGBA output.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @asmorkalov. For consistency, IMREAD_COLOR_RGB currently converts all RGBA to RGB instead of RGBA output.

@asmorkalov
Copy link
Copy Markdown
Contributor

Perf results for system wide (Ubuntu 16.04) and own (from 3rdparty) on Jetson-tk1 (ARMv7+NEON):

Geometric mean (ms)

  Name of Test   system-1  own-1    own-1   
                                      vs    
                                   system-1 
                                  (x-factor)
Decode::JPEG     129.067  129.898    0.99   
Decode_rgb::JPEG 129.084  129.561    1.00   
Encode::JPEG     165.524  118.029    1.40   
decode::PNG       82.793  68.338     1.21   
decode_rgb::PNG   79.697  63.584     1.25   
encode::PNG      135.514  116.040    1.17 

@asmorkalov asmorkalov changed the title imgcodecs: Add rgb flag imgcodecs: Add rgb flag for imread and imdecode Jul 1, 2024
@asmorkalov asmorkalov added this to the 4.11.0 milestone Jul 1, 2024
@asmorkalov asmorkalov self-assigned this Jul 2, 2024
Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@vpisarev vpisarev self-requested a review July 3, 2024 07:51
@asmorkalov asmorkalov merged commit 934e689 into opencv:4.x Jul 3, 2024
@asmorkalov asmorkalov mentioned this pull request Jul 16, 2024
Eldies added a commit to cvat-ai/datumaro that referenced this pull request Feb 7, 2025
@Eldies Eldies mentioned this pull request Feb 10, 2025
6 tasks
zhiltsov-max added a commit to cvat-ai/datumaro that referenced this pull request Feb 10, 2025
zhiltsov-max added a commit to cvat-ai/datumaro that referenced this pull request Feb 10, 2025
Restrict opencv version to below 4.11 to keep api stable after opencv/opencv#25809
Eldies added a commit to cvat-ai/datumaro that referenced this pull request Mar 5, 2025
* syncing util/mask_tools.py

* syncing util/image.py

* keeping exif unconditionally

* syncing components/media.py

* syncing components/importer.py

* syncing util/meta_file_util.py

* moving cli/contexts/project/diff.py to cli/util/compare.py

* moving Registry and PluginRegistry to components/registry.py

* syncing components/exporter.py

* syncing components/hl_ops.py

* syncing components/dataset.py

* limiting opencv version (due to opencv/opencv#25809)

* fixes

* upper case extension fix

* fixes

* always keeping exif info

* limiting opencv version

* Update src/datumaro/components/media.py

Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>

* test for reading exif orientation

* changelog entry

* fixed isort

* fixed test

* fixed changelog

* Update src/datumaro/components/hl_ops/__init__.py

Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>

* fixing filter examples

* hl_ops tests

* syncing plugins/data_formats/celeba

* syncing plugins/data_formats/cifar.py

* setting DETECT_CONFIDENCE for yolo formats

* syncing plugins/data_formats/image_dir.py

* better detection for yolo classification importer

* syncing plugins/data_formats/imagenet.py and plugins/data_formats/imagenet_txt.py

* syncing plugins/data_formats/camvid.py

* syncing tests/integration/cli/test_detect_format.py

* syncing cli/util/project.py

* syncing tests/integration/cli/test_filter.py

* syncing tests/integration/cli/test_transform.py

* yolo streaming exporter

* syncing plugins/data_formats/coco

* Update src/datumaro/components/media.py

Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>

* Update src/datumaro/components/media.py

Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>

* Update src/datumaro/components/media.py

Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>

* Update tests/unit/test_video.py

Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>

* Update src/datumaro/components/registry.py

Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>

* coco find_images_dir do not fail if images folder doe not exist - because cvat needs to be able to export and then import dataset without images

* coco find_rootpath do not fail if path does not end with ANNOTATIONS_DIR - because cvat needs it

* fixes

* fix linters

* tests for HLOps.compare

* syncing tests/unit/test_image.py

* accounting for the new flag in cv2

* syncing components/importer.py

* fixes

* fixes

* fixes

* fixes

* tests in test_masks.py from upstream

* a bit of info on ImageColorChannel.UNCHANGED

* fixing wrong merge

* removing bad changes

* rolling back changes in test

* do not recollect subset names in StreamDatasetStorage if transformations do not change subsets

* fixes

* Refactor with_subset_dirs

* Support detect() calls with no return value

* Update importer detection confidence

* Lower the default confidence

* Align default format detection confidence in detector and importer

* Clean imports

* syncing tests/conftest.py and tests/unit/data_formats/conftest.py

* syncing imagenet tests

* test new yolo classification detetection behaviour

* syncing tests/unit/test_format_detection.py

* Apply suggestions from code review

Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>

* fixes

* fixes

* Apply suggestions from code review

Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>

* small fixes

* basic streaming tests for coco and yolo formats

* returning previous tests and behaviour for coco

* Improve function name

* raising error on unknown image id

* test coco streaming

* test yolo streaming

---------

Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

is that possible to change default image from BGR to RGB at 5.0 version?

3 participants