Version: 1.0
Loading...
Searching...
No Matches
Canny Edge Detection Example

#include <stdio.h>
#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image/stb_image_write.h"
int main(void) {
int width, height, channels;
unsigned char* inputImage = stbi_load("test.jpg", &width, &height, &channels, 0);
if (inputImage) {
unsigned char* outputImage = (unsigned char*)calloc(width * channels * height * sizeof(unsigned char), 1);
if (outputImage) {
int stride = width * channels;
int lower_threshold = 30; // pixels below this value are ignored
int upper_threshold = 100; // pixels above this value are considered as edge pixels
// Gaussian Noise reduction kernel
// CannyGaus3x3 = 3 x 3 Gaussian
// CannyGaus5x5 = 5 x 5 Gaussian
OC_STATUS status;
status = ocularGrayscaleFilter(inputImage, inputImage, width, height, stride);
if (status == OC_STATUS_OK) {
channels = 1; // grayscale filter converts data to single channel, so reset channels
status = ocularCannyEdgeDetect(inputImage, outputImage, width, height, channels, CannyGaus3x3, lower_threshold, upper_threshold);
if (status == OC_STATUS_OK) {
stbi_write_jpg("test_out.jpg", width, height, channels, outputImage, 100);
}
}
}
free(outputImage);
}
stbi_image_free(inputImage);
}
OC_STATUS ocularGrayscaleFilter(unsigned char *Input, unsigned char *Output, int Width, int Height, int Stride)
Converts an RGB image to single channel grayscale (a slightly faster implementation of the saturation...

Before

After