forked from justadudewhohacks/opencv4nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotHist.js
41 lines (32 loc) · 1.12 KB
/
plotHist.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const cv = require('../');
const img = cv.imread('../data/Lenna.png');
// single axis for 1D hist
const getHistAxis = channel => ([
{
channel,
bins: 256,
ranges: [0, 256]
}
]);
// calc histogram for blue, green, red channel
const bHist = cv.calcHist(img, getHistAxis(0));
const gHist = cv.calcHist(img, getHistAxis(1));
const rHist = cv.calcHist(img, getHistAxis(2));
const blue = new cv.Vec(255, 0, 0);
const green = new cv.Vec(0, 255, 0);
const red = new cv.Vec(0, 0, 255);
// plot channel histograms
const plot = new cv.Mat(300, 600, cv.CV_8UC3, [255, 255, 255]);
cv.plot1DHist(bHist, plot, blue, { thickness: 2 });
cv.plot1DHist(gHist, plot, green, { thickness: 2 });
cv.plot1DHist(rHist, plot, red, { thickness: 2 });
cv.imshow('rgb image', img);
cv.imshow('rgb histogram', plot);
cv.waitKey();
const grayImg = img.bgrToGray();
const grayHist = cv.calcHist(grayImg, getHistAxis(0));
const grayHistPlot = new cv.Mat(300, 600, cv.CV_8UC3, [255, 255, 255]);
cv.plot1DHist(grayHist, grayHistPlot, new cv.Vec(0, 0, 0));
cv.imshow('grayscale image', grayImg);
cv.imshow('grayscale histogram', grayHistPlot);
cv.waitKey();