-
Notifications
You must be signed in to change notification settings - Fork 1
/
histograma.py
executable file
·63 lines (51 loc) · 1.35 KB
/
histograma.py
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
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
from imagen import ImagenArchivo
#import matplotlib.pyplot as plt
import numpy as np
import colorsys
import sys
from collections import defaultdict
def cargar(filename):
img = ImagenArchivo(filename)
print "mode: %s" % img.mode
print "size: %s" % str(img.size)
return img
def crear_histograma_grayscale(img):
ancho, alto = img.size
histo = []
for x in range(ancho):
for y in range(alto):
r, g, b = img.getpixel((x, y))
histo.append(r)
plt.hist(histo, bins=np.arange(0, 255, 1))
plt.show()
def crear_histograma_hue(img):
ancho, alto = img.size
histo = []
for x in range(ancho):
for y in range(alto):
r, g, b = img.getpixel((x, y))
if not (r == 0 and g == 0 and b == 0):
h, s, v = colorsys.rgb_to_hsv(r/255, g/255, b/255)
histo.append(h)
plt.hist(histo, bins=np.arange(0., 1., 0.001))
plt.show()
def crear_histograma_no_normalizado(img):
ancho, alto = img.size
total = ancho * alto
h = defaultdict(int)
ret = dict()
for x in range(ancho):
for y in range(alto):
r, g, b = img.getpixel((x, y))
h[r] += 1
return h
if __name__ == '__main__':
if len(sys.argv) <= 1:
print "Uso:"
print "%s imagen_entrada" % sys.argv[0]
else:
img = cargar(sys.argv[1])
crear_histograma(img)