image_moments.py 3.46 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
import math
22

23
class RawCentralMoments(Extractor):
24
    """Calculate raw and central set of image moments."""
25
    
26
    def __init__(self):
27 28
        """Constructor.
        """
29
        self._moments_order = [(0, 1), (1, 0), (1, 1), (0, 2), (2, 0)]        
30
        
31 32
    
    def run(self, image):
33
        """Calculate raw and central set of image moments of order 1 and 2.
34
        	
35 36 37
        Parameters
        ----------
        image : opencv image
38 39
        image to be analyzed.
		
40 41 42
        Returns
        -------
        features : tuple
43
        Returns a tuple containing a list of labels, type and values for each feature extracted.
44
        """
Diego André Sant'Ana's avatar
Diego André Sant'Ana committed
45
        #raw_moments = moments(image)
46
        	
47
        image_binary = ImageUtils.image_binary(image, bgr = True)
48
        
49
        m = measure.moments(image_binary)
50 51
        m=np.nan_to_num(m)
        	
52
        values_m = [m[p, q] for (p, q) in self._moments_order]
53
		
54 55 56
        labels_m = [M+str(p)+str(q) for M,(p,q) in zip(['M_'] * len(self._moments_order), self._moments_order)]
        row = m[0, 1] / m[0, 0]
        col = m[1, 0] / m[0, 0]
57
        	
58
        mu = measure.moments_central(image_binary, center=(row, col), order=3)
59 60
        mu=np.nan_to_num(mu)
        	
61
        values_mu = [mu[p, q] for (p, q) in self._moments_order]
62 63
        			
        	
64
        labels_mu = [M+str(p)+str(q) for M,(p,q) in zip(['Mu_'] * len(self._moments_order), self._moments_order)]
65
        	
66 67 68
        labels = labels_m + labels_mu
        types = [Extractor.NUMERIC] * len(labels)
        values = values_m + values_mu
69
        	
70 71 72 73
        return labels, types, values


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

94
        m = measure.moments(image_binary)
95

96 97 98
        row = m[0, 1] / m[0, 0]
        col = m[1, 0] / m[0, 0]

99
        mu = measure.moments_central(image_binary, row, col)
100
        
101 102
        nu = measure.moments_normalized(mu)
        hu = measure.moments_hu(nu)
103
        
Diego André Sant'Ana's avatar
Diego André Sant'Ana committed
104 105 106 107
        values_hu = list(hu)"""
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

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




120 121
        return labels, types, values