vf_edgedetect.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. * @see 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. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  41. static const AVOption edgedetect_options[] = {
  42. { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_DOUBLE, {.dbl=50/255.}, 0, 1, FLAGS },
  43. { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_DOUBLE, {.dbl=20/255.}, 0, 1, FLAGS },
  44. { NULL },
  45. };
  46. AVFILTER_DEFINE_CLASS(edgedetect);
  47. static av_cold int init(AVFilterContext *ctx, const char *args)
  48. {
  49. int ret;
  50. EdgeDetectContext *edgedetect = ctx->priv;
  51. edgedetect->class = &edgedetect_class;
  52. av_opt_set_defaults(edgedetect);
  53. if ((ret = av_set_options_string(edgedetect, args, "=", ":")) < 0)
  54. return ret;
  55. edgedetect->low_u8 = edgedetect->low * 255. + .5;
  56. edgedetect->high_u8 = edgedetect->high * 255. + .5;
  57. return 0;
  58. }
  59. static int query_formats(AVFilterContext *ctx)
  60. {
  61. static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
  62. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  63. return 0;
  64. }
  65. static int config_props(AVFilterLink *inlink)
  66. {
  67. AVFilterContext *ctx = inlink->dst;
  68. EdgeDetectContext *edgedetect = ctx->priv;
  69. edgedetect->tmpbuf = av_malloc(inlink->w * inlink->h);
  70. edgedetect->gradients = av_calloc(inlink->w * inlink->h, sizeof(*edgedetect->gradients));
  71. edgedetect->directions = av_malloc(inlink->w * inlink->h);
  72. if (!edgedetect->tmpbuf || !edgedetect->gradients || !edgedetect->directions)
  73. return AVERROR(ENOMEM);
  74. return 0;
  75. }
  76. static void gaussian_blur(AVFilterContext *ctx, int w, int h,
  77. uint8_t *dst, int dst_linesize,
  78. const uint8_t *src, int src_linesize)
  79. {
  80. int i, j;
  81. memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
  82. memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
  83. for (j = 2; j < h - 2; j++) {
  84. dst[0] = src[0];
  85. dst[1] = src[1];
  86. for (i = 2; i < w - 2; i++) {
  87. /* Gaussian mask of size 5x5 with sigma = 1.4 */
  88. dst[i] = ((src[-2*src_linesize + i-2] + src[2*src_linesize + i-2]) * 2
  89. + (src[-2*src_linesize + i-1] + src[2*src_linesize + i-1]) * 4
  90. + (src[-2*src_linesize + i ] + src[2*src_linesize + i ]) * 5
  91. + (src[-2*src_linesize + i+1] + src[2*src_linesize + i+1]) * 4
  92. + (src[-2*src_linesize + i+2] + src[2*src_linesize + i+2]) * 2
  93. + (src[ -src_linesize + i-2] + src[ src_linesize + i-2]) * 4
  94. + (src[ -src_linesize + i-1] + src[ src_linesize + i-1]) * 9
  95. + (src[ -src_linesize + i ] + src[ src_linesize + i ]) * 12
  96. + (src[ -src_linesize + i+1] + src[ src_linesize + i+1]) * 9
  97. + (src[ -src_linesize + i+2] + src[ src_linesize + i+2]) * 4
  98. + src[i-2] * 5
  99. + src[i-1] * 12
  100. + src[i ] * 15
  101. + src[i+1] * 12
  102. + src[i+2] * 5) / 159;
  103. }
  104. dst[i ] = src[i ];
  105. dst[i + 1] = src[i + 1];
  106. dst += dst_linesize;
  107. src += src_linesize;
  108. }
  109. memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
  110. memcpy(dst, src, w);
  111. }
  112. enum {
  113. DIRECTION_45UP,
  114. DIRECTION_45DOWN,
  115. DIRECTION_HORIZONTAL,
  116. DIRECTION_VERTICAL,
  117. };
  118. static int get_rounded_direction(int gx, int gy)
  119. {
  120. /* reference angles:
  121. * tan( pi/8) = sqrt(2)-1
  122. * tan(3pi/8) = sqrt(2)+1
  123. * Gy/Gx is the tangent of the angle (theta), so Gy/Gx is compared against
  124. * <ref-angle>, or more simply Gy against <ref-angle>*Gx
  125. *
  126. * Gx and Gy bounds = [-1020;1020], using 16-bit arithmetic:
  127. * round((sqrt(2)-1) * (1<<16)) = 27146
  128. * round((sqrt(2)+1) * (1<<16)) = 158218
  129. */
  130. if (gx) {
  131. int tanpi8gx, tan3pi8gx;
  132. if (gx < 0)
  133. gx = -gx, gy = -gy;
  134. gy <<= 16;
  135. tanpi8gx = 27146 * gx;
  136. tan3pi8gx = 158218 * gx;
  137. if (gy > -tan3pi8gx && gy < -tanpi8gx) return DIRECTION_45UP;
  138. if (gy > -tanpi8gx && gy < tanpi8gx) return DIRECTION_HORIZONTAL;
  139. if (gy > tanpi8gx && gy < tan3pi8gx) return DIRECTION_45DOWN;
  140. }
  141. return DIRECTION_VERTICAL;
  142. }
  143. static void sobel(AVFilterContext *ctx, int w, int h,
  144. uint16_t *dst, int dst_linesize,
  145. const uint8_t *src, int src_linesize)
  146. {
  147. int i, j;
  148. EdgeDetectContext *edgedetect = ctx->priv;
  149. for (j = 1; j < h - 1; j++) {
  150. dst += dst_linesize;
  151. src += src_linesize;
  152. for (i = 1; i < w - 1; i++) {
  153. const int gx =
  154. -1*src[-src_linesize + i-1] + 1*src[-src_linesize + i+1]
  155. -2*src[ i-1] + 2*src[ i+1]
  156. -1*src[ src_linesize + i-1] + 1*src[ src_linesize + i+1];
  157. const int gy =
  158. -1*src[-src_linesize + i-1] + 1*src[ src_linesize + i-1]
  159. -2*src[-src_linesize + i ] + 2*src[ src_linesize + i ]
  160. -1*src[-src_linesize + i+1] + 1*src[ src_linesize + i+1];
  161. dst[i] = FFABS(gx) + FFABS(gy);
  162. edgedetect->directions[j*w + i] = get_rounded_direction(gx, gy);
  163. }
  164. }
  165. }
  166. static void non_maximum_suppression(AVFilterContext *ctx, int w, int h,
  167. uint8_t *dst, int dst_linesize,
  168. const uint16_t *src, int src_linesize)
  169. {
  170. int i, j;
  171. EdgeDetectContext *edgedetect = ctx->priv;
  172. #define COPY_MAXIMA(ay, ax, by, bx) do { \
  173. if (src[i] > src[(ay)*src_linesize + i+(ax)] && \
  174. src[i] > src[(by)*src_linesize + i+(bx)]) \
  175. dst[i] = av_clip_uint8(src[i]); \
  176. } while (0)
  177. for (j = 1; j < h - 1; j++) {
  178. dst += dst_linesize;
  179. src += src_linesize;
  180. for (i = 1; i < w - 1; i++) {
  181. switch (edgedetect->directions[j*w + i]) {
  182. case DIRECTION_45UP: COPY_MAXIMA( 1, -1, -1, 1); break;
  183. case DIRECTION_45DOWN: COPY_MAXIMA(-1, -1, 1, 1); break;
  184. case DIRECTION_HORIZONTAL: COPY_MAXIMA( 0, -1, 0, 1); break;
  185. case DIRECTION_VERTICAL: COPY_MAXIMA(-1, 0, 1, 0); break;
  186. }
  187. }
  188. }
  189. }
  190. static void double_threshold(AVFilterContext *ctx, int w, int h,
  191. uint8_t *dst, int dst_linesize,
  192. const uint8_t *src, int src_linesize)
  193. {
  194. int i, j;
  195. EdgeDetectContext *edgedetect = ctx->priv;
  196. const int low = edgedetect->low_u8;
  197. const int high = edgedetect->high_u8;
  198. for (j = 0; j < h; j++) {
  199. for (i = 0; i < w; i++) {
  200. if (src[i] > high) {
  201. dst[i] = src[i];
  202. continue;
  203. }
  204. if ((!i || i == w - 1 || !j || j == h - 1) &&
  205. src[i] > low &&
  206. (src[-src_linesize + i-1] > high ||
  207. src[-src_linesize + i ] > high ||
  208. src[-src_linesize + i+1] > high ||
  209. src[ i-1] > high ||
  210. src[ i+1] > high ||
  211. src[ src_linesize + i-1] > high ||
  212. src[ src_linesize + i ] > high ||
  213. src[ src_linesize + i+1] > high))
  214. dst[i] = src[i];
  215. else
  216. dst[i] = 0;
  217. }
  218. dst += dst_linesize;
  219. src += src_linesize;
  220. }
  221. }
  222. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
  223. {
  224. AVFilterContext *ctx = inlink->dst;
  225. EdgeDetectContext *edgedetect = ctx->priv;
  226. AVFilterLink *outlink = inlink->dst->outputs[0];
  227. uint8_t *tmpbuf = edgedetect->tmpbuf;
  228. uint16_t *gradients = edgedetect->gradients;
  229. AVFilterBufferRef *out;
  230. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  231. if (!out) {
  232. avfilter_unref_bufferp(&in);
  233. return AVERROR(ENOMEM);
  234. }
  235. avfilter_copy_buffer_ref_props(out, in);
  236. /* gaussian filter to reduce noise */
  237. gaussian_blur(ctx, inlink->w, inlink->h,
  238. tmpbuf, inlink->w,
  239. in->data[0], in->linesize[0]);
  240. /* compute the 16-bits gradients and directions for the next step */
  241. sobel(ctx, inlink->w, inlink->h,
  242. gradients, inlink->w,
  243. tmpbuf, inlink->w);
  244. /* non_maximum_suppression() will actually keep & clip what's necessary and
  245. * ignore the rest, so we need a clean output buffer */
  246. memset(tmpbuf, 0, inlink->w * inlink->h);
  247. non_maximum_suppression(ctx, inlink->w, inlink->h,
  248. tmpbuf, inlink->w,
  249. gradients, inlink->w);
  250. /* keep high values, or low values surrounded by high values */
  251. double_threshold(ctx, inlink->w, inlink->h,
  252. out->data[0], out->linesize[0],
  253. tmpbuf, inlink->w);
  254. avfilter_unref_bufferp(&in);
  255. return ff_filter_frame(outlink, out);
  256. }
  257. static av_cold void uninit(AVFilterContext *ctx)
  258. {
  259. EdgeDetectContext *edgedetect = ctx->priv;
  260. av_freep(&edgedetect->tmpbuf);
  261. av_freep(&edgedetect->gradients);
  262. av_freep(&edgedetect->directions);
  263. }
  264. static const AVFilterPad edgedetect_inputs[] = {
  265. {
  266. .name = "default",
  267. .type = AVMEDIA_TYPE_VIDEO,
  268. .config_props = config_props,
  269. .filter_frame = filter_frame,
  270. .min_perms = AV_PERM_READ,
  271. },
  272. { NULL }
  273. };
  274. static const AVFilterPad edgedetect_outputs[] = {
  275. {
  276. .name = "default",
  277. .type = AVMEDIA_TYPE_VIDEO,
  278. },
  279. { NULL }
  280. };
  281. AVFilter avfilter_vf_edgedetect = {
  282. .name = "edgedetect",
  283. .description = NULL_IF_CONFIG_SMALL("Detect and draw edge."),
  284. .priv_size = sizeof(EdgeDetectContext),
  285. .init = init,
  286. .uninit = uninit,
  287. .query_formats = query_formats,
  288. .inputs = edgedetect_inputs,
  289. .outputs = edgedetect_outputs,
  290. .priv_class = &edgedetect_class,
  291. };