├── test ├── lena_gray_256.png └── index.js ├── package.json ├── LICENSE ├── .gitignore ├── readme.md └── index.js /test/lena_gray_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abdelaziz18003/ndarray-imhist/master/test/lena_gray_256.png -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | const test = require('tape'); 2 | const getPixels = require('get-pixels'); 3 | const imhist = require('../index'); 4 | 5 | test('Basic tests', (t) => { 6 | t.throws( 7 | () => imhist([1, 2, 3], {plot: false}), 8 | /to be an ndarray/, 9 | 'imhist of non ndarray inputs should throw' 10 | ); 11 | t.end(); 12 | }); 13 | 14 | test('Tests on lena_gray_256.png image', (t) => { 15 | getPixels('test/lena_gray_256.png', (err, pixels) => { 16 | const [x, y] = imhist(pixels, {channel: 0, plot: false}); 17 | t.equal(x.length, 256, 'X length should be 256'); 18 | t.equal(y.length, 256, 'Y length should be 256'); 19 | t.equal(y.reduce((a, b) => a + b), 65536, 'Y sum should be equal to 256x256'); 20 | t.end(); 21 | }) 22 | }); 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ndarray-imhist", 3 | "version": "0.5.0", 4 | "description": "A package to plot images' histograms using node.js", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test/index.js | tap-spec" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/Abdelaziz18003/ndarray-imhist.git" 12 | }, 13 | "keywords": [], 14 | "author": "Abdelaziz Mokhnache", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/Abdelaziz18003/ndarray-imhist/issues" 18 | }, 19 | "homepage": "https://github.com/Abdelaziz18003/ndarray-imhist#readme", 20 | "devDependencies": { 21 | "get-pixels": "^3.3.2", 22 | "ndarray": "^1.0.18", 23 | "tap-spec": "^5.0.0", 24 | "tape": "^4.11.0" 25 | }, 26 | "dependencies": { 27 | "ndarray-scratch": "^1.2.0" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Abdelaziz Mokhnache 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # ndarray-imhist 2 | 3 | A package to plot grayscale images' histograms using node.js and Gnuplot, inspired by Matlab `imhist` command. 4 | 5 | ## Installation 6 | 7 | ```bash 8 | npm install ndarray-imhist 9 | ``` 10 | 11 | [Gnuplot](http://www.gnuplot.info/) is required for this package to work. Make sure that it is installed and added to the path correctly by typing: 12 | 13 | ```bash 14 | gnuplot --version 15 | ``` 16 | 17 | ## Usage 18 | 19 | ### Syntax 20 | 21 | ```js 22 | imhist(img [, options]); 23 | // or 24 | let [x, y] = imhist(img [, options]); 25 | ``` 26 | 27 | displays the histogram of a grayscale image `img` (should be an [ndarray](https://github.com/scijs/ndarray)) and return the historgram data as an array of two arrays containing `x` and `y` values respectively. 28 | 29 | ### Options 30 | 31 | Option | required | default | description 32 | ------- | -------- | ------- | ----------- 33 | channel | no | 0 | Select the RGB channel to get the historgram for. `0` for the red channel, `1` for the green and `2` for the blue one. If `img` is a 2D array, this option has no effect. 34 | color | no | "blue" | Set the impulses color, must be a valid Gnuplot "rgbcolor". Otherwise, plotting will fail silently 35 | plot | no | true | If true, plot the histogram using Gnuplot. Otherwise, return just the histogram data. 36 | 37 | 38 | ### Examples 39 | 40 | ```js 41 | const getPixels = require("get-pixels"); 42 | const imhist = require("ndarray-imhist"); 43 | 44 | getPixels("lena.png", function(err, pixels) { 45 | if(!err && pixels) { 46 | // show the image histogram 47 | imhist(pixels); 48 | 49 | // return the image histogram data without plotting it. 50 | let [x, y] = imhist(pixels, {plot: false}); 51 | console.log(x, y); 52 | } 53 | }) 54 | ``` 55 | 56 | Here is how the histogram will look like: 57 | 58 | ![ndarray-imhist](https://user-images.githubusercontent.com/11301627/59524131-02eb6f80-8ecb-11e9-893d-90c39f50e800.png) 59 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const { spawn } = require('child_process'); 3 | 4 | const pool = require('ndarray-scratch'); 5 | 6 | const minGrayLevel = 0; 7 | const maxGrayLevel = 255; 8 | 9 | const defaultOptions = { 10 | channel: 0, 11 | color: 'blue', 12 | plot: true 13 | } 14 | 15 | function imhist (ndarray, options) { 16 | validateInput(ndarray); 17 | options = Object.assign({}, defaultOptions, options); 18 | 19 | ndarray = ndarray.pick(null, null, options.channel); 20 | ndarray = pool.clone(ndarray); 21 | 22 | const grayLevels = range(minGrayLevel, maxGrayLevel); 23 | const frequencies = new Array((maxGrayLevel - minGrayLevel) + 1); 24 | 25 | frequencies.fill(0); 26 | 27 | for (let i = 0; i < ndarray.data.length; i++) { 28 | frequencies[ndarray.data[i]]++; 29 | } 30 | 31 | // plot the histogram using gnuplot 32 | if (options.plot) { 33 | plotData(grayLevels, frequencies, options) 34 | } 35 | return [grayLevels, frequencies]; 36 | } 37 | 38 | function range (min, max) { 39 | let array = []; 40 | for (let i = min; i <= max; i++) { 41 | array.push(i); 42 | } 43 | return array; 44 | } 45 | 46 | function isNdarray (array) { 47 | return ( 48 | array.hasOwnProperty('data') && 49 | array.hasOwnProperty('shape') && 50 | array.hasOwnProperty('stride') && 51 | array.hasOwnProperty('offset') 52 | ) 53 | } 54 | 55 | function validateInput (input) { 56 | if (!isNdarray(input)) { 57 | throw new Error('Expected input to be an ndarray'); 58 | } 59 | } 60 | 61 | function plotData (grayLevels, frequencies, {color}) { 62 | let data = ''; 63 | grayLevels.forEach(level => { 64 | data += `${level} ${frequencies[level]}\n`; 65 | }) 66 | let gnuplot = spawn('gnuplot', ['-p']); 67 | gnuplot.stdin.write(`set xrange [${minGrayLevel}:${maxGrayLevel}]\n`); 68 | gnuplot.stdin.write(`plot '-' with impulses lc rgbcolor "${color}" notitle\n`); 69 | gnuplot.stdin.write(`${data}`); 70 | gnuplot.stdin.write(`EOF`); 71 | gnuplot.stdin.end(); 72 | return gnuplot; 73 | } 74 | 75 | module.exports = imhist; --------------------------------------------------------------------------------