{"id":281,"date":"2016-06-24T14:05:48","date_gmt":"2016-06-24T12:05:48","guid":{"rendered":"https:\/\/zaven.co\/blog\/?p=281"},"modified":"2025-04-08T19:55:20","modified_gmt":"2025-04-08T17:55:20","slug":"opencv-advanced-android-development-edge-detection","status":"publish","type":"post","link":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/","title":{"rendered":"OpenCV in advanced Android development: Edge detection"},"content":{"rendered":"<p>In the last Android development tutorial we\u2019ve cover <a href=\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-light-source-detection\/\" target=\"_blank\" rel=\"noopener noreferrer\">light source detection using OpenCV<\/a>. This time we\u2019ll cover <strong>edge detection<\/strong>, which allows us to distinguish objects from background. This task is usually the starting point to more advanced content analysis (e.g. determining object features, quantities etc.).<br \/>\n<!--more--><\/p>\n<h2>Edge detection OpenCV<\/h2>\n<p>Again, OpenCV makes it easier by providing the <code>Imgproc.Canny()<\/code> method, which does all the work for us. The method itself, <em>nomen omen<\/em>, uses Canny\u2019s algorithm to perform this task.<\/p>\n<pre><code class=\"language-java\">\nprivate void detectEdges(Bitmap bitmap) {\n\tMat rgba = new Mat();\n\tUtils.bitmapToMat(bitmap, rgba);\n\n\tMat edges = new Mat(rgba.size(), CvType.CV_8UC1);\n\tImgproc.cvtColor(rgba, edges, Imgproc.COLOR_RGB2GRAY, 4);\n\tImgproc.Canny(edges, edges, 80, 100);\n\n\t\/\/ Don't do that at home or work it's for visualization purpose.\n\tBitmapHelper.showBitmap(this, bitmap, imageView);\n\tBitmap resultBitmap = Bitmap.createBitmap(edges.cols(), edges.rows(), Bitmap.Config.ARGB_8888);\n\tUtils.matToBitmap(edges, resultBitmap);\n\tBitmapHelper.showBitmap(this, resultBitmap, detectEdgesImageView);\n}\n\n<\/code><\/pre>\n<p>The process is pretty straight-forward:<\/p>\n<ol>\n<li>Read the bitmap<\/li>\n<li>Covert to greyscale<\/li>\n<li>Apply Canny\u2019s algorithm<\/li>\n<\/ol>\n<p>As always, we start by create an array from the initial bitmap.<\/p>\n<pre><code class=\"language-java\">\nMat rgba = new Mat();\nUtils.bitmapToMat(bitmap, rgba);\n<\/code><\/pre>\n<p>Next make an array, in which all the calculation results will be saved, and convert the picture to greyscale. And at the end use the <code>Imgproc.Canny()<\/code> method to <strong>detect edges<\/strong>.<\/p>\n<pre><code class=\"language-java\">\nMat edges = new Mat(rgba.size(), CvType.CV_8UC1);\nImgproc.cvtColor(rgba, edges, Imgproc.COLOR_RGB2GRAY, 4);\nImgproc.Canny(edges, edges, 80, 100);\n<\/code><\/pre>\n<p>The <code>Imgproc.Canny()<\/code> method takes the following parameters:<\/p>\n<ul>\n<li><code>Mat image <\/code>&#8211; 8-bit input image<\/li>\n<li><code>Mat edges <\/code>&#8211; <em>output edge map<\/em>; single channels 8-bit image, which has the same size as image<\/li>\n<li><code>double treshold1<\/code>&#8211; first threshold for the hysteresis procedure<\/li>\n<li><code>dobule treshold2<\/code>&#8211; second threshold for the hysteresis procedure<\/li>\n<\/ul>\n<p>Now we can display two pictures: the original one and the other that is the result of our <code>Imgproc.Canny()<\/code> method.<\/p>\n<pre><code class=\"language-java\">\n\/\/ Don't do that at home or work it's for visualization purpose.\nBitmapHelper.showBitmap(this, bitmap, imageView);\nBitmap resultBitmap = Bitmap.createBitmap(edges.cols(), edges.rows(), Bitmap.Config.ARGB_8888);\nUtils.matToBitmap(edges, resultBitmap);\nBitmapHelper.showBitmap(this, resultBitmap, detectEdgesImageView);\n<\/code><\/pre>\n<p>And that is it!<\/p>\n<div id=\"attachment_282\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-282\" class=\"wp-image-282\" src=\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/Screenshot_2016-06-24-10-41-21-730x1298.png\" alt=\"OpenCV in advanced Android development: Edge detection\"   srcset=\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/Screenshot_2016-06-24-10-41-21-730x1298.png 730w, https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/Screenshot_2016-06-24-10-41-21.png 1080w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><p id=\"caption-attachment-282\" class=\"wp-caption-text\"><strong>OpenCV: Edge detection<\/strong><\/p><\/div>\n<p>As you can see, <strong>OpenCV can deliver great effects with minimum effort when building your mobile app<\/strong>. It\u2019s a great and useful library, which provides loads of solutions out-of-the-box.<\/p>\n<hr>\n<h3>A code sample that we\u2019ve used in a commercial Android application can be found on our <a href=\"https:\/\/github.com\/zavenco\/DigitalImageProcessing\/blob\/master\/app\/src\/main\/java\/co\/zaven\/digitalimageprocessing\/activities\/DetectEdgesActivity.java\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub page<\/a>.<\/h3>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>In the last Android development tutorial we\u2019ve cover light source detection using OpenCV. This time we\u2019ll cover edge detection, which allows us to distinguish objects from background. This task is usually the starting point to more advanced content analysis (e.g. determining object features, quantities etc.).<\/p>\n","protected":false},"author":11,"featured_media":283,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56,5],"tags":[24,22,23,12,32,7,25],"class_list":["post-281","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-development","category-tutorials","tag-advanced-android-development","tag-android-app-building","tag-android-application","tag-app-development","tag-edge-detection","tag-opencv","tag-opencv-in-android-studio-project"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>OpenCV Edge detection Android | Zaven Blog<\/title>\n<meta name=\"description\" content=\"In the last OpenCV Android development tutorial we\u2019ve cover light source detection using OpenCV. This time we\u2019ll cover edge detection. Check it out!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OpenCV Edge detection Android | Zaven Blog\" \/>\n<meta property=\"og:description\" content=\"In the last OpenCV Android development tutorial we\u2019ve cover light source detection using OpenCV. This time we\u2019ll cover edge detection. Check it out!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/\" \/>\n<meta property=\"og:site_name\" content=\"Zaven Blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-24T12:05:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-08T17:55:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/edge_detect.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1265\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Slawomir Onyszko\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Slawomir Onyszko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/\",\"url\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/\",\"name\":\"OpenCV Edge detection Android | Zaven Blog\",\"isPartOf\":{\"@id\":\"https:\/\/zaven.co\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/edge_detect.jpg\",\"datePublished\":\"2016-06-24T12:05:48+00:00\",\"dateModified\":\"2025-04-08T17:55:20+00:00\",\"author\":{\"@id\":\"https:\/\/zaven.co\/blog\/#\/schema\/person\/54027190fa6219030f9c4705fa7e0168\"},\"description\":\"In the last OpenCV Android development tutorial we\u2019ve cover light source detection using OpenCV. This time we\u2019ll cover edge detection. Check it out!\",\"breadcrumb\":{\"@id\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/#primaryimage\",\"url\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/edge_detect.jpg\",\"contentUrl\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/edge_detect.jpg\",\"width\":1920,\"height\":1265,\"caption\":\"advanced Android development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/zaven.co\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OpenCV in advanced Android development: Edge detection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/zaven.co\/blog\/#website\",\"url\":\"https:\/\/zaven.co\/blog\/\",\"name\":\"Zaven Blog\",\"description\":\"Software development blog. Generative AI, web &amp; mobile applications.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/zaven.co\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/zaven.co\/blog\/#\/schema\/person\/54027190fa6219030f9c4705fa7e0168\",\"name\":\"Slawomir Onyszko\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/zaven.co\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4dc8884e2167c9cc97596c4b71c7c250?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4dc8884e2167c9cc97596c4b71c7c250?s=96&d=mm&r=g\",\"caption\":\"Slawomir Onyszko\"},\"description\":\"S\u0142awek is our mobile developer who mostly takes care of creating Android applications. He is constantly enhancing his skills of advanced Android app development and he wants to share this knowledge via Zaven\u2019s blog. In his spare time S\u0142awek enjoys watching a good movie at the cinema.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/mronyszko\"],\"url\":\"https:\/\/zaven.co\/blog\/author\/slawekzaven-pl\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"OpenCV Edge detection Android | Zaven Blog","description":"In the last OpenCV Android development tutorial we\u2019ve cover light source detection using OpenCV. This time we\u2019ll cover edge detection. Check it out!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/","og_locale":"en_US","og_type":"article","og_title":"OpenCV Edge detection Android | Zaven Blog","og_description":"In the last OpenCV Android development tutorial we\u2019ve cover light source detection using OpenCV. This time we\u2019ll cover edge detection. Check it out!","og_url":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/","og_site_name":"Zaven Blog","article_published_time":"2016-06-24T12:05:48+00:00","article_modified_time":"2025-04-08T17:55:20+00:00","og_image":[{"width":1920,"height":1265,"url":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/edge_detect.jpg","type":"image\/jpeg"}],"author":"Slawomir Onyszko","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Slawomir Onyszko","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/","url":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/","name":"OpenCV Edge detection Android | Zaven Blog","isPartOf":{"@id":"https:\/\/zaven.co\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/#primaryimage"},"image":{"@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/#primaryimage"},"thumbnailUrl":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/edge_detect.jpg","datePublished":"2016-06-24T12:05:48+00:00","dateModified":"2025-04-08T17:55:20+00:00","author":{"@id":"https:\/\/zaven.co\/blog\/#\/schema\/person\/54027190fa6219030f9c4705fa7e0168"},"description":"In the last OpenCV Android development tutorial we\u2019ve cover light source detection using OpenCV. This time we\u2019ll cover edge detection. Check it out!","breadcrumb":{"@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/#primaryimage","url":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/edge_detect.jpg","contentUrl":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/edge_detect.jpg","width":1920,"height":1265,"caption":"advanced Android development"},{"@type":"BreadcrumbList","@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-edge-detection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zaven.co\/blog\/"},{"@type":"ListItem","position":2,"name":"OpenCV in advanced Android development: Edge detection"}]},{"@type":"WebSite","@id":"https:\/\/zaven.co\/blog\/#website","url":"https:\/\/zaven.co\/blog\/","name":"Zaven Blog","description":"Software development blog. Generative AI, web &amp; mobile applications.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zaven.co\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zaven.co\/blog\/#\/schema\/person\/54027190fa6219030f9c4705fa7e0168","name":"Slawomir Onyszko","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/zaven.co\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4dc8884e2167c9cc97596c4b71c7c250?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4dc8884e2167c9cc97596c4b71c7c250?s=96&d=mm&r=g","caption":"Slawomir Onyszko"},"description":"S\u0142awek is our mobile developer who mostly takes care of creating Android applications. He is constantly enhancing his skills of advanced Android app development and he wants to share this knowledge via Zaven\u2019s blog. In his spare time S\u0142awek enjoys watching a good movie at the cinema.","sameAs":["https:\/\/www.linkedin.com\/in\/mronyszko"],"url":"https:\/\/zaven.co\/blog\/author\/slawekzaven-pl\/"}]}},"_links":{"self":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts\/281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/comments?post=281"}],"version-history":[{"count":16,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts\/281\/revisions"}],"predecessor-version":[{"id":69941,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts\/281\/revisions\/69941"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/media\/283"}],"wp:attachment":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/media?parent=281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/categories?post=281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/tags?post=281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}