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