Skip to content

Commit

Permalink
Fixed ostream operator for image class
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Oct 21, 2024
1 parent e04a409 commit 92c5c5b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/machine_learning/image/filters/average_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace avg_filter {
int width = img._width();
Image resulted_img(height, width);
std::vector<std::vector<float> > kernel = {{1.0/9, 1.0/9, 1.0/9}, {1.0/9, 1.0/9, 1.0/9}, {1.0/9, 1.0/9, 1.0/9}};
return img.apply_filter2d_float(kernel).get_2d_array();
return img.apply_filter2d(kernel).get_2d_array();
}
}

Expand Down
36 changes: 34 additions & 2 deletions src/machine_learning/image/image.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef img_H
#define img_H

#include <cinttypes>
#ifdef __cplusplus
#include <iostream>
#include <vector>
Expand Down Expand Up @@ -274,11 +275,11 @@ class Image{
}

/**
* @brief apply_filter2d_float function
* @brief apply_filter2d function
* @param filter: 3x3 kernel with floats to be applied to the image
* @return vector<vector<int32_t> > the resulted image
*/
Image apply_filter2d_float(std::vector<std::vector<float> > &filter) const {
Image apply_filter2d(std::vector<std::vector<float> > &filter) const {
assert(filter.size() == 3 && filter[0].size() == 3);
Image resulted_img(height, width);
for(int x = 0; x<height; x++){
Expand Down Expand Up @@ -312,5 +313,36 @@ class Image{
}
return resulted_img;
}

/**
* @brief overloaded operator << for Image class
*/
friend std::ostream & operator << (std::ostream &out, const Image &img) {
int height = img._height(), width = img._width();

auto write = [&](const int &start_at, const int &end_at) -> void {
for(int i = start_at; i<=end_at && i<height; i++) {
int j = 0;
for(; j<=10 && j<width; j++) {
if(j != std::min(10, width - 1)) {
out << img.get_point(i, j) << ", ";
}
else {
out << img.get_point(i, j);
}
}
out << '\n';
}
};

out << '[';
write(0, 2);
out << "...";
out << '\n';
write(height - 3, height - 1);
out << "(height: " << height << ", width: " << width << ')' << ']';
out << '\n';
return out;
}
};
#endif
32 changes: 27 additions & 5 deletions tests/machine_learning/image/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ TEST_CASE("Testing secondary constructor of Image class") {
}

TEST_CASE("Testing getting point of a 2d image") {
std::vector<std::vector<int32_t> > img = {{1,0,0}, {1,0,0}, {0,0,1}, {0,0,0}};
std::vector<std::vector<int32_t> > img = {{1,0,0}, {1,0,0}, {0,0,1}, {0,0,0}};
Image i(img);
REQUIRE(i.get_point(0, 0) == 1);
REQUIRE(i.get_point(0, 1) == 0);
}

TEST_CASE("Testing setting point at a 2d image") {
std::vector<std::vector<int32_t> > img = {{1,0,0}, {1,0,0}, {0,0,1}, {0,0,0}};
std::vector<std::vector<int32_t> > img = {{1,0,0}, {1,0,0}, {0,0,1}, {0,0,0}};
Image i(img);

REQUIRE(i.get_point(0, 0) == 1);
Expand All @@ -34,7 +34,7 @@ TEST_CASE("Testing setting point at a 2d image") {
}

TEST_CASE("Testing adding to a point in 2d image") {
std::vector<std::vector<int32_t> > img = {{1,0,0}, {1,0,0}, {0,0,1}, {0,0,0}};
std::vector<std::vector<int32_t> > img = {{1,0,0}, {1,0,0}, {0,0,1}, {0,0,0}};
Image i(img);

REQUIRE(i.get_point(1, 1) == 0);
Expand Down Expand Up @@ -74,16 +74,38 @@ TEST_CASE("Testing sub 2 2d images") {
TEST_CASE("Testing multiplying 2 2d images") {
std::vector<std::vector<int32_t> > img = {{0, 0, 0}, {1, 1, 1}, {0, 0, 0}};
std::vector<std::vector<int32_t> > img2 = {{0, 0, 0}, {1, 1, 1}, {0, 0, 0}};
Image i1(img), i2(img2);
Image i1(img), i2(img2);
i1 = i1.mul(i2);
REQUIRE(i1.get_2d_array() == img2);
}

TEST_CASE("Testing applying 2d filter") {
std::vector<std::vector<int32_t> > img = {{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}};
std::vector<std::vector<int32_t> > kernel = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
std::vector<std::vector<int32_t> > kernel = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
std::vector<std::vector<int32_t> > check(img.size(), std::vector<int32_t>(img[0].size(), 0));
Image i(img);
i = i.apply_filter2d(kernel);
REQUIRE(i.get_2d_array() == check);

std::vector<std::vector<float> > kernel2 = { {1.0/9, 1.0/9, 1.0/9}, {1.0/9, 1.0/9, 1.0/9}, {1.0/9, 1.0/9, 1.0/9} };

CHECK_NOTHROW(i = i.apply_filter2d(kernel2));
}

TEST_CASE("Testing operator << for image class") {
std::vector<std::vector<int32_t> > img = {{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}};

Image i(img);

CHECK_NOTHROW(std::cout << i << '\n');

std::vector<std::vector<int32_t> > img2(100, std::vector<int32_t>(100));

for(int i = 0; i<100; i++) {
std::iota(img2[i].begin(), img2[i].end(), 1);
}

Image i2(img2);

CHECK_NOTHROW(std::cout << i2 << '\n');
}

0 comments on commit 92c5c5b

Please sign in to comment.