diff --git a/libseam_carving/energy.cpp b/libseam_carving/energy.cpp index 0566fce..ab3650b 100644 --- a/libseam_carving/energy.cpp +++ b/libseam_carving/energy.cpp @@ -7,25 +7,21 @@ namespace seam_carving::energy { void ComputeEnergy(cv::InputArray in_img, cv::OutputArray out_img) { - cv::GaussianBlur(in_img, out_img, cv::Size(3, 3), 0, 0, cv::BORDER_DEFAULT); + // blur to remove noise + cv::GaussianBlur(in_img, out_img, cv::Size(3, 3), 0, 0); - // conversion should be skipped if img is grayscale already - if (in_img.channels() != 1) - cv::cvtColor(out_img, out_img, cv::COLOR_BGR2GRAY, 0); + // conversion should be skipped if the incoming img is grayscale already + if (out_img.channels() != 1) + cv::cvtColor(out_img, out_img, cv::COLOR_BGR2GRAY, 1); /** * apply kernel. set the depth on the kernel results to be 16-bit * signed ints to avoid overflow since img.depth() will be CV_8U; + * TODO(#23): investigate if Scharr is better */ cv::Mat x_nrg, y_nrg; - /** TODO(#23): investigate if Scharr is better */ - if (in_img.channels() != 1) { - cv::Sobel(out_img, x_nrg, CV_16S, 1, 0); - cv::Sobel(out_img, y_nrg, CV_16S, 0, 1); - } else { - cv::Sobel(in_img, x_nrg, CV_16S, 1, 0); - cv::Sobel(in_img, y_nrg, CV_16S, 0, 1); - } + cv::Sobel(out_img, x_nrg, CV_16S, 1, 0); + cv::Sobel(out_img, y_nrg, CV_16S, 0, 1); std::cout << "post-sobel!\n"; std::cout << "x\n" << x_nrg << "\n"; @@ -38,9 +34,11 @@ namespace seam_carving::energy { std::cout << "post-abs!\n"; std::cout << "x\n" << x_nrg << "\n"; std::cout << "y\n" << y_nrg << "\n\n"; - cv::addWeighted(x_nrg, 0.5, y_nrg, 0.5, 0, out_img, CV_8U); - std::cout << out_img.getMat() << "out\n"; + cv::addWeighted(x_nrg, 0.5, y_nrg, 0.5, 0, out_img); + + std::cout << "out\n"; + std::cout << out_img.getMat() << "\n\n"; } void ComputeVerticalMap(cv::InputArray sobel, cv::OutputArray output) { diff --git a/tests/data.json b/tests/data.json index d0f71e0..1a28185 100644 --- a/tests/data.json +++ b/tests/data.json @@ -1,47 +1,47 @@ [ { - "test_id": 0, + "test_id": 5, "input": { "rows": 6, "cols": 6, "type": 16, "data": [ - 117, - 189, - 114, - 0, + 137, + 5, + 146, + 204, + 17, + 153, + 96, + 146, + 182, + 75, + 250, + 69, 26, - 86, - 230, - 32, - 210, - 142, - 183, - 40, - 99, - 38, - 144, - 88, - 202, - 68, - 15, - 151, - 171, - 243, + 137, + 67, + 133, 26, - 27, - 143, - 129, - 0, - 255, 221, - 3, - 6, - 32, - 176, - 47, - 252, - 213 + 54, + 178, + 87, + 200, + 163, + 202, + 180, + 187, + 31, + 199, + 155, + 244, + 255, + 111, + 76, + 87, + 148, + 78 ] }, "energy": { @@ -51,40 +51,40 @@ "type": 0, "data": [ 0, - 11, - 32, + 71, 54, - 40, + 7, + 21, 0, - 91, - 80, - 25, - 111, - 126, - 56, - 43, - 95, - 107, - 79, + 9, + 62, + 63, + 29, + 12, + 33, + 34, + 54, + 28, + 57, + 99, + 73, + 93, + 73, + 34, + 104, 115, - 22, - 0, - 96, - 126, - 37, - 177, - 50, - 40, - 116, - 184, - 68, - 192, - 128, + 58, + 79, + 136, + 21, + 136, + 95, + 27, 0, - 51, 128, - 115, - 31, + 19, + 108, + 68, 0 ] }, @@ -93,41 +93,41 @@ "cols": 6, "type": 2, "data": [ - 163, - 171, - 192, - 214, - 296, - 256, - 174, - 163, - 160, - 326, - 326, - 256, - 83, - 135, - 243, - 215, - 251, - 200, - 40, - 136, - 225, - 136, - 276, - 178, - 40, + 244, + 320, + 432, + 447, + 447, + 337, + 244, + 304, + 415, + 319, + 411, + 337, 116, - 235, - 99, - 192, - 128, + 160, + 258, + 295, + 329, + 261, + 119, + 93, + 231, + 240, + 211, + 180, + 36, + 201, + 330, + 281, + 141, + 59, 0, - 51, 128, - 115, - 31, + 128, + 128, + 62, 0 ] }, @@ -136,41 +136,41 @@ "cols": 6, "type": 2, "data": [ - 130, - 130, - 126, - 94, - 40, + 393, + 393, + 317, + 256, + 128, 0, - 221, - 199, - 119, - 151, - 126, - 56, - 242, - 214, - 258, - 205, - 137, - 22, - 214, - 321, + 459, + 505, + 444, + 189, + 150, + 76, + 344, + 331, + 354, + 234, 225, - 174, - 199, - 50, - 318, - 341, - 283, - 99, - 192, + 81, + 414, + 321, + 264, + 240, + 211, 128, - 278, - 278, - 227, - 146, - 31, + 357, + 465, + 392, + 281, + 141, + 59, + 446, + 446, + 318, + 190, + 62, 0 ] } @@ -185,7 +185,7 @@ }, { "row": 1, - "col": 1 + "col": 0 }, { "row": 2, @@ -193,7 +193,7 @@ }, { "row": 3, - "col": 0 + "col": 1 }, { "row": 4, @@ -209,23 +209,23 @@ "dir": "HORZ", "coords": [ { - "row": 0, + "row": 2, "col": 0 }, { - "row": 0, + "row": 3, "col": 1 }, { - "row": 1, + "row": 3, "col": 2 }, { - "row": 0, + "row": 2, "col": 3 }, { - "row": 0, + "row": 1, "col": 4 }, { @@ -239,36 +239,36 @@ "cols": 5, "type": 16, "data": [ - 189, - 114, - 0, - 26, - 86, - 230, - 210, - 142, - 183, - 40, - 38, - 144, - 88, - 202, - 68, - 151, - 171, - 243, - 26, + 6, 27, + 212, + 235, + 33, + 75, + 129, + 226, + 78, + 179, + 87, + 240, + 132, + 97, + 247, + 138, + 80, + 163, + 251, + 87, 129, - 0, - 255, 221, - 3, - 32, - 176, - 47, - 252, - 213 + 102, + 0, + 90, + 184, + 234, + 36, + 169, + 110 ] }, "remove - horizontal": { @@ -276,36 +276,36 @@ "cols": 6, "type": 16, "data": [ - 230, - 32, - 114, - 142, - 183, - 40, - 99, - 38, - 144, - 88, - 202, - 68, - 15, - 151, - 171, - 243, - 26, + 37, + 6, 27, - 143, + 212, + 235, + 179, + 195, + 75, + 129, + 226, + 97, + 247, + 138, + 87, + 240, + 163, + 251, + 87, + 83, 129, - 0, - 255, 221, - 3, - 6, - 32, - 176, - 47, - 252, - 213 + 102, + 0, + 90, + 73, + 184, + 234, + 36, + 169, + 110 ] }, "insert - vertical": { @@ -313,48 +313,48 @@ "cols": 7, "type": 16, "data": [ - 117, - 117, - 189, - 114, - 0, - 26, - 86, - 230, - 32, - 32, - 210, - 142, - 183, - 40, - 99, - 99, - 38, - 144, - 88, - 202, - 68, - 15, - 15, - 151, - 171, - 243, - 26, + 37, + 37, + 6, 27, - 143, - 143, + 212, + 235, + 33, + 195, + 195, + 75, + 129, + 226, + 78, + 179, + 218, + 218, + 87, + 240, + 132, + 97, + 247, + 138, + 155, + 155, + 80, + 163, + 251, + 87, + 83, + 83, 129, - 0, - 255, 221, - 3, - 6, - 6, - 32, - 176, - 47, - 252, - 213 + 102, + 0, + 90, + 73, + 73, + 184, + 234, + 36, + 169, + 110 ] }, "insert - horizontal": { @@ -362,50 +362,50 @@ "cols": 6, "type": 16, "data": [ - 117, - 189, - 114, - 0, - 26, - 86, - 117, - 189, - 210, - 0, - 26, - 86, - 230, - 32, - 210, - 142, - 183, - 40, - 99, - 38, - 144, - 88, - 202, - 68, - 15, - 151, - 171, - 243, - 26, + 37, + 6, 27, - 143, + 212, + 235, + 33, + 195, + 75, + 129, + 226, + 78, + 33, + 218, + 87, + 240, + 132, + 78, + 179, + 218, + 155, + 80, + 132, + 97, + 247, + 138, + 155, + 80, + 163, + 251, + 87, + 83, 129, - 0, - 255, 221, - 3, - 6, - 32, - 176, - 47, - 252, - 213 + 102, + 0, + 90, + 73, + 184, + 234, + 36, + 169, + 110 ] } } } -] +] \ No newline at end of file diff --git a/tests/data/Seam Carving Tests - TestId_5.csv b/tests/data/Seam Carving Tests - TestId_5.csv new file mode 100644 index 0000000..3810a15 --- /dev/null +++ b/tests/data/Seam Carving Tests - TestId_5.csv @@ -0,0 +1,72 @@ +HEADERS,,,,,, +MATRIX ROWS,6,,,,, +MATRIX COLS,6,,,,, +COORD_RANGE_MAX,99,,,,, +MATRIX,,,,,, +TYPE,CV_8UC3,,,,, +137,5,146,204,17,153, +96,146,182,75,250,69, +26,137,67,133,26,221, +54,178,87,200,163,202, +180,187,31,199,155,244, +255,111,76,87,148,78, +SOBEL,,,,,, +TYPE,CV_8UC1,,,,, +0,71,54,7,21,0, +9,62,63,29,12,33, +34,54,28,57,99,73, +93,73,34,104,115,58, +79,136,21,136,95,27, +0,128,19,108,68,0, +VERT MAP,,,,,, +TYPE,CV_16UC1,,,,, +244,320,432,447,447,337, +244,304,415,319,411,337, +116,160,258,295,329,261, +119,93,231,240,211,180, +36,201,330,281,141,59, +0,128,128,128,62,0, +HORZ MAP,,,,,, +TYPE,CV_16UC1,,,,, +393,393,317,256,128,0, +459,505,444,189,150,76, +344,331,354,234,225,81, +414,321,264,240,211,128, +357,465,392,281,141,59, +446,446,318,190,62,0, +VERT SEAM,,,,,, +0.00,1.00,2.00,3.01,4.00,5.00, +HORZ SEAM,,,,,, +2.00,3.01,3.02,2.03,1.04,0.05, +REMOVE VERT,,,,,, +TYPE,CV_8UC3,,,,, +6,27,212,235,33,, +75,129,226,78,179,, +87,240,132,97,247,, +138,80,163,251,87,, +129,221,102,0,90,, +184,234,36,169,110,, +REMOVE HORZ,,,,,, +TYPE,CV_8UC3,,,,, +37,6,27,212,235,179, +195,75,129,226,97,247, +138,87,240,163,251,87, +83,129,221,102,0,90, +73,184,234,36,169,110, +INSERT VERT,,,,,, +TYPE,CV_8UC3,,,,, +37,37,6,27,212,235,33 +195,195,75,129,226,78,179 +218,218,87,240,132,97,247 +138,155,155,80,163,251,87 +83,83,129,221,102,0,90 +73,73,184,234,36,169,110 +INSERT HORZ,,,,,, +TYPE,CV_8UC3,,,,, +37,6,27,212,235,33, +195,75,129,226,78,33, +218,87,240,132,78,179, +218,155,80,132,97,247, +138,155,80,163,251,87, +83,129,221,102,0,90, +73,184,234,36,169,110, \ No newline at end of file diff --git a/tests/generator.cpp b/tests/generator.cpp index a566b73..5d0c088 100644 --- a/tests/generator.cpp +++ b/tests/generator.cpp @@ -31,11 +31,11 @@ vector generateMatrix(size_t size) { * Applies the Sobel convolution operator to the input. */ void applySobel(cv::InputArray input, cv::OutputArray output) { - cv::Mat input_mat = input.getMat(); - cv::Mat x_nrg, y_nrg; + cv::GaussianBlur(input, output, cv::Size(3, 3), 0, 0); - cv::Sobel(input_mat, x_nrg, CV_16S, 1, 0); - cv::Sobel(input_mat, y_nrg, CV_16S, 0, 1); + cv::Mat x_nrg, y_nrg; + cv::Sobel(output, x_nrg, CV_16S, 1, 0); + cv::Sobel(output, y_nrg, CV_16S, 0, 1); // convert back to CV_8U depth and merge cv::convertScaleAbs(x_nrg, x_nrg); @@ -60,4 +60,4 @@ int main() { cout << input << "\n\n"; cout << "SOBEL\n"; cout << output << "\n\n"; -} \ No newline at end of file +} diff --git a/tests/loader.cpp b/tests/loader.cpp index 2890202..53d5814 100644 --- a/tests/loader.cpp +++ b/tests/loader.cpp @@ -27,7 +27,7 @@ int main() { nlohmann::ordered_json data; // parse test files - for (int test_id = 0; test_id < test_count; ++test_id) { + for (int test_id = 5; test_id < test_count; ++test_id) { // build file name file_name = file_prefix + std::to_string(test_id) + file_ext; Parser p(file_name, test_id); diff --git a/tests/parser.cpp b/tests/parser.cpp index ae8e18b..e41917b 100644 --- a/tests/parser.cpp +++ b/tests/parser.cpp @@ -6,7 +6,7 @@ void Parser::ParseMatrix( const std::size_t& rows, const std::size_t& cols, - const std::string &label, const std::string &parent_label) { + const std::string& label, const std::string& parent_label) { int type = ParseMatrixType(); std::vector res; diff --git a/tests/unit/energy.test.cpp b/tests/unit/energy.test.cpp index 65fd9ec..2896ecb 100644 --- a/tests/unit/energy.test.cpp +++ b/tests/unit/energy.test.cpp @@ -36,6 +36,7 @@ TEST_P(EnergyTest, ComputeEnergy) { cv::Mat expected = energy_data.sobel_matrix; cv::Mat actual; + std::cout << sct::PrintWithLabel(input, "input") << "\n"; std::cout << sct::PrintWithLabel(expected, "expected") << "\n"; sce::ComputeEnergy(input, actual);