{"id":6320,"date":"2015-12-28T10:05:31","date_gmt":"2015-12-28T15:05:31","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6320"},"modified":"2017-11-17T12:50:06","modified_gmt":"2017-11-17T17:50:06","slug":"cartoonize-an-image-using-bilateral-filtering","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/12\/28\/cartoonize-an-image-using-bilateral-filtering\/","title":{"rendered":"Cartoonize an image using bilateral filtering"},"content":{"rendered":"<h1><\/h1>\r\n<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/4291457-avi-nehemiah\">Avi's<\/a> pick of the week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/12-central-diff-m\">Bilateral Filtering<\/a> by Douglas Lanman.\r\n\r\nBilateral filtering is an edge-preserving smoothing filter that can be used for a wide variety of image processing tasks such as de-noising and tone mapping, another fun application of a bilateral filter is to \"cartoonize\" an image. In his submission Douglas provides a helper function that shows how a bilateral filter can be used to\u00a0turn an image into a cartoon version of itself.\r\n<h2><strong>Contents<\/strong><\/h2>\r\n<div>\r\n<ul>\r\n\t<li><a href=\"#1\">Connect to a webcam<\/a><\/li>\r\n\t<li><a href=\"#2\">Read input image from webcam<\/a><\/li>\r\n\t<li><a href=\"#3\">Cartoonize image<\/a><\/li>\r\n\t<li><a href=\"#4\">Other edge-preserving filters<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n<h2><strong>Connect to a webcam<\/strong><a name=\"1\"><\/a><\/h2>\r\nLets start by reading in an input image from a webcam using the webcam object in MATLAB. You can view the live stream using the preview function.\r\n<pre class=\"codeinput\">clear <span class=\"string\">camera<\/span>;\r\ncamera = webcam;\r\npreview(camera)\r\n<\/pre>\r\n<a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_04.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-6321 size-full\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_04.png\" alt=\"POTW_Post_04\" width=\"656\" height=\"534\" \/><\/a>\r\n<h2><strong>Read input image from webcam<\/strong><a name=\"2\"><\/a><\/h2>\r\nYou then use the snapshot function to grab an input frame from the webcam.\r\n<pre class=\"codeinput\">inputImage = snapshot(camera);\r\nfigure;\r\nimshow(inputImage);title(<span class=\"string\">'Input Image'<\/span>);\r\n\r\n\r\n\r\n<\/pre>\r\n<h2><strong><a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_03.png\"><img decoding=\"async\" loading=\"lazy\" width=\"814\" height=\"575\" class=\"aligncenter size-full wp-image-6322\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_03.png\" alt=\"POTW_Post_03\" \/><\/a>Convert image into cartoon version of itself\u00a0<\/strong><a name=\"3\"><\/a><\/h2>\r\nNow lets \"cartoonize\" the image with a bilateral filter. Notice how edges in the image are preserved by the bilateral filter.\r\n<pre class=\"codeinput\">cartoonImage = cartoon(im2double(inputImage));\r\nfigure;\r\nsubplot(1,2,1);\r\nimshow(inputImage);\r\ntitle(<span class=\"string\">'Original Image'<\/span>);\r\nsubplot(1,2,2);\r\nimshow(cartoonImage);\r\ntitle(<span class=\"string\">'Cartoon Image'<\/span>);\r\n<\/pre>\r\n<h2><a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_05.png\"><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" class=\"aligncenter size-full wp-image-6323\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_05.png\" alt=\"POTW_Post_05\" \/><\/a><\/h2>\r\n<h2><strong>Other edge-preserving filters<\/strong><a name=\"4\"><\/a><\/h2>\r\nIf you are interested in using an edge-preserving filter I would recommend you try the new guided filter in the Image Processing Toolbox.\r\n<pre class=\"codeinput\">doc <span class=\"string\">imguidedfilter<\/span>\r\n<\/pre>\r\n<p class=\"footer\"><a href=\"https:\/\/www.mathworks.com\/products\/matlab\/\">Published with MATLAB\u00ae R2015b<\/a><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_04.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\nAvi's pick of the week is Bilateral Filtering by Douglas Lanman.\r\n\r\nBilateral filtering is an edge-preserving smoothing filter that can be used for a wide variety of image processing tasks such as... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/12\/28\/cartoonize-an-image-using-bilateral-filtering\/\">read more >><\/a><\/p>","protected":false},"author":132,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6320"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/132"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6320"}],"version-history":[{"count":11,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6320\/revisions"}],"predecessor-version":[{"id":8982,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6320\/revisions\/8982"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}