Skip to content

Instantly share code, notes, and snippets.

@merryhime
Created August 13, 2016 16:50
Show Gist options
  • Save merryhime/68c1b03fca5cc01e63f53e1f4a1a8527 to your computer and use it in GitHub Desktop.
Save merryhime/68c1b03fca5cc01e63f53e1f4a1a8527 to your computer and use it in GitHub Desktop.
// It's an angled sobel operator.
// Pseudocode follows:
// input is RGB, output is greyscale
auto edge_detect (u8 input_image[W][L][3]) → u8[W][L] {
u8 output_image[W][L] = {0};
constexpr double angle = -135.0;
double radians = angle * 2.0 * PI / 360.0;
// Calculate kernel to convolve with.
constepxr double dr = PI / 4.0;
double kernel[3][3];
kernel[0][0] = cos(radians + dr);
kernel[0][1] = cos(radians + 2.0 * dr);
kernel[0][2] = cos(radians + 3.0 * dr);
kernel[1][0] = cos(radians);
kernel[1][1] = 0;
kernel[1][2] = cos(radians + 4.0 * dr);
kernel[2][0] = cos(radians - dr);
kernel[2][1] = cos(radians - 2.0 * dr);
kernel[2][2] = cos(radians - 3.0 * dr);
for (image_y = 1..H-2)
for (image_x = 1..W-2) {
double R = 0.0;
double G = 0.0;
double B = 0.0;
for (i = 0..2)
for (j = 0..2) {
std::array<u8, 3> in_px = input_image[image_x + i - 1][image_y + j - 1];
R += in_px[0] * kernel[i][j];
G += in_px[1] * kernel[i][j];
B += in_px[2] * kernel[i][j];
}
R = Clamp(R, 0, 255);
G = Clamp(G, 0, 255);
B = Clamp(B, 0, 255);
output_image[image_x][image_y] = (R+B+G)/3.0;
}
return output_image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment