-
Notifications
You must be signed in to change notification settings - Fork 0
/
Activity_3_1_histogram.py
67 lines (53 loc) · 1.39 KB
/
Activity_3_1_histogram.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
64
65
66
67
"""
@author: romy bompart
@title: Fourth Activity - Histogram
"""
import skimage
from skimage.color import rgb2gray
from skimage import data
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.size'] = 18
import numpy as np
def Invert(image):
grayim = rgb2gray(image)
a, b = np.shape(grayim)
inverted = np.empty([a, b])
for k in range(a):
for i in range (b):
inverted[k,i] = 255 - grayim[k,i]
return inverted
def binarization(image, middle):
a, b = np.shape(image)
binarized = np.empty([a, b])
for k in range(a):
for i in range (b):
if (image[k,i]>=middle):
binarized[k,i] = 1
else:
binarized[k,i]=0
return binarized
def histogram(image):
maxi = image.max()
bins = np.zeros([255])
a, b = np.shape(image)
for k in range(a):
for i in range (b):
bins[int(image[k,i]*255)] = 1 + bins[int(image[k,i]*255)]
return bins
image = data.chelsea()
plt.figure()
plt.imshow(image)
plt.show()
invertedimage = Invert(image)
plt.figure()
plt.imshow(invertedimage, cmap="gray")
plt.show()
value =0.5
binimage = binarization( rgb2gray(image),value)
plt.figure()
plt.imshow(binimage, cmap="gray")
plt.show()
his = histogram(rgb2gray(image))
plt.bar(np.arange(len(his)),his,align='center',alpha=1)
plt.show()