{"id":230,"date":"2016-06-10T15:26:17","date_gmt":"2016-06-10T13:26:17","guid":{"rendered":"http:\/\/zaven.co\/blog\/?p=230"},"modified":"2025-04-08T19:55:21","modified_gmt":"2025-04-08T17:55:21","slug":"opencv-advanced-android-development-drawing-histogram","status":"publish","type":"post","link":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/","title":{"rendered":"OpenCV in advanced Android development: drawing a histogram"},"content":{"rendered":"<p>In the last tutorial we\u2019ve covered <a href=\"http:\/\/zaven.co\/blog\/opencv-in-android-studio-project\/\" target=\"_blank\" rel=\"noopener noreferrer\">OpenCV and Android Studio project integration<\/a>. This time we\u2019ll try to make actual use of this setup.&nbsp;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\/HistogramActivity.java\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub page<\/a>.<!--more--><\/p>\n<h2>Extract the data from an image<\/h2>\n<p><em>An image histogram<\/em> is a chart that represents the distribution (frequency of occurrence) of pixels within an image.<\/p>\n<p>To calculate the input data, the best option is to use the <code>Imgproc.calcHist<\/code> method, provided <strong>OpenCV<\/strong>. It will help us to <strong>create the histogram from a set of arrays<\/strong> (mostly image data).<\/p>\n<div id=\"attachment_231\" style=\"width: 237px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-231\" class=\"size-full wp-image-231\" src=\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/SunHistacp.jpg\" alt=\"advanced Android development\"  ><p id=\"caption-attachment-231\" class=\"wp-caption-text\">Image histogram showing distribution of Brightness and Red (source: en.wikipedia.org)<\/p><\/div>\n<pre><code class=\"language-java\">\n\/\/ Create the required arrays and convert the bitmap (our image) \n\/\/ so that it fits the array.\nMat rgba = new Mat();\nUtils.bitmapToMat(bitmap, rgba);\n\n\/\/ Get the bitmap size.\nSize rgbaSize = rgba.size();\n<\/code><\/pre>\n<p>Now we are ready to <em>configure our histograms<\/em>:<\/p>\n<pre><code class=\"language-java\">\n\/\/ Set the amount of bars in the histogram.\nint histSize = 256;\nMatOfInt histogramSize = new MatOfInt(histSize);\n\n\/\/ Set the height of the histogram and width of the bar.\nint histogramHeight = (int) rgbaSize.height;\nint binWidth = 5;\n\n\/\/ Set the value range.\nMatOfFloat histogramRange = new MatOfFloat(0f, 256f);\n\n\/\/ Create two separate lists: one for colors and one for channels (these will be used as separate datasets).\nScalar[] colorsRgb = new Scalar[]{new Scalar(200, 0, 0, 255), new Scalar(0, 200, 0, 255), new Scalar(0, 0, 200, 255)};\nMatOfInt[] channels = new MatOfInt[]{new MatOfInt(0), new MatOfInt(1), new MatOfInt(2)};\n\n\/\/ Create an array to be saved in the histogram and a second array, on which the histogram chart will be drawn.\nMat[] histograms = new Mat[]{new Mat(), new Mat(), new Mat()};\nMat histMatBitmap = new Mat(rgbaSize, rgba.type());\n\n<\/code><\/pre>\n<h2>Separating channels<\/h2>\n<p>Now we must <strong>calculate the histogram for each channel<\/strong>, standardize the datasets and then draw it.<\/p>\n<pre><code class=\"language-java\">\nfor (int i = 0; i &lt; channels.length; i++) {\n    Imgproc.calcHist(Collections.singletonList(rgba), channels[i], new Mat(), histograms[i], histogramSize, histogramRange);\n    Core.normalize(histograms[i], histograms[i], histogramHeight, 0, Core.NORM_INF);\n    for (int j = 0; j &lt; histSize; j++) {\n        Point p1 = new Point(binWidth * (j - 1), histogramHeight - Math.round(histograms[i].get(j - 1, 0)[0]));\n        Point p2 = new Point(binWidth * j, histogramHeight - Math.round(histograms[i].get(j, 0)[0]));\n        Imgproc.line(histMatBitmap, p1, p2, colorsRgb[i], 2, 8, 0);\n    }\n}\n<\/code><\/pre>\n<p>Using the <code>Imgproc.calcHist<\/code> method, calculate the histogram for each channel. The following parameters can be used:<\/p>\n<ul>\n<li><code>List&lt;Mat&gt; images<\/code>: array photos list<\/li>\n<li><code>MatOflnt channels<\/code>: channels list<\/li>\n<li><code>Mat mask<\/code>: optional mask<\/li>\n<li><code>Mat hist<\/code>: an array in which the calculations will be saved<\/li>\n<li><code>MatOflnt histSize<\/code>: the amount of the bars for every used dimension<\/li>\n<li><code>MatOfFloat rangers<\/code>: the value range measured for all the dimensions<\/li>\n<\/ul>\n<h2>Normalize the data<\/h2>\n<p>After calculating the histogram, standardize it in such a way so his values fit into the range indicated by the given parameters according to the <code>Core.normalize<\/code> method.<\/p>\n<p>The method takes the following parameters:<\/p>\n<ul>\n<li><code>Mat src<\/code>: a source array<\/li>\n<li><code>Mat dst<\/code>: an initial array<\/li>\n<li><code>double alpha<\/code><\/li>\n<li><code>double beta<\/code><\/li>\n<li><code>int norm_type<\/code><\/li>\n<\/ul>\n<h2>Draw the chart<\/h2>\n<p>In the second for loop, we create points for each histogram bar and draw a line based on that.<\/p>\n<pre><code class=\"language-java\">\nfor (int j = 0; j &lt; histSize; j++) {\n    Point p1 = new Point(binWidth * (j - 1), histogramHeight - Math.round(histograms[i].get(j - 1, 0)[0]));\n    Point p2 = new Point(binWidth * j, histogramHeight - Math.round(histograms[i].get(j, 0)[0]));\n    Imgproc.line(histMatBitmap, p1, p2, colorsRgb[i], 2, 8, 0);\n}\n<\/code><\/pre>\n<p>At the end we display our initial bitmap and create a second one (made out of our arrays) to be displayed as well.<\/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);\n\nBitmap histBitmap = Bitmap.createBitmap(histMatBitmap.cols(), histMatBitmap.rows(), Bitmap.Config.ARGB_8888);\nUtils.matToBitmap(histMatBitmap, histBitmap);\nBitmapHelper.showBitmap(this, histBitmap, histogramView);\n<\/code><\/pre>\n<p>That\u2019s all! Let&#8217;s&nbsp;see the result here:<\/p>\n<div id=\"attachment_251\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-251\" class=\"wp-image-251\" src=\"http:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/histogram_visualistaion-730x1298.jpg\" alt=\"Android app building\"   srcset=\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/histogram_visualistaion-730x1298.jpg 730w, https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/histogram_visualistaion.jpg 1080w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><p id=\"caption-attachment-251\" class=\"wp-caption-text\"><em>Histogram visualisation<\/em><\/p><\/div>\n<p>As you can see, it\u2019s much easier to <strong>calculate and draw a histogram with OpenCV itself when building an Android app<\/strong>.<\/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\/HistogramActivity.java\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub page<\/a>.<\/h3>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>In the last tutorial we\u2019ve covered OpenCV and Android Studio project integration. This time we\u2019ll try to make actual use of this setup.&nbsp;A code sample that we\u2019ve used in a commercial Android application can be found on our GitHub page.<\/p>\n","protected":false},"author":11,"featured_media":238,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56,5],"tags":[24,22,23,12,29,7,25],"class_list":["post-230","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-histogram","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 Drawing a histogram Android | Zaven Blog<\/title>\n<meta name=\"description\" content=\"In the last tutorial we\u2019ve covered OpenCV and Android Studio project integration. This time we\u2019ll try to make actual use of this setup and drawing a histogram!\" \/>\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-drawing-histogram\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OpenCV Drawing a histogram Android | Zaven Blog\" \/>\n<meta property=\"og:description\" content=\"In the last tutorial we\u2019ve covered OpenCV and Android Studio project integration. This time we\u2019ll try to make actual use of this setup and drawing a histogram!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/\" \/>\n<meta property=\"og:site_name\" content=\"Zaven Blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-10T13:26:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-08T17:55:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/mountains-863474_1920.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1280\" \/>\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=\"4 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-drawing-histogram\/\",\"url\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/\",\"name\":\"OpenCV Drawing a histogram Android | Zaven Blog\",\"isPartOf\":{\"@id\":\"https:\/\/zaven.co\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/mountains-863474_1920.jpg\",\"datePublished\":\"2016-06-10T13:26:17+00:00\",\"dateModified\":\"2025-04-08T17:55:21+00:00\",\"author\":{\"@id\":\"https:\/\/zaven.co\/blog\/#\/schema\/person\/54027190fa6219030f9c4705fa7e0168\"},\"description\":\"In the last tutorial we\u2019ve covered OpenCV and Android Studio project integration. This time we\u2019ll try to make actual use of this setup and drawing a histogram!\",\"breadcrumb\":{\"@id\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/#primaryimage\",\"url\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/mountains-863474_1920.jpg\",\"contentUrl\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/mountains-863474_1920.jpg\",\"width\":1920,\"height\":1280,\"caption\":\"OpenCV in advanced Android development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/zaven.co\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OpenCV in advanced Android development: drawing a histogram\"}]},{\"@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 Drawing a histogram Android | Zaven Blog","description":"In the last tutorial we\u2019ve covered OpenCV and Android Studio project integration. This time we\u2019ll try to make actual use of this setup and drawing a histogram!","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-drawing-histogram\/","og_locale":"en_US","og_type":"article","og_title":"OpenCV Drawing a histogram Android | Zaven Blog","og_description":"In the last tutorial we\u2019ve covered OpenCV and Android Studio project integration. This time we\u2019ll try to make actual use of this setup and drawing a histogram!","og_url":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/","og_site_name":"Zaven Blog","article_published_time":"2016-06-10T13:26:17+00:00","article_modified_time":"2025-04-08T17:55:21+00:00","og_image":[{"width":1920,"height":1280,"url":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/mountains-863474_1920.jpg","type":"image\/jpeg"}],"author":"Slawomir Onyszko","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Slawomir Onyszko","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/","url":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/","name":"OpenCV Drawing a histogram Android | Zaven Blog","isPartOf":{"@id":"https:\/\/zaven.co\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/#primaryimage"},"image":{"@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/#primaryimage"},"thumbnailUrl":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/mountains-863474_1920.jpg","datePublished":"2016-06-10T13:26:17+00:00","dateModified":"2025-04-08T17:55:21+00:00","author":{"@id":"https:\/\/zaven.co\/blog\/#\/schema\/person\/54027190fa6219030f9c4705fa7e0168"},"description":"In the last tutorial we\u2019ve covered OpenCV and Android Studio project integration. This time we\u2019ll try to make actual use of this setup and drawing a histogram!","breadcrumb":{"@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/#primaryimage","url":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/mountains-863474_1920.jpg","contentUrl":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2016\/06\/mountains-863474_1920.jpg","width":1920,"height":1280,"caption":"OpenCV in advanced Android development"},{"@type":"BreadcrumbList","@id":"https:\/\/zaven.co\/blog\/opencv-advanced-android-development-drawing-histogram\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zaven.co\/blog\/"},{"@type":"ListItem","position":2,"name":"OpenCV in advanced Android development: drawing a histogram"}]},{"@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\/230","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=230"}],"version-history":[{"count":23,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts\/230\/revisions"}],"predecessor-version":[{"id":69942,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts\/230\/revisions\/69942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/media\/238"}],"wp:attachment":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/media?parent=230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/categories?post=230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/tags?post=230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}