| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "tsibareva_e_edge_select_sobel/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "tsibareva_e_edge_select_sobel/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace tsibareva_e_edge_select_sobel { | ||
| 10 | |||
| 11 | const std::vector<std::vector<int>> kSobelX = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}; | ||
| 12 | |||
| 13 | const std::vector<std::vector<int>> kSobelY = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}}; | ||
| 14 | |||
| 15 | ✗ | TsibarevaEEdgeSelectSobelSEQ::TsibarevaEEdgeSelectSobelSEQ(const InType &in) | |
| 16 | ✗ | : height_(std::get<1>(in)), width_(std::get<2>(in)), threshold_(std::get<3>(in)) { | |
| 17 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 18 | GetInput() = in; | ||
| 19 | ✗ | } | |
| 20 | |||
| 21 | ✗ | bool TsibarevaEEdgeSelectSobelSEQ::ValidationImpl() { | |
| 22 | ✗ | return true; | |
| 23 | } | ||
| 24 | |||
| 25 | ✗ | bool TsibarevaEEdgeSelectSobelSEQ::PreProcessingImpl() { | |
| 26 | ✗ | GetOutput() = std::vector<int>(static_cast<size_t>(height_ * width_), 0); | |
| 27 | ✗ | return true; | |
| 28 | } | ||
| 29 | |||
| 30 | ✗ | bool TsibarevaEEdgeSelectSobelSEQ::RunImpl() { | |
| 31 | const auto &flat_pixels = std::get<0>(GetInput()); | ||
| 32 | ✗ | input_pixels_ = std::vector<int>(flat_pixels); | |
| 33 | |||
| 34 | auto &output_pixels = GetOutput(); | ||
| 35 | |||
| 36 | ✗ | for (int row = 0; row < height_; ++row) { | |
| 37 | ✗ | for (int col = 0; col < width_; ++col) { | |
| 38 | ✗ | int gx = GradientX(col, row); | |
| 39 | ✗ | int gy = GradientY(col, row); | |
| 40 | |||
| 41 | ✗ | int mag = static_cast<int>(std::sqrt((gx * gx) + (gy * gy) + 0.0)); | |
| 42 | ✗ | output_pixels[(static_cast<size_t>(row) * width_) + col] = (mag <= threshold_) ? 0 : mag; | |
| 43 | } | ||
| 44 | } | ||
| 45 | ✗ | return true; | |
| 46 | } | ||
| 47 | |||
| 48 | ✗ | bool TsibarevaEEdgeSelectSobelSEQ::PostProcessingImpl() { | |
| 49 | ✗ | return true; | |
| 50 | } | ||
| 51 | |||
| 52 | ✗ | int TsibarevaEEdgeSelectSobelSEQ::GradientX(int x, int y) { | |
| 53 | int sum = 0; | ||
| 54 | |||
| 55 | ✗ | for (int ky = -1; ky <= 1; ++ky) { | |
| 56 | ✗ | for (int kx = -1; kx <= 1; ++kx) { | |
| 57 | ✗ | int nx = x + kx; | |
| 58 | ✗ | int ny = y + ky; | |
| 59 | |||
| 60 | ✗ | int weight = kSobelX[ky + 1][kx + 1]; | |
| 61 | |||
| 62 | ✗ | if (nx >= 0 && nx < width_ && ny >= 0 && ny < height_) { | |
| 63 | ✗ | sum += weight * input_pixels_[(static_cast<size_t>(ny) * width_) + nx]; | |
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | ✗ | return sum; | |
| 69 | } | ||
| 70 | |||
| 71 | ✗ | int TsibarevaEEdgeSelectSobelSEQ::GradientY(int x, int y) { | |
| 72 | int sum = 0; | ||
| 73 | |||
| 74 | ✗ | for (int ky = -1; ky <= 1; ++ky) { | |
| 75 | ✗ | for (int kx = -1; kx <= 1; ++kx) { | |
| 76 | ✗ | int nx = x + kx; | |
| 77 | ✗ | int ny = y + ky; | |
| 78 | |||
| 79 | ✗ | int weight = kSobelY[ky + 1][kx + 1]; | |
| 80 | |||
| 81 | ✗ | if (nx >= 0 && nx < width_ && ny >= 0 && ny < height_) { | |
| 82 | ✗ | sum += weight * input_pixels_[(static_cast<size_t>(ny) * width_) + nx]; | |
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | ✗ | return sum; | |
| 88 | } | ||
| 89 | |||
| 90 | } // namespace tsibareva_e_edge_select_sobel | ||
| 91 |