image_moments.py 1.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
    Calculate raw, central and Hu's set of image moments.
    
    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

class ImageMoments(Extractor):

    
19 20
    def __init__(self):
        self._moments_order = [(0, 1), (1, 0), (1, 1), (0, 2), (2, 0)]        
21
        
22 23
    
    def run(self, image):
24
        
25
        image_grayscale = ImageUtils.image_grayscale(image, bgr = True)
26
        
27
        m = measure.moments(image_grayscale)
28 29 30
        
        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)]
31
        
32 33 34
        row = m[0, 1] / m[0, 0]
        col = m[1, 0] / m[0, 0]

35
        mu = measure.moments_central(image_grayscale, row, col)
36 37 38 39 40 41 42 43
        
        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)]
        
        nu = measure.moments_normalized(mu)
        hu = measure.moments_hu(nu)

        values_hu = list(hu)
44
        labels_hu = [m+n for m,n in zip(['Hu_'] * len(values_hu),map(str,range(0,len(values_hu))))]
45 46 47
        
        labels = labels_m + labels_mu + labels_hu
        types = [Extractor.NUMERIC] * len(labels)
48 49
        values = values_m + values_mu + values_hu
        
50 51
        return labels, types, values