Moments
Reference: M. K. Hu, "Visual Pattern Recognition by Moment Invariants", IRE Trans. Info. Theory, vol. IT-8, pp.179–187, 1962.
Image moments are a set of particular weighted averages of the intensity values of the pixels of the image. These values can be used to calculate some properties of the image, such as area or centroid value. The nth-order moment can be calculated by the following equation:
()
Although many improvements and modifications on image moments were proposed throughout the years, such as the Zernike Moments, TRIOSlib implementation is based on Hu moments, without any further modification or improvement.
Moments is a feature extractor implemented on TRIOSlib. It receives a parameter order
that indicates the
maximum order to be calculated. The feature extractor, then, calculates the nth-order moments for every
n that is less or equal than the given parameter. If no parameter is given, the feature extractor defaults
to order = 2
.
# file docs/examples/methods/moments.py
from trios.classifiers import SKClassifier
from sklearn.tree import DecisionTreeClassifier
from trios.contrib.features.moments import MomentsExtractor
import trios
import numpy as np
import trios.shortcuts.persistence as p
drive_location = 'datasets/'
training = trios.Imageset([
('%s/training/images/%2d_training.tif'%(drive_location, i),
'%s/training/1st_manual/%2d_manual1.gif'%(drive_location, i),
'%s/training/mask/%2d_training_mask.gif'%(drive_location, i))
for i in range(21, 41)])
testset = trios.Imageset([
('%s/test/images/%02d_test.tif'%(drive_location, i),
'%s/test/1st_manual/%02d_manual1.gif'%(drive_location, i),
'%s/test/mask/%02d_test_mask.gif'%(drive_location, i))
for i in range(1, 21)])
if __name__ == '__main__':
win = np.ones((9,9), np.uint8)
moments = MomentsExtractor(win, order=4)
op = trios.WOperator(win, SKClassifier(DecisionTreeClassifier()), moments)
print('Training')
op.train(training)
print('Evaluating')
print('Accuracy:', 1 - op.eval(testset))
Accuracy: 0.7638990221330619