This code is for classifying hyperspectral images that were generated using the IMEC VNIR SNAPSCAN camera. The code may be used for other hyperspectral datasets, however to use the data from Spectral Angle Mapper supervised classification, the images will need to be processed in other software if HSI Studio from IMEC is not available. The code is split into four separate scripts, which need to be run one after the other, to save computer memory when using larger number of sample data.
This code will generate the results under a new folder 'Data_classification_results'. These will contains:
(1) The K-Means classified data as a classified image and the labelled pixel matrix
(2) The data pre-treatments comparing the selection spectra, selected bands, and the selected wavelengths plotted over the second derivative of the root spectra
(3) The generation of the classification model, with the model training confusion matrix, and classification report
(4) The model predicted images, labelled pixel matrices, confusion matrics, accuracy reports, comparison of the predicted spectra, and PLS-DA plots, and estimated biomass
Two images in Data_files are provided as an example of how the code works. The raw image data is in ENVI format, containing a header file (.hdr) and raw data file (.raw). Both are needed to extract the raw data. The classified image data from SAM in HSI Studio (IMEC) contains the class image (.png) and the spectra for each class (.csv).
pip install -r https://github.com/corinef/Automated-root-classification/blob/main/requirements.txt
- Load data files
- Run K-Means
- Save classified images and spectral output
- Load spectral output from SAM and K-Means
- Plot original spectra
- Run SG smoothing
- Take the average of all root spectra
- Find peaks
- Save selected bands to .csv
- Load data files
- Load classified images
- Load selected bands
- Crop datacubes to a 300x300 pixel region and extract the pixel labels from the classification method
- Convert the datacube and pixel labels to a dataframe and reduce the bands (columns) to the selected bands
- Merge all datasets to one dataframe
- Run the classification model on the dataframe
Results of SAM (left) and K-Means (right) classification
Comparison of classification method spectra
Selected wavelengths from the second derivative of the root spectra
Cropped region of datacube (left), SAM classified image (middle), K-Means classified image (right)
Random Forest (RF) model confusion matrix, SAM (left), K-Means (right)
RF predicted image from SAM (left), K-Means (right)
Accuracy reports
Comparison of predicted spectra
Estimated biomass
- Spectral Python (SPy)
- website: https://www.spectralpython.net/
- citation: Boggs, T., March, D., kormang, McGibbney, L. J., Magimel, F., Mason, G., Banman, K., Jouni, M., Kumar, R., The Gitter Badger, Aarnio, T., Wang, W., & kidpixo. (2022)
- license: "The MIT License (MIT)"
- Pandas:
- website:https://pandas.pydata.org/
- citation: The Pandas Development Team. (2022). pandas-dev/pandas: Pandas (1.4.2).
- license: "Pandas is distributed under the modified (3-clause) BSD License"
- matplotlib:
- website: https://matplotlib.org/
- citation: J. D. Hunter, "Matplotlib: A 2D Graphics Environment", Computing in Science & Engineering, vol. 9, no. 3, pp. 90-95, 2007.
- license: "Matplotlib only uses BSD compatible code, and its license is based on the PSF license."
- seaborn:
- website: https://seaborn.pydata.org/
- citation:
- license: "The MIT License (MIT)"
- SciPy :
- website:https://scipy.org/
- citation: Virtanen, P., Gommers, R., Oliphant, T. E., Haberland, M., Reddy, T., Cournapeau, D., Burovski, E., Peterson, P., Weckesser, W., Bright, J., van der Walt, S. J., Brett, M., Wilson, J., Millman, K. J., Mayorov, N., Nelson, A. R. J., Jones, E., Kern, R., Larson, E., . . . SciPy, C. (2020). Author Correction: SciPy 1.0: fundamental algorithms for scientific computing in Python. Nature Methods, 17(3), 352-352.
- license: "SciPy is distributed under the modified (3-clause) BSD license."
- Pillow (PIL Fork):
- website:https://pypi.org/project/pillow/
- citation: Clark, A. (2015). Pillow (PIL Fork) Documentation. (9.0.1). readthedocs.
- license: "Pillow is licensed under the open source HPND License"
- Scikit-learn:
- website:https://scikit-learn.org/stable/index.html
- citation: Pedregosa, F., Varoquaux, G., Gramfort, A., Michel, V., Thirion, B., Grisel, O., Blondel, M., Prettenhofer, P., Weiss, R., Dubourg, V., Vanderplas, J., Passos, A., Cournapeau, D., Brucher, M., Perrot, M., & Duchesnay, É. (2011). Scikit-learn: Machine Learning in Python. Journal of Machine Learning Research, 12, 2825–2830.
- license: "Scikit-learn is distributed under the modified (3-clause) BSD license."
- This project is licensed under the Apache 2.0 License.