-
Notifications
You must be signed in to change notification settings - Fork 0
/
clase_03_09_momentos.py
64 lines (49 loc) · 1.72 KB
/
clase_03_09_momentos.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
"""
Universidad Adolfo Ibañez
Facultad de Ingeniería y Ciencias
TICS 585 - Reconocimiento de Patrones en imágenes
Extracción de los 7 momentos de HU
Autor:. Miguel Carrasco (04-08-2021)
rev.1.0
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
from math import copysign, log10
def cambio_escala (image, factor):
factor_times = factor
height = int(image.shape[0] * factor_times)
width = int(image.shape[1] * factor_times)
dim = (width, height)
#cambio de escala
output = cv2.resize(image, dim, interpolation=cv2.INTER_CUBIC)
return output
#******************************
# PROGRAMA PRINCIPAL *
#******************************
im=[[2, 1, 2, 3, 2, 1, 4, 0],
[4, 3, 4, 5, 1, 4, 3, 0],
[3, 4, 23, 19, 13, 1, 3, 1],
[0, 2, 18, 12, 17, 18, 4, 2],
[1, 1, 22, 45, 45, 23, 5, 2],
[0, 1, 1, 31, 21, 12, 6, 3],
[0, 2, 4, 2, 3, 5, 7, 4],
[0, 0, 5, 1, 1, 3, 3, 6]]
#transformamos los datos a uint8
image = np.array(im, dtype='uint8')
#cambiamos la escala de la imagen
scaled_img = cambio_escala (image, 10)
#extracción de los siete momentos de HU
huMoments = cv2.HuMoments(cv2.moments(image))
huMoments_Scaled = cv2.HuMoments(cv2.moments(scaled_img))
# Log scale hu moments
for i in range(0,7):
huMoments[i] = -1* copysign(1.0, huMoments[i]) * log10(np.abs(huMoments[i]))
huMoments_Scaled[i] = -1* copysign(1.0, huMoments_Scaled[i]) * log10(np.abs(huMoments_Scaled[i]))
print(f'Momento {i}\t Original:{huMoments[i].round(7)}\t Modificada:{ huMoments_Scaled[i].round(7)}')
plt.figure(dpi=100)
plt.subplot(121)
plt.imshow(image, cmap='gray')
plt.subplot(122)
plt.imshow(scaled_img, cmap='gray')
plt.show()