image_moments.py 2.96 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 11 12 13 14 15 16 17
    Name: image_moments.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
from skimage import measure

from util.utils import ImageUtils

from extractor import Extractor

18
class RawCentralMoments(Extractor):
19
    """Calculate raw and central set of image moments."""
20
    
21
    def __init__(self):
22 23
        """Constructor.
        """
24
        self._moments_order = [(0, 1), (1, 0), (1, 1), (0, 2), (2, 0)]        
25
        
26 27
    
    def run(self, image):
28 29 30 31 32 33
        """Calculate raw and central set of image moments of order 1 and 2.
        
        Parameters
        ----------
        image : opencv image
            Image to be analyzed.
34
        
35 36 37 38 39
        Returns
        -------
        features : tuple
            Returns a tuple containing a list of labels, type and values for each feature extracted.
        """
40
        image_binary = ImageUtils.image_binary(image, bgr = True)
41
        
42
        m = measure.moments(image_binary)
43 44 45
        
        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)]
46
        
47 48 49
        row = m[0, 1] / m[0, 0]
        col = m[1, 0] / m[0, 0]

50
        mu = measure.moments_central(image_binary, row, col)
51 52 53 54
        
        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)]
        
55 56 57 58 59 60 61 62
        labels = labels_m + labels_mu
        types = [Extractor.NUMERIC] * len(labels)
        values = values_m + values_mu
        
        return labels, types, values


class HuMoments(Extractor):
63
    """Calculate Hu's set of image moments."""
64 65 66 67 68 69
    
    def __init__(self):
        pass
        
    
    def run(self, image):
70 71 72 73 74 75
        """Calculate Hu's set set of image moments.
        
        Parameters
        ----------
        image : opencv image
            Image to be analyzed.
76
        
77 78 79 80 81
        Returns
        -------
        features : tuple
            Returns a tuple containing a list of labels, type and values for each feature extracted.
        """
82
        image_binary = ImageUtils.image_binary(image, bgr = True)
83
        
84
        m = measure.moments(image_binary)
85 86 87 88
        
        row = m[0, 1] / m[0, 0]
        col = m[1, 0] / m[0, 0]

89
        mu = measure.moments_central(image_binary, row, col)
90
        
91 92 93 94
        nu = measure.moments_normalized(mu)
        hu = measure.moments_hu(nu)

        values_hu = list(hu)
95
        labels_hu = [m+n for m,n in zip(['Hu_'] * len(values_hu),map(str,range(0,len(values_hu))))]
96
        
97
        labels = labels_hu
98
        types = [Extractor.NUMERIC] * len(labels)
99
        values = values_hu
100
        
101 102
        return labels, types, values