image_moments.py 1.66 KB
Newer Older
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
#!/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):

    
    def __init__(self, image, image_grayscale = None):
        if image_grayscale is None:
            image_grayscale = ImageUtils.image_grayscale(image)
        
        super(self.__class__, self).__init__(image=image, image_grayscale=image)
        
        self._moments_order = [(0, 1), (1, 0), (1, 1), (0, 2), (2, 0)]
        
    
    def run(self):
        m = measure.moments(self.image_grayscale)
        
        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)]
     
        row = m[0, 1] / m[0, 0]
        col = m[1, 0] / m[0, 0]

        mu = measure.moments_central(self.image_grayscale, row, col)
        
        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)
        labels_hu = [m+n for m,n in zip(['Hu_'] * len(valores_hu),map(str,range(0,len(valores_hu))))]
        
        values = values_m + values_mu + values_hu
        labels = labels_m + labels_mu + labels_hu

        types = [Extractor.NUMERIC] * len(labels)

        return labels, types, values