-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmoment_invariants.py
123 lines (103 loc) · 4.32 KB
/
moment_invariants.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# cython: language_level=3
"""
@Time : 2020/9/3 11:32
@Author : Zhang Qi
@Email : [email protected]
@File : moment_invariants.py
@Title : 不变矩
@Description :
"""
import numpy as np
import logging
def get_rank_moment(image: np.ndarray, row_moment: int,
col_moment: int) -> np.ndarray:
"""返回不变矩,阶数为row_moment + col_moment
:param image: numpy.array, 图像
:param row_moment: int, 行阶数
:param col_moment: int, 列阶数
:return: numpy.array,不变矩
"""
"""初始化"""
moment = 0.
"""计算矩"""
n_row, n_col = image.shape
for i_row in range(n_row):
for j_col in range(n_col):
moment += np.power(i_row, row_moment)\
* np.power(j_col, col_moment) * image[i_row][j_col]
return moment
def get_center_moment(image: np.ndarray, row_moment: int,
col_moment: int) -> np.ndarray:
"""计算中心矩
:param image: numpy.array, 图像
:param row_moment: int,行阶数
:param col_moment: int,列阶数
:return: numpy.array, 中心矩
"""
"""计算均值"""
'''计算1阶矩'''
rank_moment00 = get_rank_moment(image, row_moment=0, col_moment=0)
rank_moment10 = get_rank_moment(image, row_moment=1, col_moment=0)
rank_moment01 = get_rank_moment(image, row_moment=0, col_moment=1)
'''计算均值'''
mean_row = rank_moment10 / rank_moment00
mean_col = rank_moment01 / rank_moment00
"""计算不变矩"""
moment = 0.
n_row, n_col = image.shape
for i_row in range(n_row):
for j_col in range(n_col):
moment += np.power((i_row - mean_row), row_moment) * \
np.power((j_col - mean_col), col_moment) * image[i_row][j_col]
return moment
def normal_center_moment(image: np.ndarray, row_moment: int,
col_moment: int) -> np.ndarray:
"""计算归一化后的中心矩
:param image: numpy.array, 图像
:param row_moment: int,行阶数
:param col_moment: int,列阶数
:return: numpy.array, 中心矩
"""
"""中心矩"""
center_moment = get_center_moment(
image, row_moment=row_moment, col_moment=col_moment)
"""归一化的分母部分"""
tmp_center_moment = get_center_moment(image, row_moment=0, col_moment=0)
tmp_power = (row_moment + col_moment) / 2 + 1
tmp_moment = tmp_center_moment ** tmp_power
return center_moment / tmp_moment
def get_moment_invariants_seq(image: np.ndarray) -> list:
"""返回二阶和三阶不变矩组(7个)
:param image: numpy.array, 图片
:return:
"""
"""计算归一化后的中心矩"""
'''二阶'''
logging.info("正在计算二阶中心矩")
moment20 = normal_center_moment(image, row_moment=2, col_moment=0)
moment02 = normal_center_moment(image, row_moment=0, col_moment=2)
moment11 = normal_center_moment(image, row_moment=1, col_moment=1)
'''三阶'''
logging.info("正在计算三阶中心矩")
moment03 = normal_center_moment(image, 0, 3)
moment12 = normal_center_moment(image, 1, 2)
moment21 = normal_center_moment(image, 2, 1)
moment30 = normal_center_moment(image, 3, 0)
"""计算不变矩组"""
logging.info("正在计算不变矩组")
num1 = moment02 + moment20
num2 = (moment02 - moment20) ** 2 + 4 * moment11 ** 2
num3 = (moment30 - 3 * moment12) ** 2 + (3 * moment21 - moment03) ** 2
num4 = (moment30 + moment12) ** 2 + (moment21 + moment03) ** 2
num5 = (moment30 - 3 * moment12) * (moment30 + moment12) * ((moment30 + moment12) ** 2 - 3 * (moment21 + moment03) ** 2) + \
(3 * moment21 - moment03) * (moment21 + moment03) * \
(3 * (moment30 - moment12) ** 2 - (moment21 - moment03) ** 2)
num6 = (moment20 - moment02) * ((moment30 + moment12) ** 2 - (moment21 + moment03)
** 2) + 4 * moment11 * (moment30 + moment12) * (moment21 + moment03)
num7 = (3 * moment21 - moment03) * (moment30 + moment12) * ((moment30 + moment12) ** 2 - 3 * (moment21 + moment03) ** 2) + \
(3 * moment12 - moment03) * (moment21 + moment03) * \
(3 * (moment30 + moment12) ** 2 - (moment21 - moment03) ** 2)
moment_invariants_list = [num1, num2, num3, num4, num5, num6, num7]
return moment_invariants_list