AlgoPlus
v0.1.0
Loading...
Searching...
No Matches
src
machine_learning
image
edge_detection
prewitt.h
1
#ifndef PREWITT_H
2
#define PREWITT_H
3
4
#ifdef __cplusplus
5
#include <cmath>
6
#include <iostream>
7
#include <vector>
8
#include "../image.h"
9
#endif
10
17
namespace
prewitt
{
23
inline
std::vector<std::vector<int32_t>>
square
(
const
std::vector<std::vector<int32_t>>& G) {
24
int
height = G.size();
25
int
width = G[0].size();
26
std::vector<std::vector<int32_t>> res_img(height, std::vector<int32_t>(width));
27
for
(
int
i = 0; i < height; i++) {
28
for
(
int
j = 0; j < width; j++) {
29
res_img[i][j] = sqrt(G[i][j]);
30
}
31
}
32
return
res_img;
33
}
34
42
inline
std::vector<std::vector<int32_t>>
Prewitt
(
const
std::vector<std::vector<int32_t>>& image) {
43
Image
img(image);
44
std::vector<std::vector<int32_t>> k1 = {{-1, -1, -1}, {0, 0, 0}, {1, 1, 1}};
45
std::vector<std::vector<int32_t>> k2 = {{-1, 0, 1}, {-1, 0, 1}, {-1, 0, 1}};
46
47
Image
G_x(img.
apply_filter2d
(k1));
48
Image
G_y(img.
apply_filter2d
(k2));
49
50
G_x = G_x.
mul
(G_x);
51
G_y = G_y.
mul
(G_y);
52
53
Image
G(G_x);
// G = G_x^2
54
G = G.
add
(G_y);
// G += G_y^2
55
56
return
square
(G.
get_2d_array
());
// result is: G = sqrt(G_x^2 + G_y^2)
57
}
58
}
// namespace prewitt
59
60
#endif
Image
Definition
image.h:13
Image::add
Image add(const T img2) const
add function adds the img2 to img
Definition
image.h:121
Image::get_2d_array
std::vector< std::vector< int32_t > > get_2d_array() const
get_2d_array function
Definition
image.h:63
Image::mul
Image mul(const T img2) const
mul function multiplies the img2 to img
Definition
image.h:179
Image::apply_filter2d
Image apply_filter2d(std::vector< std::vector< T > > &filter) const
apply_filter2d function
Definition
image.h:207
prewitt
Prewitt operator is used for edge detection. It creates 2 3x3 arrays G_x and G_y and apply them to an...
Definition
prewitt.h:17
prewitt::Prewitt
std::vector< std::vector< int32_t > > Prewitt(const std::vector< std::vector< int32_t > > &image)
Prewitt apply function.
Definition
prewitt.h:42
prewitt::square
std::vector< std::vector< int32_t > > square(const std::vector< std::vector< int32_t > > &G)
square function
Definition
prewitt.h:23
Generated by
1.13.2