Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

#include "task/include/task.hpp"

namespace badanov_a_select_edge_sobel_seq {
namespace badanov_a_select_edge_sobel {

using InType = std::vector<uint8_t>;
using OutType = std::vector<uint8_t>;
using TestType = std::tuple<int, std::string>;
using BaseTask = ppc::task::Task<InType, OutType>;

} // namespace badanov_a_select_edge_sobel_seq
} // namespace badanov_a_select_edge_sobel
2 changes: 2 additions & 0 deletions tasks/badanov_a_select_edge_sobel/data/test_10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 1
128
3 changes: 3 additions & 0 deletions tasks/badanov_a_select_edge_sobel/data/test_11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
3 2
0 128 0
128 255 128
2 changes: 2 additions & 0 deletions tasks/badanov_a_select_edge_sobel/data/test_7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 8
0 32 64 96 128 160 192 224
9 changes: 9 additions & 0 deletions tasks/badanov_a_select_edge_sobel/data/test_8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
8 1
0
32
64
96
128
160
192
224
3 changes: 3 additions & 0 deletions tasks/badanov_a_select_edge_sobel/data/test_9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2 2
0 255
255 0
39 changes: 39 additions & 0 deletions tasks/badanov_a_select_edge_sobel/omp/include/ops_omp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <array>
#include <cstdint>
#include <vector>

#include "badanov_a_select_edge_sobel/common/include/common.hpp"
#include "task/include/task.hpp"

namespace badanov_a_select_edge_sobel {

class BadanovASelectEdgeSobelOMP : public BaseTask {
public:
static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() {
return ppc::task::TypeOfTask::kOMP;
}
explicit BadanovASelectEdgeSobelOMP(const InType &in);

private:
bool ValidationImpl() override;
bool PreProcessingImpl() override;
bool RunImpl() override;
bool PostProcessingImpl() override;

void ApplySobelOperator(const std::vector<uint8_t> &input, std::vector<float> &magnitude, float &max_magnitude);
void ComputeGradientAtPixel(const std::vector<uint8_t> &input, int row, int col, float &gradient_x,
float &gradient_y) const;
void ApplyThreshold(const std::vector<float> &magnitude, float max_magnitude, std::vector<uint8_t> &output) const;

static constexpr std::array<std::array<int, 3>, 3> kKernelX = {{{{-1, 0, 1}}, {{-2, 0, 2}}, {{-1, 0, 1}}}};

static constexpr std::array<std::array<int, 3>, 3> kKernelY = {{{{-1, -2, -1}}, {{0, 0, 0}}, {{1, 2, 1}}}};

int width_ = 0;
int height_ = 0;
int threshold_ = 50;
};

} // namespace badanov_a_select_edge_sobel
137 changes: 137 additions & 0 deletions tasks/badanov_a_select_edge_sobel/omp/src/ops_omp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#include "badanov_a_select_edge_sobel/omp/include/ops_omp.hpp"

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <vector>

#include "badanov_a_select_edge_sobel/common/include/common.hpp"

#ifdef _OPENMP
# include <omp.h>
#endif

namespace badanov_a_select_edge_sobel {

BadanovASelectEdgeSobelOMP::BadanovASelectEdgeSobelOMP(const InType &in) {
SetTypeOfTask(GetStaticTypeOfTask());
GetInput() = in;
GetOutput() = std::vector<uint8_t>();
}

bool BadanovASelectEdgeSobelOMP::ValidationImpl() {
const auto &input = GetInput();
return !input.empty();
}

bool BadanovASelectEdgeSobelOMP::PreProcessingImpl() {
const auto &input = GetInput();

width_ = static_cast<int>(std::sqrt(input.size()));
height_ = width_;

if (width_ * height_ != static_cast<int>(input.size())) {
width_ = static_cast<int>(input.size());
height_ = 1;
}

GetOutput() = std::vector<uint8_t>(input.size(), 0);

return true;
}

void BadanovASelectEdgeSobelOMP::ApplySobelOperator(const std::vector<uint8_t> &input, std::vector<float> &magnitude,
float &max_magnitude) {
max_magnitude = 0.0F;
const int height = height_;
const int width = width_;

#pragma omp parallel default(none) shared(input, magnitude, max_magnitude, height, width)
{
float local_max_magnitude = 0.0F;

#pragma omp for schedule(static)
for (int row = 1; row < height - 1; ++row) {
for (int col = 1; col < width - 1; ++col) {
float gradient_x = 0.0F;
float gradient_y = 0.0F;

ComputeGradientAtPixel(input, row, col, gradient_x, gradient_y);

const float magnitude_value = std::sqrt((gradient_x * gradient_x) + (gradient_y * gradient_y));
const size_t idx = (static_cast<size_t>(row) * static_cast<size_t>(width)) + static_cast<size_t>(col);
magnitude[idx] = magnitude_value;

local_max_magnitude = std::max(magnitude_value, local_max_magnitude);
}
}

#pragma omp critical
{
max_magnitude = std::max(local_max_magnitude, max_magnitude);
}
}
}

void BadanovASelectEdgeSobelOMP::ComputeGradientAtPixel(const std::vector<uint8_t> &input, int row, int col,
float &gradient_x, float &gradient_y) const {
gradient_x = 0.0F;
gradient_y = 0.0F;

for (int kernel_row = -1; kernel_row <= 1; ++kernel_row) {
for (int kernel_col = -1; kernel_col <= 1; ++kernel_col) {
const size_t pixel_index =
(static_cast<size_t>(row + kernel_row) * static_cast<size_t>(width_)) + static_cast<size_t>(col + kernel_col);
const uint8_t pixel = input[pixel_index];

const int kx_idx = kernel_row + 1;
const int ky_idx = kernel_col + 1;
const int kernel_x_value = kKernelX.at(static_cast<size_t>(kx_idx)).at(static_cast<size_t>(ky_idx));
const int kernel_y_value = kKernelY.at(static_cast<size_t>(kx_idx)).at(static_cast<size_t>(ky_idx));

gradient_x += static_cast<float>(pixel) * static_cast<float>(kernel_x_value);
gradient_y += static_cast<float>(pixel) * static_cast<float>(kernel_y_value);
}
}
}

void BadanovASelectEdgeSobelOMP::ApplyThreshold(const std::vector<float> &magnitude, float max_magnitude,
std::vector<uint8_t> &output) const {
if (max_magnitude > 0.0F) {
const float scale = 255.0F / max_magnitude;
const size_t size = magnitude.size();
const int threshold = threshold_;

#pragma omp parallel for schedule(static) default(none) shared(magnitude, output, scale, size) firstprivate(threshold)
for (size_t i = 0; i < size; ++i) {
output[i] = (magnitude[i] * scale > static_cast<float>(threshold)) ? 255 : 0;
}
} else {
std::ranges::fill(output, 0);
}
}

bool BadanovASelectEdgeSobelOMP::RunImpl() {
const auto &input = GetInput();
auto &output = GetOutput();

if (height_ < 3 || width_ < 3) {
output = input;
return true;
}

std::vector<float> magnitude(input.size(), 0.0F);
float max_magnitude = 0.0F;

ApplySobelOperator(input, magnitude, max_magnitude);
ApplyThreshold(magnitude, max_magnitude, output);

return true;
}

bool BadanovASelectEdgeSobelOMP::PostProcessingImpl() {
return true;
}

} // namespace badanov_a_select_edge_sobel
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <cstdint>
#include <vector>

#include "badanov_a_select_edge_sobel_seq/common/include/common.hpp"
#include "badanov_a_select_edge_sobel/common/include/common.hpp"
#include "task/include/task.hpp"

namespace badanov_a_select_edge_sobel_seq {
namespace badanov_a_select_edge_sobel {

class BadanovASelectEdgeSobelSEQ : public BaseTask {
public:
Expand Down Expand Up @@ -36,4 +36,4 @@ class BadanovASelectEdgeSobelSEQ : public BaseTask {
int threshold_ = 50;
};

} // namespace badanov_a_select_edge_sobel_seq
} // namespace badanov_a_select_edge_sobel
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "badanov_a_select_edge_sobel_seq/seq/include/ops_seq.hpp"
#include "badanov_a_select_edge_sobel/seq/include/ops_seq.hpp"

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <vector>

#include "badanov_a_select_edge_sobel_seq/common/include/common.hpp"
#include "badanov_a_select_edge_sobel/common/include/common.hpp"

namespace badanov_a_select_edge_sobel_seq {
namespace badanov_a_select_edge_sobel {

BadanovASelectEdgeSobelSEQ::BadanovASelectEdgeSobelSEQ(const InType &in) {
SetTypeOfTask(GetStaticTypeOfTask());
Expand Down Expand Up @@ -113,4 +113,4 @@ bool BadanovASelectEdgeSobelSEQ::PostProcessingImpl() {
return true;
}

} // namespace badanov_a_select_edge_sobel_seq
} // namespace badanov_a_select_edge_sobel
39 changes: 39 additions & 0 deletions tasks/badanov_a_select_edge_sobel/tbb/include/ops_tbb.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <array>
#include <cstdint>
#include <vector>

#include "badanov_a_select_edge_sobel/common/include/common.hpp"
#include "task/include/task.hpp"

namespace badanov_a_select_edge_sobel {

class BadanovASelectEdgeSobelTBB : public BaseTask {
public:
static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() {
return ppc::task::TypeOfTask::kTBB;
}
explicit BadanovASelectEdgeSobelTBB(const InType &in);

private:
bool ValidationImpl() override;
bool PreProcessingImpl() override;
bool RunImpl() override;
bool PostProcessingImpl() override;

void ApplySobelOperator(const std::vector<uint8_t> &input, std::vector<float> &magnitude, float &max_magnitude);
void ComputeGradientAtPixel(const std::vector<uint8_t> &input, int row, int col, float &gradient_x,
float &gradient_y) const;
void ApplyThreshold(const std::vector<float> &magnitude, float max_magnitude, std::vector<uint8_t> &output) const;

static constexpr std::array<std::array<int, 3>, 3> kKernelX = {{{{-1, 0, 1}}, {{-2, 0, 2}}, {{-1, 0, 1}}}};

static constexpr std::array<std::array<int, 3>, 3> kKernelY = {{{{-1, -2, -1}}, {{0, 0, 0}}, {{1, 2, 1}}}};

int width_ = 0;
int height_ = 0;
int threshold_ = 50;
};

} // namespace badanov_a_select_edge_sobel
Loading
Loading