spectralpython / spectral

Python module for hyperspectral image processing
MIT License
573 stars 139 forks source link

'classifiers.pyc' line 202 error = "object has no attribute 'reshape' " #50

Closed fdovila closed 8 years ago

fdovila commented 8 years ago

image.reshape in python 2.7 error = "object has no attribute 'reshape' "

Hello Everyone,

Great library BTW. Thanks to Thomas Boggs!

I convert my spectral data from matlab using enviwrite

info = enviinfo(myXYL_M);
enviwrite(myXYL_M,info,'my_hypercube.dat');

I read it into python (2.7, I use Enthought Canopy_64bit) with

import numpy as np
from spectral import *
import spectral.io.envi as envi

# # tried with this to solve issue, didn't work
# import matplotlib
# from numpy import *
# from pylab import *
# from PIL import Image 

# IMPORT HYPERCUBE
img = envi.open('my_hypercube.dat.hdr')

# load classes from file
myclasses = np.loadtxt('myclasses.txt', delimiter=",") #seeding classes

# SHOW CLASSES IN OVERLAY WITH IMG
# view = imshow(img, (1400, 700, 450), classes=myclasses) #nice for 1920 WLs
view = imshow(img, (24, 12, 7), classes=myclasses)  #nice for 33 WLs
view.set_display_mode('overlay')
view.class_alpha = 0.5

This is my issue:

Whenever I try to apply a classifier, I run into this error:

clmap = gmlc.classify_image(img)
Processing...  0.0%---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-19-52f9d70ae993> in <module>()
----> 1 clmap = gmlc.classify_image(img)

/python2.7/site-packages/spectral/algorithms/classifiers.pyc in classify_image(self, image)
    200         status.display_percentage('Processing...')
    201         shape = image.shape
--> 202         image = image.reshape(-1, shape[-1])
    203         scores = np.empty((image.shape[0], len(self.classes)), np.float64)
    204         delta = np.empty_like(image, dtype=np.float64)

AttributeError: 'BsqFile' object has no attribute 'reshape' 

Again, after trying Fisher Linear Discriminant:

fld = linear_discriminant(classes)
len(fld.eigenvectors)
Out[22]: 33

img_fld = fld.transform(img)

v = imshow(img_fld[:, :, :3])

classes.transform(fld.transform)

gmlc = GaussianClassifier(classes)
Setting min samples to 4

clmap = gmlc.classify_image(img_fld)
Processing...  0.0%---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-27-77f389619efe> in <module>()
----> 1 clmap = gmlc.classify_image(img_fld)

python2.7/site-packages/spectral/algorithms/classifiers.pyc in classify_image(self, image)
    200         status.display_percentage('Processing...')
    201         shape = image.shape
--> 202         image = image.reshape(-1, shape[-1])
    203         scores = np.empty((image.shape[0], len(self.classes)), np.float64)
    204         delta = np.empty_like(image, dtype=np.float64)

AttributeError: 'TransformedImage' object has no attribute 'reshape' 
tboggs commented 8 years ago

Hello,

Your timing is great because that bug was just fixed in issue #49 and the fix is included in SPy version 0.18, which is available here and on PyPI. Note that classification will be much faster if you load the data into a numpy array (i.e., using img.load()) but you may not want to if memory is an issue.

Since this is a duplicate, I'm going to close this issue but if - for some reason - it occurs with 0.18, feel free to re-open it.

fdovila commented 8 years ago

Thanks! It works. Sorry for the duplicate.