From ffe31d8d7c8f1a23d6fca329c0b65aa17668e729 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Mon, 24 Oct 2016 08:40:49 +0300 Subject: [PATCH 01/13] add test for Threshold --- src/workaround.cpp | 10 ++++++++-- test/test_workaround.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/test_workaround.cpp diff --git a/src/workaround.cpp b/src/workaround.cpp index 2eba458..9304f0c 100644 --- a/src/workaround.cpp +++ b/src/workaround.cpp @@ -1,10 +1,16 @@ #include "workaround.hpp" -#include +#include using namespace std; void MatrixProcessor::Threshold(unsigned char* const data, const int width, const int height, const int threshold) { - // TODO: Add thresholding logic here. + if (data != nullptr) { + for (int i = 0; i < width * height; i++) + if (data[i] < threshold) + data[i] = 0; + } else { + throw std::invalid_argument("Data is empty!"); + } } diff --git a/test/test_workaround.cpp b/test/test_workaround.cpp new file mode 100644 index 0000000..5645705 --- /dev/null +++ b/test/test_workaround.cpp @@ -0,0 +1,25 @@ +#include + +#include "workaround.hpp" + +TEST(MatrixProcessor, No_Throw_When_DataOK) { + MatrixProcessor mp; + int width = 4, height = 4; + unsigned char* data; + unsigned char threshold = 'e'; + + data = new unsigned char[width * height]; + for (int i = 0; i < width * height; i++) + data[i] = static_cast('a' + i); + + EXPECT_NO_THROW(mp.Threshold(data, width, height, threshold)); +} + +TEST(MatrixProcessor, Throw_When_DataFail) { + MatrixProcessor mp; + int width = 4, height = 4; + unsigned char* data; + unsigned char threshold = 'e'; + + EXPECT_ANY_THROW(mp.Threshold(data, width, height, threshold)); +} From 9031d8a232fd940599385c107646d770682b99fa Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Tue, 25 Oct 2016 11:32:29 +0300 Subject: [PATCH 02/13] add some tests --- test/test_workaround.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/test/test_workaround.cpp b/test/test_workaround.cpp index 5645705..c8822bc 100644 --- a/test/test_workaround.cpp +++ b/test/test_workaround.cpp @@ -15,11 +15,38 @@ TEST(MatrixProcessor, No_Throw_When_DataOK) { EXPECT_NO_THROW(mp.Threshold(data, width, height, threshold)); } -TEST(MatrixProcessor, Throw_When_DataFail) { +/*TEST(MatrixProcessor, Throw_When_DataFail) { MatrixProcessor mp; int width = 4, height = 4; unsigned char* data; unsigned char threshold = 'e'; EXPECT_ANY_THROW(mp.Threshold(data, width, height, threshold)); +}*/ + +TEST(MatrixProcessor, Correct_Work) { + MatrixProcessor mp; + int width = 4, height = 4; + unsigned char* data; + unsigned char threshold = 'b'; + + data = new unsigned char[width * height]; + for (int i = 0; i < width * height; i++) + data[i] = 'a' + i; + + mp.Threshold(data, width, height, threshold); + EXPECT_EQ(0, data[0]); +} + +TEST(MatrixProcessor, Correct_Work_With_One_Element) { + MatrixProcessor mp; + int width = 1, height = 1; + unsigned char* data; + unsigned char threshold = 'b'; + + data = new unsigned char[width * height]; + data[0] = threshold + 1; + + mp.Threshold(data, width, height, threshold); + EXPECT_EQ(threshold + 1, data[0]); } From f3000ef3822fd38d0244d7978be18f88d820a535 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Tue, 25 Oct 2016 11:34:00 +0300 Subject: [PATCH 03/13] bug fixed --- src/workaround.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/workaround.cpp b/src/workaround.cpp index 9304f0c..fd28b70 100644 --- a/src/workaround.cpp +++ b/src/workaround.cpp @@ -1,5 +1,6 @@ #include "workaround.hpp" +#include #include using namespace std; From 89ab2815f04f5658df95b217ffbae75430cef070 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Tue, 25 Oct 2016 18:55:24 +0300 Subject: [PATCH 04/13] add Averaging --- include/workaround.hpp | 3 +++ src/workaround.cpp | 33 ++++++++++++++++++++++------ test/test_workaround.cpp | 46 +++++++++++++++++++++++++--------------- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/include/workaround.hpp b/include/workaround.hpp index 18ffbc4..440ae14 100644 --- a/include/workaround.hpp +++ b/include/workaround.hpp @@ -4,4 +4,7 @@ class MatrixProcessor { public: void Threshold(unsigned char* const data, const int width, const int height, const int threshold); + + void Averaging(unsigned char* const data, const int width, const int height, + const int surroundings); }; diff --git a/src/workaround.cpp b/src/workaround.cpp index fd28b70..e0ffb9c 100644 --- a/src/workaround.cpp +++ b/src/workaround.cpp @@ -1,17 +1,36 @@ #include "workaround.hpp" #include -#include using namespace std; void MatrixProcessor::Threshold(unsigned char* const data, const int width, const int height, const int threshold) { - if (data != nullptr) { - for (int i = 0; i < width * height; i++) - if (data[i] < threshold) - data[i] = 0; - } else { - throw std::invalid_argument("Data is empty!"); + for (int i = 0; i < width * height; i++) + if (data[i] < threshold) + data[i] = 0; +} + +void MatrixProcessor::Averaging(unsigned char* const data, const int width, + const int height, const int surroundings) { + unsigned char* dst = new unsigned char[width * height]; + + for (int i = 0; i < width * height; i++) { + int sum = 0, count = 0; + + for (int k = -surroundings; k <= surroundings; k++) + for (int l = -surroundings; l <= surroundings; l++) { + int index = i + k * width + l; + if (index < 0 || index >= width * height) + continue; + + sum += data[index]; + count++; + } + + dst[i] = sum / count; } + + for (int i = 0; i < width * height; i++) + data[i] = dst[i]; } diff --git a/test/test_workaround.cpp b/test/test_workaround.cpp index c8822bc..2d6c01c 100644 --- a/test/test_workaround.cpp +++ b/test/test_workaround.cpp @@ -2,47 +2,38 @@ #include "workaround.hpp" -TEST(MatrixProcessor, No_Throw_When_DataOK) { +TEST(MatrixProcessor, Threshold_No_Throw_When_DataOK) { MatrixProcessor mp; int width = 4, height = 4; unsigned char* data; - unsigned char threshold = 'e'; + unsigned char threshold = 5; data = new unsigned char[width * height]; for (int i = 0; i < width * height; i++) - data[i] = static_cast('a' + i); + data[i] = i; EXPECT_NO_THROW(mp.Threshold(data, width, height, threshold)); } -/*TEST(MatrixProcessor, Throw_When_DataFail) { +TEST(MatrixProcessor, Threshold_Correct_Work) { MatrixProcessor mp; int width = 4, height = 4; unsigned char* data; - unsigned char threshold = 'e'; - - EXPECT_ANY_THROW(mp.Threshold(data, width, height, threshold)); -}*/ - -TEST(MatrixProcessor, Correct_Work) { - MatrixProcessor mp; - int width = 4, height = 4; - unsigned char* data; - unsigned char threshold = 'b'; + unsigned char threshold = 5; data = new unsigned char[width * height]; for (int i = 0; i < width * height; i++) - data[i] = 'a' + i; + data[i] = i; mp.Threshold(data, width, height, threshold); EXPECT_EQ(0, data[0]); } -TEST(MatrixProcessor, Correct_Work_With_One_Element) { +TEST(MatrixProcessor, Threshold_Correct_Work_With_One_Element) { MatrixProcessor mp; int width = 1, height = 1; unsigned char* data; - unsigned char threshold = 'b'; + unsigned char threshold = 6; data = new unsigned char[width * height]; data[0] = threshold + 1; @@ -50,3 +41,24 @@ TEST(MatrixProcessor, Correct_Work_With_One_Element) { mp.Threshold(data, width, height, threshold); EXPECT_EQ(threshold + 1, data[0]); } + +TEST(MatrixProcessor, Averaging_Correct_Work) { + MatrixProcessor mp; + const int width = 3, height = 3; + unsigned char* data; + int surroundings = 1; + + data = new unsigned char[width * height]; + for (int i = 0; i < width * height; i++) + data[i] = i; + + unsigned char expected[width * height] = { '2','2','3','3','4','4','5','5','6' }; + mp.Averaging(data, width, height,surroundings); + bool flag = true; + + for (int i = 0; i < width * height; i++) + if (data[i] != expected[i]) + flag = false; + + EXPECT_TRUE(flag); +} From df3b27ca43b4b679d8f5f6f7f12f771cbdf085b8 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Wed, 26 Oct 2016 18:50:31 +0300 Subject: [PATCH 05/13] bug fixed --- src/workaround.cpp | 29 +++++++++++++++++++++++++++-- test/test_workaround.cpp | 5 +++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/workaround.cpp b/src/workaround.cpp index e0ffb9c..b3296d4 100644 --- a/src/workaround.cpp +++ b/src/workaround.cpp @@ -15,7 +15,32 @@ void MatrixProcessor::Averaging(unsigned char* const data, const int width, const int height, const int surroundings) { unsigned char* dst = new unsigned char[width * height]; - for (int i = 0; i < width * height; i++) { + for (int i = 0; i < width * height; i++) + /*for (int j = 0; j < height; j++)*/ { + int sum = 0, count = 0; + + for (int k = -surroundings; k <= surroundings; k++) { + if (i / width < 1 && k == -1 || i / width == width - 1 && k == 1) + continue; + + for (int l = -surroundings; l <= surroundings; l++) { + if (i % width == 0 && l == -1 || (i + 1) % width == 0 && l == 1) + continue; + + int index = i + k * width + l; + + sum += data[index]; + count++; + } + } + + dst[i] = sum / count; + } + + + + + /*for (int i = 0; i < width * height; i++) { int sum = 0, count = 0; for (int k = -surroundings; k <= surroundings; k++) @@ -29,7 +54,7 @@ void MatrixProcessor::Averaging(unsigned char* const data, const int width, } dst[i] = sum / count; - } + }*/ for (int i = 0; i < width * height; i++) data[i] = dst[i]; diff --git a/test/test_workaround.cpp b/test/test_workaround.cpp index 2d6c01c..1f6e4b3 100644 --- a/test/test_workaround.cpp +++ b/test/test_workaround.cpp @@ -1,4 +1,5 @@ #include +#include "string" #include "workaround.hpp" @@ -56,6 +57,10 @@ TEST(MatrixProcessor, Averaging_Correct_Work) { mp.Averaging(data, width, height,surroundings); bool flag = true; + std::string str; + for (int i = 0; i < width * height; i++) + str[i] = data[i]; + for (int i = 0; i < width * height; i++) if (data[i] != expected[i]) flag = false; From 29176ce112e1d799b57c15e5983cc488c5d44d84 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Sun, 30 Oct 2016 11:00:00 +0300 Subject: [PATCH 06/13] fixed avegaging --- include/workaround.hpp | 3 ++ src/workaround.cpp | 67 ++++++++++++++++-------------------------- 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/include/workaround.hpp b/include/workaround.hpp index 440ae14..635b05e 100644 --- a/include/workaround.hpp +++ b/include/workaround.hpp @@ -7,4 +7,7 @@ class MatrixProcessor { void Averaging(unsigned char* const data, const int width, const int height, const int surroundings); + + void MedianFilter(unsigned char* const data, const int width, + const int height); }; diff --git a/src/workaround.cpp b/src/workaround.cpp index b3296d4..fbfa7be 100644 --- a/src/workaround.cpp +++ b/src/workaround.cpp @@ -1,61 +1,46 @@ #include "workaround.hpp" -#include +//#include -using namespace std; +//using namespace std; void MatrixProcessor::Threshold(unsigned char* const data, const int width, const int height, const int threshold) { - for (int i = 0; i < width * height; i++) - if (data[i] < threshold) - data[i] = 0; + for (int i = 0; i < width * height; i++) + if (data[i] < threshold) + data[i] = 0; } void MatrixProcessor::Averaging(unsigned char* const data, const int width, const int height, const int surroundings) { - unsigned char* dst = new unsigned char[width * height]; + unsigned char* dst = new unsigned char[width * height]; - for (int i = 0; i < width * height; i++) - /*for (int j = 0; j < height; j++)*/ { - int sum = 0, count = 0; + for (int i = 0; i < width * height; i++) { + int sum = 0, count = 0; - for (int k = -surroundings; k <= surroundings; k++) { - if (i / width < 1 && k == -1 || i / width == width - 1 && k == 1) - continue; + for (int k = -surroundings; k <= surroundings; k++) { + if (i / width < 1 && k < 0 || i / width == width - 1 && k > 0) + continue; - for (int l = -surroundings; l <= surroundings; l++) { - if (i % width == 0 && l == -1 || (i + 1) % width == 0 && l == 1) - continue; + for (int l = -surroundings; l <= surroundings; l++) { + if (i % width == 0 && l < 0 || (i + 1) % width == 0 && l > 0) + continue; - int index = i + k * width + l; + int index = i + k * width + l; - sum += data[index]; - count++; - } - } + sum += data[index]; + count++; + } + } - dst[i] = sum / count; - } + dst[i] = sum / count; + } + for (int i = 0; i < width * height; i++) + data[i] = dst[i]; +} +void MatrixProcessor::MedianFilter(unsigned char *const data, const int width, + const int height) { - - /*for (int i = 0; i < width * height; i++) { - int sum = 0, count = 0; - - for (int k = -surroundings; k <= surroundings; k++) - for (int l = -surroundings; l <= surroundings; l++) { - int index = i + k * width + l; - if (index < 0 || index >= width * height) - continue; - - sum += data[index]; - count++; - } - - dst[i] = sum / count; - }*/ - - for (int i = 0; i < width * height; i++) - data[i] = dst[i]; } From 3d283a0ea39d329f9b548da79bc318c795ddeb02 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Sun, 30 Oct 2016 11:01:14 +0300 Subject: [PATCH 07/13] add tests for averaging --- test/test_workaround.cpp | 100 ++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 43 deletions(-) diff --git a/test/test_workaround.cpp b/test/test_workaround.cpp index 1f6e4b3..3f9b76c 100644 --- a/test/test_workaround.cpp +++ b/test/test_workaround.cpp @@ -1,69 +1,83 @@ #include -#include "string" #include "workaround.hpp" TEST(MatrixProcessor, Threshold_No_Throw_When_DataOK) { - MatrixProcessor mp; - int width = 4, height = 4; - unsigned char* data; - unsigned char threshold = 5; + MatrixProcessor mp; + int width = 4, height = 4; + unsigned char* data; + unsigned char threshold = 5; - data = new unsigned char[width * height]; - for (int i = 0; i < width * height; i++) - data[i] = i; + data = new unsigned char[width * height]; + for (int i = 0; i < width * height; i++) + data[i] = i; - EXPECT_NO_THROW(mp.Threshold(data, width, height, threshold)); + EXPECT_NO_THROW(mp.Threshold(data, width, height, threshold)); } TEST(MatrixProcessor, Threshold_Correct_Work) { - MatrixProcessor mp; - int width = 4, height = 4; - unsigned char* data; - unsigned char threshold = 5; + MatrixProcessor mp; + int width = 4, height = 4; + unsigned char* data; + unsigned char threshold = 5; - data = new unsigned char[width * height]; - for (int i = 0; i < width * height; i++) - data[i] = i; + data = new unsigned char[width * height]; + for (int i = 0; i < width * height; i++) + data[i] = i; - mp.Threshold(data, width, height, threshold); - EXPECT_EQ(0, data[0]); + mp.Threshold(data, width, height, threshold); + EXPECT_EQ(0, data[0]); } TEST(MatrixProcessor, Threshold_Correct_Work_With_One_Element) { - MatrixProcessor mp; - int width = 1, height = 1; - unsigned char* data; - unsigned char threshold = 6; + MatrixProcessor mp; + int width = 1, height = 1; + unsigned char* data; + unsigned char threshold = 6; - data = new unsigned char[width * height]; - data[0] = threshold + 1; + data = new unsigned char[width * height]; + data[0] = threshold + 1; - mp.Threshold(data, width, height, threshold); - EXPECT_EQ(threshold + 1, data[0]); + mp.Threshold(data, width, height, threshold); + EXPECT_EQ(threshold + 1, data[0]); } TEST(MatrixProcessor, Averaging_Correct_Work) { - MatrixProcessor mp; - const int width = 3, height = 3; - unsigned char* data; - int surroundings = 1; + MatrixProcessor mp; + const int width = 3, height = 3; + unsigned char* data; + int surroundings = 1; - data = new unsigned char[width * height]; - for (int i = 0; i < width * height; i++) - data[i] = i; + data = new unsigned char[width * height]; + for (int i = 0; i < width * height; i++) + data[i] = i; - unsigned char expected[width * height] = { '2','2','3','3','4','4','5','5','6' }; - mp.Averaging(data, width, height,surroundings); - bool flag = true; + unsigned char expected[width * height] = { 2,2,3,3,4,4,5,5,6 }; + mp.Averaging(data, width, height,surroundings); + bool flag = true; - std::string str; - for (int i = 0; i < width * height; i++) - str[i] = data[i]; + for (int i = 0; i < width * height; i++) + if (data[i] != expected[i]) + flag = false; - for (int i = 0; i < width * height; i++) - if (data[i] != expected[i]) - flag = false; + EXPECT_TRUE(flag); +} + +TEST(MatrixProcessor, Averaging_Correct_Work_With_Big_Surroudings) { + MatrixProcessor mp; + const int width = 5, height = 5; + unsigned char* data; + int surroundings = 4; + int expected = 0; + + data = new unsigned char[width * height]; + for (int i = 0; i < width * height; i++) { + data[i] = i; + expected += i; + } + + expected /= width * height; + mp.Averaging(data, width, height, surroundings); - EXPECT_TRUE(flag); + EXPECT_EQ(expected, data[0]); } From d582ca9d326a8f9a482419c77ed503ff63c184f1 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Sun, 30 Oct 2016 12:00:45 +0300 Subject: [PATCH 08/13] add median filter --- include/workaround.hpp | 2 +- src/workaround.cpp | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/include/workaround.hpp b/include/workaround.hpp index 635b05e..2a06d9e 100644 --- a/include/workaround.hpp +++ b/include/workaround.hpp @@ -9,5 +9,5 @@ class MatrixProcessor { const int surroundings); void MedianFilter(unsigned char* const data, const int width, - const int height); + const int height, const int surroundings); }; diff --git a/src/workaround.cpp b/src/workaround.cpp index fbfa7be..ad932df 100644 --- a/src/workaround.cpp +++ b/src/workaround.cpp @@ -1,6 +1,8 @@ +#include #include "workaround.hpp" //#include +#include //using namespace std; @@ -40,7 +42,31 @@ void MatrixProcessor::Averaging(unsigned char* const data, const int width, data[i] = dst[i]; } -void MatrixProcessor::MedianFilter(unsigned char *const data, const int width, - const int height) { +void MatrixProcessor::MedianFilter(unsigned char* const data, const int width, + const int height, const int surroundings) { + unsigned char* dst = new unsigned char[width * height]; + + for (int i = 0; i < width * height; i++) { + std::vector vect; + + for (int k = -surroundings; k <= surroundings; k++) { + if (i / width < 1 && k < 0 || i / width == width - 1 && k > 0) + continue; + + for (int l = -surroundings; l <= surroundings; l++) { + if (i % width == 0 && l < 0 || (i + 1) % width == 0 && l > 0) + continue; + + int index = i + k * width + l; + + vect.push_back(data[index]); + } + } + + std::sort(vect.begin(), vect.end()); + dst[i] = vect[vect.size() / 2]; + } + for (int i = 0; i < width * height; i++) + data[i] = dst[i]; } From f278c574f8335ea6c393cb5eca70165f1e2d8914 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Sun, 30 Oct 2016 13:12:37 +0300 Subject: [PATCH 09/13] new medianFilter --- src/workaround.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/workaround.cpp b/src/workaround.cpp index ad932df..7fc6bc5 100644 --- a/src/workaround.cpp +++ b/src/workaround.cpp @@ -64,7 +64,10 @@ void MatrixProcessor::MedianFilter(unsigned char* const data, const int width, } std::sort(vect.begin(), vect.end()); - dst[i] = vect[vect.size() / 2]; + if (vect.size() % 2 == 0) + dst[i] = vect[vect.size() / 2 - 1]; + else + dst[i] = vect[vect.size() / 2]; } for (int i = 0; i < width * height; i++) From 9b2a48bdea25877b32c4758869a914a7bef260d9 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Sun, 30 Oct 2016 13:13:01 +0300 Subject: [PATCH 10/13] add test for median --- test/test_workaround.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/test/test_workaround.cpp b/test/test_workaround.cpp index 3f9b76c..efa35d0 100644 --- a/test/test_workaround.cpp +++ b/test/test_workaround.cpp @@ -54,13 +54,9 @@ TEST(MatrixProcessor, Averaging_Correct_Work) { unsigned char expected[width * height] = { 2,2,3,3,4,4,5,5,6 }; mp.Averaging(data, width, height,surroundings); - bool flag = true; for (int i = 0; i < width * height; i++) - if (data[i] != expected[i]) - flag = false; - - EXPECT_TRUE(flag); + EXPECT_EQ(expected[i], data[i]); } TEST(MatrixProcessor, Averaging_Correct_Work_With_Big_Surroudings) { @@ -81,3 +77,20 @@ TEST(MatrixProcessor, Averaging_Correct_Work_With_Big_Surroudings) { EXPECT_EQ(expected, data[0]); } + +TEST(MatrixProcessor, MefianFilter_Correct_Work) { + MatrixProcessor mp; + const int width = 3, height = 3; + unsigned char* data; + int surroundings = 1; + + data = new unsigned char[width * height]; + for (int i = 0; i < width * height; i++) + data[i] = i; + + unsigned char expected[width * height] = { 1,2,2,3,4,4,4,5,5 }; + mp.MedianFilter(data, width, height,surroundings); + + for (int i = 0; i < width * height; i++) + EXPECT_EQ(expected[i], data[i]); +} From 41815f41cb509df4a75d32b7de7433e629913f37 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Sun, 30 Oct 2016 13:14:38 +0300 Subject: [PATCH 11/13] fixed tests --- test/test_workaround.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/test_workaround.cpp b/test/test_workaround.cpp index efa35d0..a299398 100644 --- a/test/test_workaround.cpp +++ b/test/test_workaround.cpp @@ -2,7 +2,7 @@ #include "workaround.hpp" -TEST(MatrixProcessor, Threshold_No_Throw_When_DataOK) { +TEST(Threshold, No_Throw_When_DataOK) { MatrixProcessor mp; int width = 4, height = 4; unsigned char* data; @@ -15,7 +15,7 @@ TEST(MatrixProcessor, Threshold_No_Throw_When_DataOK) { EXPECT_NO_THROW(mp.Threshold(data, width, height, threshold)); } -TEST(MatrixProcessor, Threshold_Correct_Work) { +TEST(Threshold, Correct_Work) { MatrixProcessor mp; int width = 4, height = 4; unsigned char* data; @@ -29,7 +29,7 @@ TEST(MatrixProcessor, Threshold_Correct_Work) { EXPECT_EQ(0, data[0]); } -TEST(MatrixProcessor, Threshold_Correct_Work_With_One_Element) { +TEST(Threshold, Correct_Work_With_One_Element) { MatrixProcessor mp; int width = 1, height = 1; unsigned char* data; @@ -42,7 +42,7 @@ TEST(MatrixProcessor, Threshold_Correct_Work_With_One_Element) { EXPECT_EQ(threshold + 1, data[0]); } -TEST(MatrixProcessor, Averaging_Correct_Work) { +TEST(Averaging, Correct_Work) { MatrixProcessor mp; const int width = 3, height = 3; unsigned char* data; @@ -59,7 +59,7 @@ TEST(MatrixProcessor, Averaging_Correct_Work) { EXPECT_EQ(expected[i], data[i]); } -TEST(MatrixProcessor, Averaging_Correct_Work_With_Big_Surroudings) { +TEST(Averaging, Correct_Work_With_Big_Surroudings) { MatrixProcessor mp; const int width = 5, height = 5; unsigned char* data; @@ -78,7 +78,7 @@ TEST(MatrixProcessor, Averaging_Correct_Work_With_Big_Surroudings) { EXPECT_EQ(expected, data[0]); } -TEST(MatrixProcessor, MefianFilter_Correct_Work) { +TEST(MefianFilter, Correct_Work) { MatrixProcessor mp; const int width = 3, height = 3; unsigned char* data; From 67bda2915225fba8adef407fb760b8f83ea17135 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Sun, 30 Oct 2016 13:21:21 +0300 Subject: [PATCH 12/13] fix workaround --- src/workaround.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/workaround.cpp b/src/workaround.cpp index 7fc6bc5..3775743 100644 --- a/src/workaround.cpp +++ b/src/workaround.cpp @@ -1,10 +1,9 @@ #include +#include +#include #include "workaround.hpp" -//#include -#include -//using namespace std; void MatrixProcessor::Threshold(unsigned char* const data, const int width, const int height, const int threshold) { @@ -38,8 +37,8 @@ void MatrixProcessor::Averaging(unsigned char* const data, const int width, dst[i] = sum / count; } - for (int i = 0; i < width * height; i++) - data[i] = dst[i]; + memcpy(data, dst, width * height); + delete[] dst; } void MatrixProcessor::MedianFilter(unsigned char* const data, const int width, @@ -70,6 +69,6 @@ void MatrixProcessor::MedianFilter(unsigned char* const data, const int width, dst[i] = vect[vect.size() / 2]; } - for (int i = 0; i < width * height; i++) - data[i] = dst[i]; + memcpy(data, dst, width * height); + delete[] dst; } From b14f43e5b0817eb65d3f430231d83d505b051013 Mon Sep 17 00:00:00 2001 From: MichaelMurashov Date: Sun, 30 Oct 2016 17:08:11 +0300 Subject: [PATCH 13/13] bug fixed --- src/workaround.cpp | 2 +- test/test_workaround.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/workaround.cpp b/src/workaround.cpp index 3775743..694c7b5 100644 --- a/src/workaround.cpp +++ b/src/workaround.cpp @@ -64,7 +64,7 @@ void MatrixProcessor::MedianFilter(unsigned char* const data, const int width, std::sort(vect.begin(), vect.end()); if (vect.size() % 2 == 0) - dst[i] = vect[vect.size() / 2 - 1]; + dst[i] = (vect[vect.size() / 2 - 1] + vect[vect.size() / 2]) / 2; else dst[i] = vect[vect.size() / 2]; } diff --git a/test/test_workaround.cpp b/test/test_workaround.cpp index a299398..307f153 100644 --- a/test/test_workaround.cpp +++ b/test/test_workaround.cpp @@ -88,7 +88,7 @@ TEST(MefianFilter, Correct_Work) { for (int i = 0; i < width * height; i++) data[i] = i; - unsigned char expected[width * height] = { 1,2,2,3,4,4,4,5,5 }; + unsigned char expected[width * height] = { 2,2,3,3,4,4,5,5,6 }; mp.MedianFilter(data, width, height,surroundings); for (int i = 0; i < width * height; i++)