-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcalcHist.coffee
More file actions
58 lines (39 loc) · 1.32 KB
/
calcHist.coffee
File metadata and controls
58 lines (39 loc) · 1.32 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#Adaptation of the example found here:
#http://opencv.willowgarage.com/documentation/cpp/imgproc_histograms.html?highlight=calchist#calcHist
cv = require './opencv'
keypress = require 'keypress'
path = require 'path'
fullpath = path.resolve 'lena.jpg'
console.log 'Opening ', fullpath
src = cv.imread fullpath, 1
return console.log 'Error opening file' if src.empty
console.log src.size
cv.namedWindow "org", 0
cv.imshow "org", src
hsv = new cv.Mat
cv.cvtColor src, hsv, cv.CV_BGR2HSV
#quantize hue to 30 levels and saturation to 32 levels
hbins = 30
sbins = 32
histSize = [hbins, sbins]
hranges = [0, 180]
sranges = [0, 256]
ranges = [hranges, sranges]
channels = [0, 1]
mask = new cv.Mat
hist = new cv.Mat
cv.calcHist [hsv], channels, mask, hist, 2, histSize, ranges, true, false
minMax = cv.minMaxIdx hist
tmp = new cv.Mat
scale = 10
histImg = tmp.zeros sbins * scale, hbins * 10, cv.CV_8UC3
for h in [0..hbins - 1]
for s in [0..sbins - 1]
binVal = hist.at h, s
intensity = Math.round(binVal * 255 / minMax.maxVal)
cv.rectangle histImg, {x: h*scale, y: s*scale}, {x: (h+1)*scale - 1, y: (s+1)*scale - 1}, [intensity, intensity, intensity, intensity], -1
cv.namedWindow "H-S Histogram", 1
cv.imshow "H-S Histogram", histImg
cv.closeOnEsc()
#Important on Windows, if you have windows which display stuff.
cv.runMessageLoop()