import rastereasy
With rastereasy
, you can plot spectral bands associated with selected pixels by a simple click. You can also extract in a list of numpy arrays the associated values.
1) Open an image
name_im='./data/demo/sentinel.tif'
names = {"NIR":8,"G":3,"CO" : 1,"SWIR2":11,"B": 2,"R":4,"RE1":5,"RE2":6,"RE3":7,"WA":9,"SWIR1":10,"SWIR3":12}
image_names=rastereasy.Geoimage(name_im,names=names)
2) Plot spectra by clicking on pixels
help(image_names.plot_spectra)
Help on method plot_spectra in module rastereasy.rastereasy:
plot_spectra(
bands=None,
fig_size=(15, 5),
percentile=2,
title='',
title_im='Original image (click outside to stop)',
title_spectra='Spectra',
xlabel='Bands',
ylabel='Value'
) method of rastereasy.rastereasy.Geoimage instance
Interactive tool to explore and plot spectral values from user-selected pixels.
This method displays the image and allows the user to click on pixels to see
their spectral values across all bands plotted as a line graph. Multiple pixels
can be selected to compare different spectral signatures.
Parameters
----------
bands : list of str, optional
List of three band identifiers to use for the background image display.
If None, uses the first three bands in the image.
Default is None.
fig_size : tuple, optional
Size of the figure in inches as (width, height).
Default is (15, 5).
percentile : int, optional
Percentile value for contrast stretching of the background image.
Default is 2.
title : str, optional
Main title for the figure.
Default is ''.
title_im : str, optional
Title for the image panel.
Default is "Original image (click outside to stop)".
title_spectra : str, optional
Title for the spectral plot panel.
Default is "Spectra".
xlabel : str, optional
X-axis label for the spectral plot.
Default is "Bands".
ylabel : str, optional
Y-axis label for the spectral plot.
Default is "Value".
Returns
-------
tuple
A tuple containing:
- series : list of lists - Spectral values for each selected pixel
- pixel_i : list of int - Row coordinates of selected pixels
- pixel_j : list of int - Column coordinates of selected pixels
Examples
--------
>>> # Explore spectral signatures in the image
>>> spectra, rows, cols = image.plot_spectra()
>>> print(f"Selected {len(spectra)} pixels")
>>>
>>> # Customize the display
>>> spectra, rows, cols = image.plot_spectra(
>>> bands=["NIR", "R", "G"],
>>> title_im="Click on different vegetation types",
>>> title_spectra="Vegetation Spectral Signatures")
Notes
-----
To end pixel selection, click outside the image area or on the "Finish" button.
This tool is particularly useful for:
- Exploring spectral differences between land cover types
- Identifying spectral anomalies
- Training classification algorithms
- Building spectral libraries
Plot spectra with the three first bands in color composition
You can click on the Finish
button or outside the image to stop the visualization.
image_names.plot_spectra()
# -> This will plot an image where you can click to visualize spectre
([], [], [])
Plot spectra with selected bands for color composition
image=rastereasy.Geoimage(name_im)
image.plot_spectra(['5','1','3'],title_im='Sentinel-2 image',title_spectra='associated spectra',xlabel='given bands',ylabel='spectral values')
# -> This will plot an image where you can click to visualize spectre
([], [], [])
3) Get spectral values of some pixels selected by a simple click
To this end, you need to put in some variables the outputs
series, pixels_i,pixels_j = image.plot_spectra(['8','9','2'],title_im='Sentinel-2 image',title_spectra='associated spectra',xlabel='given bands',ylabel='spectral values')
for i in range(len(series)):
print('* pixel i = ', pixels_i[i], 'pixel j = ', pixels_j[i], 'spectral values = ',series[i])
* pixel i = 306 pixel j = 474 spectral values = [ 797 754 1070 1434 1804 2392 2635 2722 2909 2797 3286 2763]
* pixel i = 290 pixel j = 500 spectral values = [1031 1302 1636 2116 2401 2491 2607 2574 2705 2645 3465 3354]
* pixel i = 342 pixel j = 534 spectral values = [ 760 984 1322 1628 2000 2422 2605 2712 2782 2951 3151 2875]
* pixel i = 321 pixel j = 570 spectral values = [1141 1460 1794 2206 2439 2612 2667 2699 2808 2748 3481 3291]
* pixel i = 178 pixel j = 581 spectral values = [ 814 1078 1408 1806 2007 2112 2192 2164 2271 2407 3149 3045]
* pixel i = 163 pixel j = 484 spectral values = [ 608 794 1128 1614 1901 2038 2120 2082 2254 2237 3077 2907]
* pixel i = 246 pixel j = 222 spectral values = [ 802 1076 1418 1852 2136 2244 2333 2328 2467 2422 3253 3191]
* pixel i = 488 pixel j = 697 spectral values = [249 280 476 343 365 246 265 225 231 226 175 143]