question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Image x, y coordinate of pixel from which spectrum was displayed?

See original GitHub issue

Hello, is there a way to display the x/y coordinates of a pixel from which a spectrum was just displayed using imshow? When I double-click on a displayed image and a pixel spectrum is displayed in a new pop-up window, it would be great if the x/y coord was displayed so that I could then easily extract that spectrum from the cube using

spectrum1 = image.read_pixel(2225, 3801)

Now I have to use ENVI to get the pixel coordinate, or display the image in the normal backend using %matplotlib widget magic, with which x/y coords and band DN values are streamed when you move the mouse.

Thank you…

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pythonic2020commented, Nov 21, 2020

Here is sample code to extract spectra from cube using Jupyter Notebook and new mouse handler from above:

import numpy as np
import matplotlib as mpl

# May need to install wxpython and pyopengl
mpl.use('WXAgg') # Must enable for spectral plots and 3-d cube, and call it before importing pyplot
import matplotlib.pyplot as plt

import spectral.io.envi as envi

# Open cube as SpyFile
cube1 = envi.open('envi_cube.hdr', image=envi_cube')

from spectral.graphics.spypylab import ImageViewMouseHandler, KeyParser
from spectral import settings

class MyMouseHandler(ImageViewMouseHandler):
    def handle_event(self, event): 
        '''Callback for click event in the image display. Will write coordinates
of clicked pixel in notebook cell. Written by tboggs.''' 
        if self.show_events: 
            print(event, ', key = %s' % event.key) 
        if event.inaxes is not self.view.axes: 
             return 
        (r, c) = (int(event.ydata + 0.5), int(event.xdata + 0.5)) 
        (nrows, ncols) = self.view._image_shape 
        if r < 0 or r >= nrows or c < 0 or c >= ncols: 
            return 
        kp = KeyParser(event.key) 
        if event.button == 1: 
            if event.dblclick and kp.key is None: 
                if self.view.source is not None:  
                    print("YOU CLICKED", (r, c)) 
                    if self.view.spectrum_plot_fig_id is None: 
                        f = plt.figure() 
                        self.view.spectrum_plot_fig_id = f.number 
                    try: 
                        f = plt.figure(self.view.spectrum_plot_fig_id) 
                    except: 
                        f = plt.figure() 
                        self.view.spectrum_plot_fig_id = f.number 
                    s = f.gca() 
                    settings.plotter.plot(self.view.source[r, c], 
                                          self.view.source) 
                    s.xaxis.axes.relim() 
                    s.xaxis.axes.autoscale(True) 
                    f.canvas.draw()

from spectral.graphics.spypylab import imshow 

# Must use full image to get accurate image coordinates for spectra. Don't view a subset!

view = imshow(data=cube1, bands=(76, 83, 92), origin='upper', stretch=(0.08, 0.98),
              source=cube1, title='Cube - Bands 76-83-92/RGB', figsize=(6, 6))

handler = MyMouseHandler(view)
handler.connect()

# Use magnify tool in pop-up window to zoom to area of interest.  Then double-click in the window to display spectrum.

# Extract spectrum from pixel selected above.  Insert your own coordinates.  Creates 1-d numpy array.
spectrum1 = cube1.read_pixel(2225, 3801) 
1reaction
pythonic2020commented, Nov 20, 2020

It worked!!! YOU CLICKED line displayed in notebook!!! Perfect. Great job. Many thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pixels, Coordinates, and Colors
To create a two-dimensional image, each point in the image is assigned a color. A point in 2D can be identified by a...
Read more >
What is a pixel—ArcGIS Pro | Documentation
Multispectral images can be displayed as natural color and color infrared if the bands are available. Pixels represent a location on the ground....
Read more >
Image Analysis — CASAdocs documentation - Read the Docs
The spatial axes can alternately contain GLON/GLAT or other coordinate systems. The spectral axis of images created in CASA is always in frequency...
Read more >
Interactive pixel information of an image in Python?
So that as I move the cursor over the image, I have a continually updated display such as pixel[103,214] = 198 (for grayscale)...
Read more >
Representations of spectral coordinates in FITS
or z, shifts the “pixel” coordinates of spectral features by the same amount over the whole image. This allows relative veloc- ities between...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found