diff --git a/modules/photo/doc/decolor.rst b/modules/photo/doc/decolor.rst new file mode 100644 index 000000000..061b2b18c --- /dev/null +++ b/modules/photo/doc/decolor.rst @@ -0,0 +1,20 @@ +Decolorization +============== + +.. highlight:: cpp + +decolor +------- + +Transforms a color image to a grayscale image. It is a basic tool in digital printing, stylized black-and-white photograph rendering, and in many single channel image processing applications. + +.. ocv:function:: void decolor( InputArray src, OutputArray grayscale, OutputArray color_boost ) + + :param src: Input 8-bit 3-channel image. + + :param grayscale: Output 8-bit 1-channel image. + + :param color_boost: Output 8-bit 3-channel image. + +This function is to be applied on color images. + diff --git a/modules/photo/include/opencv2/photo.hpp b/modules/photo/include/opencv2/photo.hpp index 6f5868dd4..e656b93e8 100644 --- a/modules/photo/include/opencv2/photo.hpp +++ b/modules/photo/include/opencv2/photo.hpp @@ -288,7 +288,7 @@ public: CV_EXPORTS_W Ptr createMergeRobertson(); -CV_EXPORTS_W void decolor(InputArray src, OutputArray grayscale); +CV_EXPORTS_W void decolor(InputArray src, OutputArray grayscale, OutputArray color_boost); } // cv diff --git a/modules/photo/src/contrast_preserve.cpp b/modules/photo/src/contrast_preserve.cpp index 6d4a2d131..570ef8059 100644 --- a/modules/photo/src/contrast_preserve.cpp +++ b/modules/photo/src/contrast_preserve.cpp @@ -17,14 +17,17 @@ int rounding(double a) return int(a + 0.5); } -void cv::decolor(InputArray _src, OutputArray _dst) +void cv::decolor(InputArray _src, OutputArray _dst, OutputArray _boost) { - Mat I = _src.getMat(); + Mat I = _src.getMat(); _dst.create(I.size(), CV_8UC1); Mat dst = _dst.getMat(); - if(!I.data ) - { + _boost.create(I.size(), CV_8UC3); + Mat color_boost = _boost.getMat(); + + if(!I.data ) + { cout << "Could not open or find the image" << endl ; return; } @@ -162,5 +165,40 @@ void cv::decolor(InputArray _src, OutputArray _dst) Gray.convertTo(dst,CV_8UC1,255); + /////////////////////////////////// Contrast Boosting ///////////////////////////////// + Mat lab = Mat(img.size(),CV_8UC3); + Mat color = Mat(img.size(),CV_8UC3); + Mat l = Mat(img.size(),CV_8UC1); + Mat a = Mat(img.size(),CV_8UC1); + Mat b = Mat(img.size(),CV_8UC1); + + cvtColor(I,lab,COLOR_BGR2Lab); + + int h1 = img.size().height; + int w1 = img.size().width; + + for(int i =0;i(i,j) = lab.at(i,j*3+0); + a.at(i,j) = lab.at(i,j*3+1); + b.at(i,j) = lab.at(i,j*3+2); + } + + for(int i =0;i(i,j) = 255.0*Gray.at(i,j); + } + + for(int i =0;i(i,j*3+0) = l.at(i,j); + lab.at(i,j*3+1) = a.at(i,j); + lab.at(i,j*3+2) = b.at(i,j); + } + + cvtColor(lab,color_boost,COLOR_Lab2BGR); } diff --git a/modules/photo/test/test_decolor.cpp b/modules/photo/test/test_decolor.cpp index 3edec908a..94f78a7df 100644 --- a/modules/photo/test/test_decolor.cpp +++ b/modules/photo/test/test_decolor.cpp @@ -16,19 +16,24 @@ TEST(Photo_Decolor, regression) { string folder = string(cvtest::TS::ptr()->get_data_path()) + "decolor/"; string original_path = folder + "color_image_1.png"; - string expected_path = folder + "grayscale_image_1.png"; + string expected_path1 = folder + "grayscale_image_1.png"; + string expected_path2 = folder + "color_boost_image_1.png"; Mat original = imread(original_path, IMREAD_COLOR); - Mat expected = imread(expected_path, IMREAD_GRAYSCALE); + Mat expected1 = imread(expected_path1, IMREAD_GRAYSCALE); + Mat expected2 = imread(expected_path2, IMREAD_COLOR); ASSERT_FALSE(original.empty()) << "Could not load input image " << original_path; - ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path; + ASSERT_FALSE(expected1.empty()) << "Could not load reference image " << expected_path1; + ASSERT_FALSE(expected2.empty()) << "Could not load reference image " << expected_path2; - Mat result; - decolor(original, result); + Mat grayscale, color_boost; + decolor(original, grayscale, color_boost); - DUMP(result, expected_path + ".res.png"); + DUMP(grayscale, expected_path1 + ".grayscale.png"); + DUMP(color_boost, expected_path2 + ".color_boost.png"); - ASSERT_EQ(0, norm(result != expected)); + ASSERT_EQ(0, norm(grayscale != expected1)); + ASSERT_EQ(0, norm(color_boost != expected2)); }