image_moments.py 3.39 KB
Newer Older
1 2 3 4 5 6
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
    Calculate raw, central and Hu's set of image moments.
    
7 8
    M. K. Hu, “Visual Pattern Recognition by Moment Invariants”, IRE Trans. Info. Theory, vol. IT-8, pp. 179-187, 1962
    
9 10
    Name: image_moments.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
Diego André Sant'Ana's avatar
Diego André Sant'Ana committed
11 12

    Alter method HuMoments
13 14
"""
from skimage import measure
Diego André Sant'Ana's avatar
Diego André Sant'Ana committed
15
import cv2
16
from util.utils import ImageUtils
Diego André Sant'Ana's avatar
Diego André Sant'Ana committed
17 18
from skimage.measure import regionprops, moments, moments_central
from skimage.morphology import label
19
import numpy as np
20
from .extractor import Extractor
21

22
class RawCentralMoments(Extractor):
23
    """Calculate raw and central set of image moments."""
24
    
25
    def __init__(self):
26 27
        """Constructor.
        """
28
        self._moments_order = [(0, 1), (1, 0), (1, 1), (0, 2), (2, 0)]        
29
        
30 31
    
    def run(self, image):
32 33 34 35 36 37
        """Calculate raw and central set of image moments of order 1 and 2.
        
        Parameters
        ----------
        image : opencv image
            Image to be analyzed.
38
        
39 40 41 42 43
        Returns
        -------
        features : tuple
            Returns a tuple containing a list of labels, type and values for each feature extracted.
        """
Diego André Sant'Ana's avatar
Diego André Sant'Ana committed
44 45 46
        #raw_moments = moments(image)


47
        image_binary = ImageUtils.image_binary(image, bgr = True)
48
        
49
        m = measure.moments(image_binary)
50 51 52
        
        values_m = [m[p, q] for (p, q) in self._moments_order]
        labels_m = [M+str(p)+str(q) for M,(p,q) in zip(['M_'] * len(self._moments_order), self._moments_order)]
53
        
54 55
        row = m[0, 1] / m[0, 0]
        col = m[1, 0] / m[0, 0]
56 57
	
        mu = measure.moments_central(image_binary, center=(row, col), order=3)
58 59 60
        
        values_mu = [mu[p, q] for (p, q) in self._moments_order]
        labels_mu = [M+str(p)+str(q) for M,(p,q) in zip(['Mu_'] * len(self._moments_order), self._moments_order)]
61
        #print(values_mu)
62 63 64 65 66 67 68 69
        labels = labels_m + labels_mu
        types = [Extractor.NUMERIC] * len(labels)
        values = values_m + values_mu
        
        return labels, types, values


class HuMoments(Extractor):
70
    """Calculate Hu's set of image moments."""
71 72 73 74 75 76
    
    def __init__(self):
        pass
        
    
    def run(self, image):
77 78 79 80 81 82
        """Calculate Hu's set set of image moments.
        
        Parameters
        ----------
        image : opencv image
            Image to be analyzed.
83
        
84 85 86 87 88
        Returns
        -------
        features : tuple
            Returns a tuple containing a list of labels, type and values for each feature extracted.
        """
Diego André Sant'Ana's avatar
Diego André Sant'Ana committed
89 90

        """image_binary = ImageUtils.image_binary(image, bgr = True)
91
        
92
        m = measure.moments(image_binary)
93 94 95 96
        
        row = m[0, 1] / m[0, 0]
        col = m[1, 0] / m[0, 0]

97
        mu = measure.moments_central(image_binary, row, col)
98
        
99 100 101
        nu = measure.moments_normalized(mu)
        hu = measure.moments_hu(nu)

Diego André Sant'Ana's avatar
Diego André Sant'Ana committed
102 103 104 105 106
        values_hu = list(hu)"""

        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        values_hu= cv2.HuMoments(cv2.moments(image)).flatten()
107
        values_hu= np.nan_to_num(values_hu)
108

109
        values_hu = list(values_hu)
110
        labels_hu = [m+n for m,n in zip(['Hu_'] * len(values_hu),map(str,range(0,len(values_hu))))]
111
        
112
        labels = labels_hu
113
        types = [Extractor.NUMERIC] * len(labels)
114
        values = values_hu
Diego André Sant'Ana's avatar
Diego André Sant'Ana committed
115 116 117 118




119 120
        return labels, types, values