AlgoPlus v0.1.0
Loading...
Searching...
No Matches
operations.h
1#ifndef DILATION_H
2#define DILATION_H
3
4#ifdef __cplusplus
5#include <iostream>
6#include <vector>
7#include "../image.h"
8#endif
9
10namespace morphology_operations {
17inline std::vector<std::vector<int32_t>> dilate(const std::vector<std::vector<int32_t>>& image) {
18 Image img(image);
19 assert(img.binary());
20 int height = img._height();
21 int width = img._width();
22 Image resulted_img(height, width);
23 for (int x = 1; x < height - 1; x++) {
24 for (int y = 1; y < width - 1; y++) {
25 if (!img.get_point(x - 1, y) || !img.get_point(x + 1, y) || !img.get_point(x, y - 1) ||
26 !img.get_point(x, y + 1)) {
27 resulted_img.set_point(x, y, 255);
28 } else {
29 img.set_point(x, y, image[x][y]);
30 }
31 }
32 }
33 return resulted_img.get_2d_array();
34}
35
42inline std::vector<std::vector<int32_t>> erote(const std::vector<std::vector<int32_t>>& image) {
43 Image img(image);
44 assert(img.binary());
45 int height = img._height();
46 int width = img._width();
47 Image resulted_img(height, width);
48 for (int x = 1; x < height - 1; x++) {
49 for (int y = 1; y < width - 1; y++) {
50 if (!img.get_point(x - 1, y) || !img.get_point(x + 1, y) ||
51 !img.get_point(x, y - 1) && !img.get_point(x, y + 1)) {
52 resulted_img.set_point(x, y, 0);
53 } else {
54 resulted_img.set_point(x, y, image[x][y]);
55 }
56 }
57 }
58 return resulted_img.get_2d_array();
59}
60
67inline std::vector<std::vector<int32_t>> open(const std::vector<std::vector<int32_t>>& image) {
68 Image img(image);
69 assert(img.binary());
70 std::vector<std::vector<int32_t>> resulted_img = erote(image); // first erote the image
71 resulted_img = dilate(resulted_img); // then dilate the image
72 return resulted_img;
73}
74
81inline std::vector<std::vector<int32_t>> close(const std::vector<std::vector<int32_t>>& image) {
82 Image img(image);
83 assert(img.binary());
84 std::vector<std::vector<int32_t>> resulted_img = dilate(image);
85 resulted_img = erote(resulted_img);
86 return resulted_img;
87}
88} // namespace morphology_operations
89
90#endif