for subset in ['train', 'test']:
    print("loading subset %s..." % subset)
    d = data.load(subset)

    print("computing image moment statistics for subset %s..." % subset)
    centroids = np.zeros((d.shape[0], 2))
    major_axes = np.zeros((d.shape[0], ))
    minor_axes = np.zeros((d.shape[0], ))
    angles = np.zeros((d.shape[0], ))

    for k, im in enumerate(d):
        if k % 1000 == 0:
            print("image %d of %d..." % (k + 1, d.shape[0]))

        a = data.uint_to_float(im)

        ms = skimage.measure.moments(a.astype('float64'), order=1)
        x_centroid = ms[1, 0] / ms[0, 0]
        y_centroid = ms[0, 1] / ms[0, 0]

        mc = skimage.measure.moments_central(a.astype('float64'),
                                             y_centroid,
                                             x_centroid,
                                             order=2)
        mu = mc / ms[0, 0]

        mudiff = mu[2, 0] - mu[0, 2]
        angle = 0.5 * np.arctan2(2.0 * mu[1, 1], mudiff) * (180.0 / np.pi)
        if angle < 0.0:
            angle += 180
           equivalent_diameter,euler_number,extent,filled_area,orientation,perimeter,solidity, centroid[0], centroid[1]

features = {"hu":[],"ORB":[],"tutorial":[],"haralick":[],"lbp":[],"pftas":[],
    "zernike_moments":[],"image_size":[]}

image_shapes = np.asarray([img.shape for img in data.load('train')]).astype(np.float32)
# print image_shapes.shape
# pause()

# img_shp = (64,64)
# resized_imgs = np.empty((len(images),np.prod(img_shp)),dtype="uint8")
report = [int((j+1)*images.shape[0]/100.) for j in range(100)]
# count = 0
for i,img in enumerate(images):
    img_o = img.copy()
    img = data.uint_to_float(img)

    # Hu moments
    hu = cv2.HuMoments(cv2.moments(img)).flatten()
    features["hu"].append(hu)

    # ORB
    # img_ = cv2.resize(img_o,img_shp)
    # orb = cv2.ORB(nfeatures=1,patchSize=5,nlevels=4,edgeThreshold=5)
    # kp, des = orb.detectAndCompute(img_,None)
    # if (des is None):
    #     des = np.zeros((32,))
    #     count+=1
    # else:
    #     des = des.flatten()
    # features["ORB"].append(des)
for subset in ['train', 'test']:
    print "loading subset %s..." % subset
    d = data.load(subset)

    print "computing image moment statistics for subset %s..." % subset
    centroids = np.zeros((d.shape[0], 2))
    major_axes = np.zeros((d.shape[0],))
    minor_axes = np.zeros((d.shape[0],))
    angles = np.zeros((d.shape[0],))

    for k, im in enumerate(d):
        if k % 1000 == 0:
            print "image %d of %d..." % (k + 1, d.shape[0])

        a = data.uint_to_float(im)

        ms = skimage.measure.moments(a.astype('float64'), order=1)
        x_centroid = ms[1, 0] / ms[0, 0]
        y_centroid = ms[0, 1] / ms[0, 0]

        mc = skimage.measure.moments_central(a.astype('float64'), y_centroid, x_centroid, order=2)
        mu = mc / ms[0, 0]

        mudiff = mu[2, 0] - mu[0, 2]
        angle = 0.5 * np.arctan2(2.0 * mu[1, 1], mudiff) * (180.0 / np.pi)
        if angle < 0.0:
            angle += 180

        covar = np.array([[mu[2, 0], mu[1, 1]], [mu[1, 1], mu[0, 2]]])
        eigvals, eigvecs = np.linalg.eigh(covar)