You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
11KB

  1. /*
  2. * Copyright (c) 2012 Clément Bœsch
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Edge detection filter
  23. *
  24. * @url https://en.wikipedia.org/wiki/Canny_edge_detector
  25. */
  26. #include "libavutil/opt.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. typedef struct {
  32. const AVClass *class;
  33. uint8_t *tmpbuf;
  34. uint16_t *gradients;
  35. char *directions;
  36. double low, high;
  37. uint8_t low_u8, high_u8;
  38. } EdgeDetectContext;
  39. #define OFFSET(x) offsetof(EdgeDetectContext, x)
  40. static const AVOption edgedetect_options[] = {
  41. { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_DOUBLE, {.dbl=50/255.}, 0, 1 },
  42. { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_DOUBLE, {.dbl=20/255.}, 0, 1 },
  43. { NULL },
  44. };
  45. AVFILTER_DEFINE_CLASS(edgedetect);
  46. static av_cold int init(AVFilterContext *ctx, const char *args)
  47. {
  48. int ret;
  49. EdgeDetectContext *edgedetect = ctx->priv;
  50. edgedetect->class = &edgedetect_class;
  51. av_opt_set_defaults(edgedetect);
  52. if ((ret = av_set_options_string(edgedetect, args, "=", ":")) < 0)
  53. return ret;
  54. edgedetect->low_u8 = edgedetect->low * 255. + .5;
  55. edgedetect->high_u8 = edgedetect->high * 255. + .5;
  56. return 0;
  57. }
  58. static int query_formats(AVFilterContext *ctx)
  59. {
  60. static const enum PixelFormat pix_fmts[] = {PIX_FMT_GRAY8, PIX_FMT_NONE};
  61. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  62. return 0;
  63. }
  64. static int config_props(AVFilterLink *inlink)
  65. {
  66. AVFilterContext *ctx = inlink->dst;
  67. EdgeDetectContext *edgedetect = ctx->priv;
  68. edgedetect->tmpbuf = av_malloc(inlink->w * inlink->h);
  69. edgedetect->gradients = av_calloc(inlink->w * inlink->h, sizeof(*edgedetect->gradients));
  70. edgedetect->directions = av_malloc(inlink->w * inlink->h);
  71. if (!edgedetect->tmpbuf || !edgedetect->gradients || !edgedetect->directions)
  72. return AVERROR(ENOMEM);
  73. return 0;
  74. }
  75. static void gaussian_blur(AVFilterContext *ctx, int w, int h,
  76. uint8_t *dst, int dst_linesize,
  77. const uint8_t *src, int src_linesize)
  78. {
  79. int i, j;
  80. memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
  81. memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
  82. for (j = 2; j < h - 2; j++) {
  83. dst[0] = src[0];
  84. dst[1] = src[1];
  85. for (i = 2; i < w - 2; i++) {
  86. /* Gaussian mask of size 5x5 with sigma = 1.4 */
  87. dst[i] = ((src[-2*src_linesize + i-2] + src[2*src_linesize + i-2]) * 2
  88. + (src[-2*src_linesize + i-1] + src[2*src_linesize + i-1]) * 4
  89. + (src[-2*src_linesize + i ] + src[2*src_linesize + i ]) * 5
  90. + (src[-2*src_linesize + i+1] + src[2*src_linesize + i+1]) * 4
  91. + (src[-2*src_linesize + i+2] + src[2*src_linesize + i+2]) * 2
  92. + (src[ -src_linesize + i-2] + src[ src_linesize + i-2]) * 4
  93. + (src[ -src_linesize + i-1] + src[ src_linesize + i-1]) * 9
  94. + (src[ -src_linesize + i ] + src[ src_linesize + i ]) * 12
  95. + (src[ -src_linesize + i+1] + src[ src_linesize + i+1]) * 9
  96. + (src[ -src_linesize + i+2] + src[ src_linesize + i+2]) * 4
  97. + src[i-2] * 5
  98. + src[i-1] * 12
  99. + src[i ] * 15
  100. + src[i+1] * 12
  101. + src[i+2] * 5) / 159;
  102. }
  103. dst[i ] = src[i ];
  104. dst[i + 1] = src[i + 1];
  105. dst += dst_linesize;
  106. src += src_linesize;
  107. }
  108. memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
  109. memcpy(dst, src, w);
  110. }
  111. enum {
  112. DIRECTION_45UP,
  113. DIRECTION_45DOWN,
  114. DIRECTION_HORIZONTAL,
  115. DIRECTION_VERTICAL,
  116. };
  117. static int get_rounded_direction(int gx, int gy)
  118. {
  119. /* reference angles:
  120. * tan( pi/8) = sqrt(2)-1
  121. * tan(3pi/8) = sqrt(2)+1
  122. * Gy/Gx is the tangent of the angle (theta), so Gy/Gx is compared against
  123. * <ref-angle>, or more simply Gy against <ref-angle>*Gx
  124. *
  125. * Gx and Gy bounds = [1020;1020], using 16-bit arithmetic:
  126. * round((sqrt(2)-1) * (1<<16)) = 27146
  127. * round((sqrt(2)+1) * (1<<16)) = 158218
  128. */
  129. if (gx) {
  130. int tanpi8gx, tan3pi8gx;
  131. if (gx < 0)
  132. gx = -gx, gy = -gy;
  133. gy <<= 16;
  134. tanpi8gx = 27146 * gx;
  135. tan3pi8gx = 158218 * gx;
  136. if (gy > -tan3pi8gx && gy < -tanpi8gx) return DIRECTION_45UP;
  137. if (gy > -tanpi8gx && gy < tanpi8gx) return DIRECTION_HORIZONTAL;
  138. if (gy > tanpi8gx && gy < tan3pi8gx) return DIRECTION_45DOWN;
  139. }
  140. return DIRECTION_VERTICAL;
  141. }
  142. static void sobel(AVFilterContext *ctx, int w, int h,
  143. uint16_t *dst, int dst_linesize,
  144. const uint8_t *src, int src_linesize)
  145. {
  146. int i, j;
  147. EdgeDetectContext *edgedetect = ctx->priv;
  148. for (j = 1; j < h - 1; j++) {
  149. dst += dst_linesize;
  150. src += src_linesize;
  151. for (i = 1; i < w - 1; i++) {
  152. const int gx =
  153. -1*src[-src_linesize + i-1] + 1*src[-src_linesize + i+1]
  154. -2*src[ i-1] + 2*src[ i+1]
  155. -1*src[ src_linesize + i-1] + 1*src[ src_linesize + i+1];
  156. const int gy =
  157. -1*src[-src_linesize + i-1] + 1*src[ src_linesize + i-1]
  158. -2*src[-src_linesize + i ] + 2*src[ src_linesize + i ]
  159. -1*src[-src_linesize + i+1] + 1*src[ src_linesize + i+1];
  160. dst[i] = FFABS(gx) + FFABS(gy);
  161. edgedetect->directions[j*w + i] = get_rounded_direction(gx, gy);
  162. }
  163. }
  164. }
  165. static void non_maximum_suppression(AVFilterContext *ctx, int w, int h,
  166. uint8_t *dst, int dst_linesize,
  167. const uint16_t *src, int src_linesize)
  168. {
  169. int i, j;
  170. EdgeDetectContext *edgedetect = ctx->priv;
  171. #define COPY_MAXIMA(ay, ax, by, bx) do { \
  172. if (src[i] > src[(ay)*src_linesize + i+(ax)] && \
  173. src[i] > src[(by)*src_linesize + i+(bx)]) \
  174. dst[i] = av_clip_uint8(src[i]); \
  175. } while (0)
  176. for (j = 1; j < h - 1; j++) {
  177. dst += dst_linesize;
  178. src += src_linesize;
  179. for (i = 1; i < w - 1; i++) {
  180. switch (edgedetect->directions[j*w + i]) {
  181. case DIRECTION_45UP: COPY_MAXIMA( 1, -1, -1, 1); break;
  182. case DIRECTION_45DOWN: COPY_MAXIMA(-1, -1, 1, 1); break;
  183. case DIRECTION_HORIZONTAL: COPY_MAXIMA( 0, -1, 0, 1); break;
  184. case DIRECTION_VERTICAL: COPY_MAXIMA(-1, 0, 1, 0); break;
  185. }
  186. }
  187. }
  188. }
  189. static void double_threshold(AVFilterContext *ctx, int w, int h,
  190. uint8_t *dst, int dst_linesize,
  191. const uint8_t *src, int src_linesize)
  192. {
  193. int i, j;
  194. EdgeDetectContext *edgedetect = ctx->priv;
  195. const int low = edgedetect->low_u8;
  196. const int high = edgedetect->high_u8;
  197. for (j = 0; j < h; j++) {
  198. for (i = 0; i < w; i++) {
  199. if (src[i] > high) {
  200. dst[i] = src[i];
  201. continue;
  202. }
  203. if ((!i || i == w - 1 || !j || j == h - 1) &&
  204. src[i] > low &&
  205. (src[-src_linesize + i-1] > high ||
  206. src[-src_linesize + i ] > high ||
  207. src[-src_linesize + i+1] > high ||
  208. src[ i-1] > high ||
  209. src[ i+1] > high ||
  210. src[ src_linesize + i-1] > high ||
  211. src[ src_linesize + i ] > high ||
  212. src[ src_linesize + i+1] > high))
  213. dst[i] = src[i];
  214. else
  215. dst[i] = 0;
  216. }
  217. dst += dst_linesize;
  218. src += src_linesize;
  219. }
  220. }
  221. static int end_frame(AVFilterLink *inlink)
  222. {
  223. AVFilterContext *ctx = inlink->dst;
  224. EdgeDetectContext *edgedetect = ctx->priv;
  225. AVFilterLink *outlink = inlink->dst->outputs[0];
  226. AVFilterBufferRef *inpicref = inlink->cur_buf;
  227. AVFilterBufferRef *outpicref = outlink->out_buf;
  228. uint8_t *tmpbuf = edgedetect->tmpbuf;
  229. uint16_t *gradients = edgedetect->gradients;
  230. /* gaussian filter to reduce noise */
  231. gaussian_blur(ctx, inlink->w, inlink->h,
  232. tmpbuf, inlink->w,
  233. inpicref->data[0], inpicref->linesize[0]);
  234. /* compute the 16-bits gradients and directions for the next step */
  235. sobel(ctx, inlink->w, inlink->h,
  236. gradients, inlink->w,
  237. tmpbuf, inlink->w);
  238. /* non_maximum_suppression() will actually keep & clip what's necessary and
  239. * ignore the rest, so we need a clean output buffer */
  240. memset(tmpbuf, 0, inlink->w * inlink->h);
  241. non_maximum_suppression(ctx, inlink->w, inlink->h,
  242. tmpbuf, inlink->w,
  243. gradients, inlink->w);
  244. /* keep high values, or low values surrounded by high values */
  245. double_threshold(ctx, inlink->w, inlink->h,
  246. outpicref->data[0], outpicref->linesize[0],
  247. tmpbuf, inlink->w);
  248. ff_draw_slice(outlink, 0, outlink->h, 1);
  249. return ff_end_frame(outlink);
  250. }
  251. static av_cold void uninit(AVFilterContext *ctx)
  252. {
  253. EdgeDetectContext *edgedetect = ctx->priv;
  254. av_freep(&edgedetect->tmpbuf);
  255. av_freep(&edgedetect->gradients);
  256. av_freep(&edgedetect->directions);
  257. }
  258. static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { return 0; }
  259. AVFilter avfilter_vf_edgedetect = {
  260. .name = "edgedetect",
  261. .description = NULL_IF_CONFIG_SMALL("Detect and draw edge."),
  262. .priv_size = sizeof(EdgeDetectContext),
  263. .init = init,
  264. .uninit = uninit,
  265. .query_formats = query_formats,
  266. .inputs = (const AVFilterPad[]) {
  267. {
  268. .name = "default",
  269. .type = AVMEDIA_TYPE_VIDEO,
  270. .draw_slice = null_draw_slice,
  271. .config_props = config_props,
  272. .end_frame = end_frame,
  273. .min_perms = AV_PERM_READ
  274. },
  275. { .name = NULL }
  276. },
  277. .outputs = (const AVFilterPad[]) {
  278. {
  279. .name = "default",
  280. .type = AVMEDIA_TYPE_VIDEO,
  281. },
  282. { .name = NULL }
  283. },
  284. };