mirror of https://github.com/FFmpeg/FFmpeg.git
parent
f2724d2b69
commit
22df52c444
4 changed files with 296 additions and 177 deletions
@ -0,0 +1,181 @@ |
|||||||
|
/*
|
||||||
|
* This file is part of FFmpeg. |
||||||
|
* |
||||||
|
* FFmpeg is free software; you can redistribute it and/or |
||||||
|
* modify it under the terms of the GNU Lesser General Public |
||||||
|
* License as published by the Free Software Foundation; either |
||||||
|
* version 2.1 of the License, or (at your option) any later version. |
||||||
|
* |
||||||
|
* FFmpeg is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||||
|
* Lesser General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Lesser General Public |
||||||
|
* License along with FFmpeg; if not, write to the Free Software |
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "edge_common.h" |
||||||
|
|
||||||
|
// Internal helper for ff_sobel()
|
||||||
|
static int get_rounded_direction(int gx, int gy) |
||||||
|
{ |
||||||
|
/* reference angles:
|
||||||
|
* tan( pi/8) = sqrt(2)-1 |
||||||
|
* tan(3pi/8) = sqrt(2)+1 |
||||||
|
* Gy/Gx is the tangent of the angle (theta), so Gy/Gx is compared against |
||||||
|
* <ref-angle>, or more simply Gy against <ref-angle>*Gx |
||||||
|
* |
||||||
|
* Gx and Gy bounds = [-1020;1020], using 16-bit arithmetic: |
||||||
|
* round((sqrt(2)-1) * (1<<16)) = 27146 |
||||||
|
* round((sqrt(2)+1) * (1<<16)) = 158218 |
||||||
|
*/ |
||||||
|
if (gx) { |
||||||
|
int tanpi8gx, tan3pi8gx; |
||||||
|
|
||||||
|
if (gx < 0) |
||||||
|
gx = -gx, gy = -gy; |
||||||
|
gy *= (1 << 16); |
||||||
|
tanpi8gx = 27146 * gx; |
||||||
|
tan3pi8gx = 158218 * gx; |
||||||
|
if (gy > -tan3pi8gx && gy < -tanpi8gx) return DIRECTION_45UP; |
||||||
|
if (gy > -tanpi8gx && gy < tanpi8gx) return DIRECTION_HORIZONTAL; |
||||||
|
if (gy > tanpi8gx && gy < tan3pi8gx) return DIRECTION_45DOWN; |
||||||
|
} |
||||||
|
return DIRECTION_VERTICAL; |
||||||
|
} |
||||||
|
|
||||||
|
// Simple sobel operator to get rounded gradients
|
||||||
|
void ff_sobel(int w, int h, |
||||||
|
uint16_t *dst, int dst_linesize, |
||||||
|
int8_t *dir, int dir_linesize, |
||||||
|
const uint8_t *src, int src_linesize) |
||||||
|
{ |
||||||
|
int i, j; |
||||||
|
|
||||||
|
for (j = 1; j < h - 1; j++) { |
||||||
|
dst += dst_linesize; |
||||||
|
dir += dir_linesize; |
||||||
|
src += src_linesize; |
||||||
|
for (i = 1; i < w - 1; i++) { |
||||||
|
const int gx = |
||||||
|
-1*src[-src_linesize + i-1] + 1*src[-src_linesize + i+1] |
||||||
|
-2*src[ i-1] + 2*src[ i+1] |
||||||
|
-1*src[ src_linesize + i-1] + 1*src[ src_linesize + i+1]; |
||||||
|
const int gy = |
||||||
|
-1*src[-src_linesize + i-1] + 1*src[ src_linesize + i-1] |
||||||
|
-2*src[-src_linesize + i ] + 2*src[ src_linesize + i ] |
||||||
|
-1*src[-src_linesize + i+1] + 1*src[ src_linesize + i+1]; |
||||||
|
|
||||||
|
dst[i] = FFABS(gx) + FFABS(gy); |
||||||
|
dir[i] = get_rounded_direction(gx, gy); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Filters rounded gradients to drop all non-maxima
|
||||||
|
// Expects gradients generated by ff_sobel()
|
||||||
|
// Expects zero's destination buffer
|
||||||
|
void ff_non_maximum_suppression(int w, int h, |
||||||
|
uint8_t *dst, int dst_linesize, |
||||||
|
const int8_t *dir, int dir_linesize, |
||||||
|
const uint16_t *src, int src_linesize) |
||||||
|
{ |
||||||
|
int i, j; |
||||||
|
|
||||||
|
#define COPY_MAXIMA(ay, ax, by, bx) do { \ |
||||||
|
if (src[i] > src[(ay)*src_linesize + i+(ax)] && \
|
||||||
|
src[i] > src[(by)*src_linesize + i+(bx)]) \
|
||||||
|
dst[i] = av_clip_uint8(src[i]); \
|
||||||
|
} while (0) |
||||||
|
|
||||||
|
for (j = 1; j < h - 1; j++) { |
||||||
|
dst += dst_linesize; |
||||||
|
dir += dir_linesize; |
||||||
|
src += src_linesize; |
||||||
|
for (i = 1; i < w - 1; i++) { |
||||||
|
switch (dir[i]) { |
||||||
|
case DIRECTION_45UP: COPY_MAXIMA( 1, -1, -1, 1); break; |
||||||
|
case DIRECTION_45DOWN: COPY_MAXIMA(-1, -1, 1, 1); break; |
||||||
|
case DIRECTION_HORIZONTAL: COPY_MAXIMA( 0, -1, 0, 1); break; |
||||||
|
case DIRECTION_VERTICAL: COPY_MAXIMA(-1, 0, 1, 0); break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Filter to keep all pixels > high, and keep all pixels > low where all surrounding pixels > high
|
||||||
|
void ff_double_threshold(int low, int high, int w, int h, |
||||||
|
uint8_t *dst, int dst_linesize, |
||||||
|
const uint8_t *src, int src_linesize) |
||||||
|
{ |
||||||
|
int i, j; |
||||||
|
|
||||||
|
for (j = 0; j < h; j++) { |
||||||
|
for (i = 0; i < w; i++) { |
||||||
|
if (src[i] > high) { |
||||||
|
dst[i] = src[i]; |
||||||
|
continue; |
||||||
|
} |
||||||
|
|
||||||
|
if (!(!i || i == w - 1 || !j || j == h - 1) && |
||||||
|
src[i] > low && |
||||||
|
(src[-src_linesize + i-1] > high || |
||||||
|
src[-src_linesize + i ] > high || |
||||||
|
src[-src_linesize + i+1] > high || |
||||||
|
src[ i-1] > high || |
||||||
|
src[ i+1] > high || |
||||||
|
src[ src_linesize + i-1] > high || |
||||||
|
src[ src_linesize + i ] > high || |
||||||
|
src[ src_linesize + i+1] > high)) |
||||||
|
dst[i] = src[i]; |
||||||
|
else |
||||||
|
dst[i] = 0; |
||||||
|
} |
||||||
|
dst += dst_linesize; |
||||||
|
src += src_linesize; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Applies gaussian blur, using 5x5 kernels, sigma = 1.4
|
||||||
|
void ff_gaussian_blur(int w, int h, |
||||||
|
uint8_t *dst, int dst_linesize, |
||||||
|
const uint8_t *src, int src_linesize) |
||||||
|
{ |
||||||
|
int i, j; |
||||||
|
|
||||||
|
memcpy(dst, src, w); dst += dst_linesize; src += src_linesize; |
||||||
|
memcpy(dst, src, w); dst += dst_linesize; src += src_linesize; |
||||||
|
for (j = 2; j < h - 2; j++) { |
||||||
|
dst[0] = src[0]; |
||||||
|
dst[1] = src[1]; |
||||||
|
for (i = 2; i < w - 2; i++) { |
||||||
|
/* Gaussian mask of size 5x5 with sigma = 1.4 */ |
||||||
|
dst[i] = ((src[-2*src_linesize + i-2] + src[2*src_linesize + i-2]) * 2 |
||||||
|
+ (src[-2*src_linesize + i-1] + src[2*src_linesize + i-1]) * 4 |
||||||
|
+ (src[-2*src_linesize + i ] + src[2*src_linesize + i ]) * 5 |
||||||
|
+ (src[-2*src_linesize + i+1] + src[2*src_linesize + i+1]) * 4 |
||||||
|
+ (src[-2*src_linesize + i+2] + src[2*src_linesize + i+2]) * 2 |
||||||
|
|
||||||
|
+ (src[ -src_linesize + i-2] + src[ src_linesize + i-2]) * 4 |
||||||
|
+ (src[ -src_linesize + i-1] + src[ src_linesize + i-1]) * 9 |
||||||
|
+ (src[ -src_linesize + i ] + src[ src_linesize + i ]) * 12 |
||||||
|
+ (src[ -src_linesize + i+1] + src[ src_linesize + i+1]) * 9 |
||||||
|
+ (src[ -src_linesize + i+2] + src[ src_linesize + i+2]) * 4 |
||||||
|
|
||||||
|
+ src[i-2] * 5 |
||||||
|
+ src[i-1] * 12 |
||||||
|
+ src[i ] * 15 |
||||||
|
+ src[i+1] * 12 |
||||||
|
+ src[i+2] * 5) / 159; |
||||||
|
} |
||||||
|
dst[i ] = src[i ]; |
||||||
|
dst[i + 1] = src[i + 1]; |
||||||
|
|
||||||
|
dst += dst_linesize; |
||||||
|
src += src_linesize; |
||||||
|
} |
||||||
|
memcpy(dst, src, w); dst += dst_linesize; src += src_linesize; |
||||||
|
memcpy(dst, src, w); |
||||||
|
} |
@ -0,0 +1,107 @@ |
|||||||
|
/*
|
||||||
|
* This file is part of FFmpeg. |
||||||
|
* |
||||||
|
* FFmpeg is free software; you can redistribute it and/or |
||||||
|
* modify it under the terms of the GNU Lesser General Public |
||||||
|
* License as published by the Free Software Foundation; either |
||||||
|
* version 2.1 of the License, or (at your option) any later version. |
||||||
|
* |
||||||
|
* FFmpeg is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||||
|
* Lesser General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Lesser General Public |
||||||
|
* License along with FFmpeg; if not, write to the Free Software |
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||||
|
*/ |
||||||
|
|
||||||
|
/**
|
||||||
|
* @file |
||||||
|
* common functions for edge detection |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef AVFILTER_EDGE_COMMON_H |
||||||
|
#define AVFILTER_EDGE_COMMON_H |
||||||
|
|
||||||
|
#include "avfilter.h" |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Rounded directions used in av_image_sobel() |
||||||
|
*/ |
||||||
|
enum AVRoundedDirection { |
||||||
|
DIRECTION_45UP, |
||||||
|
DIRECTION_45DOWN, |
||||||
|
DIRECTION_HORIZONTAL, |
||||||
|
DIRECTION_VERTICAL, |
||||||
|
}; |
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple sobel operator to get rounded gradients |
||||||
|
* |
||||||
|
* @param w the width of the image in pixels |
||||||
|
* @param h the height of the image in pixels |
||||||
|
* @param dst data pointers to magnitude image |
||||||
|
* @param dst_linesize linesizes for the magnitude image |
||||||
|
* @param dir data pointers to direction image |
||||||
|
* @param dir_linesize linesizes for the direction image |
||||||
|
* @param src data pointers to source image |
||||||
|
* @param src_linesize linesizes for the source image |
||||||
|
*/ |
||||||
|
void ff_sobel(int w, int h, |
||||||
|
uint16_t *dst, int dst_linesize, |
||||||
|
int8_t *dir, int dir_linesize, |
||||||
|
const uint8_t *src, int src_linesize); |
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters rounded gradients to drop all non-maxima pixels in the magnitude image |
||||||
|
* Expects gradients generated by av_image_sobel() |
||||||
|
* Expects zero's in the destination buffer dst |
||||||
|
* |
||||||
|
* @param w the width of the image in pixels |
||||||
|
* @param h the height of the image in pixels |
||||||
|
* @param dst data pointers to magnitude image |
||||||
|
* @param dst_linesize linesizes for the magnitude image |
||||||
|
* @param dir data pointers to direction image |
||||||
|
* @param dir_linesize linesizes for the direction image |
||||||
|
* @param src data pointers to source image |
||||||
|
* @param src_linesize linesizes for the source image |
||||||
|
*/ |
||||||
|
void ff_non_maximum_suppression(int w, int h, |
||||||
|
uint8_t *dst, int dst_linesize, |
||||||
|
const int8_t *dir, int dir_linesize, |
||||||
|
const uint16_t *src, int src_linesize); |
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters all pixels in src to keep all pixels > high, |
||||||
|
* and keep all pixels > low where all surrounding pixels > high |
||||||
|
* |
||||||
|
* @param low the low threshold value |
||||||
|
* @param high the hegh threshold value |
||||||
|
* @param w the width of the image in pixels |
||||||
|
* @param h the height of the image in pixels |
||||||
|
* @param dst data pointers to destination image |
||||||
|
* @param dst_linesize linesizes for the destination image |
||||||
|
* @param src data pointers to source image |
||||||
|
* @param src_linesize linesizes for the source image |
||||||
|
*/ |
||||||
|
void ff_double_threshold(int low, int high, int w, int h, |
||||||
|
uint8_t *dst, int dst_linesize, |
||||||
|
const uint8_t *src, int src_linesize); |
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies gaussian blur. |
||||||
|
* 5x5 kernels, sigma = 1.4 |
||||||
|
* |
||||||
|
* @param w the width of the image in pixels |
||||||
|
* @param h the height of the image in pixels |
||||||
|
* @param dst data pointers to destination image |
||||||
|
* @param dst_linesize linesizes for the destination image |
||||||
|
* @param src data pointers to source image |
||||||
|
* @param src_linesize linesizes for the source image |
||||||
|
*/ |
||||||
|
void ff_gaussian_blur(int w, int h, |
||||||
|
uint8_t *dst, int dst_linesize, |
||||||
|
const uint8_t *src, int src_linesize); |
||||||
|
|
||||||
|
#endif |
Loading…
Reference in new issue