From 04f2f3d766d6ceb2b5f4120be28cab04816e048b Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 1 Feb 2013 13:23:34 +0100 Subject: util: Add sobel operator --- src/util.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/util.h | 3 +++ 2 files changed, 45 insertions(+) diff --git a/src/util.c b/src/util.c index ddd46d3..ddb3ce9 100644 --- a/src/util.c +++ b/src/util.c @@ -136,3 +136,45 @@ print_time(int64_t msec) { printf("%02d:%02d:%02d.%03jd", hh, mm, ss, msec); } + +int +sobel(struct video_frame *frame, unsigned char **d) +{ + unsigned char *data; + unsigned char *p = frame->data; + int x,y; + int Yx, Yy, Y; + + data = malloc(frame->width * frame->height); + if (data == NULL) + return -1; + + for (y = 1; y < frame->height-1; ++y) { + for (x = 1; x < frame->width-1; ++x) { + Yx = (+1 * p[(y-1) * frame->stride + (x-1)] + -1 * p[(y-1) * frame->stride + (x+1)] + +2 * p[(y ) * frame->stride + (x-1)] + -2 * p[(y ) * frame->stride + (x+1)] + +1 * p[(y+1) * frame->stride + (x-1)] + -1 * p[(y+1) * frame->stride + (x+1)]); + Yx = abs(Yx); + + Yy = (+1 * p[(y-1) * frame->stride + (x-1)] + +2 * p[(y-1) * frame->stride + (x )] + +1 * p[(y-1) * frame->stride + (x+1)] + -1 * p[(y+1) * frame->stride + (x-1)] + -2 * p[(y+1) * frame->stride + (x )] + -1 * p[(y+1) * frame->stride + (x+1)]); + Yy = abs(Yy); + + Y = (Yx > Yy ? Yx : Yy) / 4; + if (Y > 255) Y = 255; + + data[y * frame->width + x] = Y; + } + } + + *d = data; + + return 0; +} diff --git a/src/util.h b/src/util.h index 4666b06..22d5587 100644 --- a/src/util.h +++ b/src/util.h @@ -32,4 +32,7 @@ frame_average(struct video_frame *frame); void print_time(int64_t msec); +int +sobel(struct video_frame *frame, unsigned char **d); + #endif /* _UTIL_H_ */ -- cgit