mirror of
https://github.com/opencv/opencv.git
synced 2024-12-11 22:59:16 +08:00
934e6899f8
imgcodecs: Add rgb flag for imread and imdecode #25809 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 - [x] Fix the broken code - [x] Add imread rgb test - [x] 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](https://github.com/user-attachments/files/15982949/image_test.zip) ```.cpp 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 - [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 - [x] 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
121 lines
3.4 KiB
C++
121 lines
3.4 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html
|
|
#include "test_precomp.hpp"
|
|
|
|
namespace opencv_test { namespace {
|
|
|
|
#ifdef HAVE_WEBP
|
|
|
|
TEST(Imgcodecs_WebP, encode_decode_lossless_webp)
|
|
{
|
|
const string root = cvtest::TS::ptr()->get_data_path();
|
|
string filename = root + "../cv/shared/lena.png";
|
|
cv::Mat img = cv::imread(filename);
|
|
ASSERT_FALSE(img.empty());
|
|
|
|
string output = cv::tempfile(".webp");
|
|
EXPECT_NO_THROW(cv::imwrite(output, img)); // lossless
|
|
|
|
cv::Mat img_webp = cv::imread(output);
|
|
|
|
std::vector<unsigned char> buf;
|
|
|
|
FILE * wfile = NULL;
|
|
|
|
wfile = fopen(output.c_str(), "rb");
|
|
if (wfile != NULL)
|
|
{
|
|
fseek(wfile, 0, SEEK_END);
|
|
size_t wfile_size = ftell(wfile);
|
|
fseek(wfile, 0, SEEK_SET);
|
|
|
|
buf.resize(wfile_size);
|
|
|
|
size_t data_size = fread(&buf[0], 1, wfile_size, wfile);
|
|
|
|
if(wfile)
|
|
{
|
|
fclose(wfile);
|
|
}
|
|
|
|
if (data_size != wfile_size)
|
|
{
|
|
EXPECT_TRUE(false);
|
|
}
|
|
}
|
|
|
|
EXPECT_EQ(0, remove(output.c_str()));
|
|
|
|
cv::Mat decode = cv::imdecode(buf, IMREAD_COLOR);
|
|
ASSERT_FALSE(decode.empty());
|
|
EXPECT_TRUE(cvtest::norm(decode, img_webp, NORM_INF) == 0);
|
|
|
|
cv::Mat decode_rgb = cv::imdecode(buf, IMREAD_COLOR_RGB);
|
|
ASSERT_FALSE(decode_rgb.empty());
|
|
|
|
cvtColor(decode_rgb, decode_rgb, COLOR_RGB2BGR);
|
|
EXPECT_TRUE(cvtest::norm(decode_rgb, img_webp, NORM_INF) == 0);
|
|
|
|
ASSERT_FALSE(img_webp.empty());
|
|
|
|
EXPECT_TRUE(cvtest::norm(img, img_webp, NORM_INF) == 0);
|
|
}
|
|
|
|
TEST(Imgcodecs_WebP, encode_decode_lossy_webp)
|
|
{
|
|
const string root = cvtest::TS::ptr()->get_data_path();
|
|
std::string input = root + "../cv/shared/lena.png";
|
|
cv::Mat img = cv::imread(input);
|
|
ASSERT_FALSE(img.empty());
|
|
|
|
for(int q = 100; q>=0; q-=20)
|
|
{
|
|
std::vector<int> params;
|
|
params.push_back(IMWRITE_WEBP_QUALITY);
|
|
params.push_back(q);
|
|
string output = cv::tempfile(".webp");
|
|
|
|
EXPECT_NO_THROW(cv::imwrite(output, img, params));
|
|
cv::Mat img_webp = cv::imread(output);
|
|
EXPECT_EQ(0, remove(output.c_str()));
|
|
EXPECT_FALSE(img_webp.empty());
|
|
EXPECT_EQ(3, img_webp.channels());
|
|
EXPECT_EQ(512, img_webp.cols);
|
|
EXPECT_EQ(512, img_webp.rows);
|
|
}
|
|
}
|
|
|
|
TEST(Imgcodecs_WebP, encode_decode_with_alpha_webp)
|
|
{
|
|
const string root = cvtest::TS::ptr()->get_data_path();
|
|
std::string input = root + "../cv/shared/lena.png";
|
|
cv::Mat img = cv::imread(input);
|
|
ASSERT_FALSE(img.empty());
|
|
|
|
std::vector<cv::Mat> imgs;
|
|
cv::split(img, imgs);
|
|
imgs.push_back(cv::Mat(imgs[0]));
|
|
imgs[imgs.size() - 1] = cv::Scalar::all(128);
|
|
cv::merge(imgs, img);
|
|
|
|
string output = cv::tempfile(".webp");
|
|
|
|
EXPECT_NO_THROW(cv::imwrite(output, img));
|
|
cv::Mat img_webp = cv::imread(output, IMREAD_UNCHANGED);
|
|
cv::Mat img_webp_bgr = cv::imread(output); // IMREAD_COLOR by default
|
|
EXPECT_EQ(0, remove(output.c_str()));
|
|
EXPECT_FALSE(img_webp.empty());
|
|
EXPECT_EQ(4, img_webp.channels());
|
|
EXPECT_EQ(512, img_webp.cols);
|
|
EXPECT_EQ(512, img_webp.rows);
|
|
EXPECT_FALSE(img_webp_bgr.empty());
|
|
EXPECT_EQ(3, img_webp_bgr.channels());
|
|
EXPECT_EQ(512, img_webp_bgr.cols);
|
|
EXPECT_EQ(512, img_webp_bgr.rows);
|
|
}
|
|
|
|
#endif // HAVE_WEBP
|
|
|
|
}} // namespace
|