Python中图像的交互式像素信息? [英] Interactive pixel information of an image in Python?

查看:373
本文介绍了Python中图像的交互式像素信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本:是否有用于显示图像的Python方法,该图像实时显示像素指数和强度?因此,当我将光标移动到图像上时,我会不断更新显示,例如像素[103,214] = 198 (对于灰度)或像素[103,214] =(138,24,211) for rgb?



长版:



假设我打开保存为ndarray im 的灰度图像,并以 imshow 来自matplotlib:

  im = plt.imread('image.png')
plt.imshow(im, cm.gray)

我得到的是图像,在窗框的右下角,像素索引的交互式显示。除非它们不完整,因为值不是整数: x = 134.64 y = 129.169 例如。



如果我以正确的分辨率设置显示器:

  plt.axis('equal')

x和y值仍然不是整数。



<$来自光谱包的c $ c> imshow 方法做得更好:

 导入频谱为spc 
spc.imshow(im)

然后在右下角我现在有 pixel = [103,152]



但是,没有这些方法还显示了像素值。所以我有两个问题:


  1. 来自 imshow 可以吗? > matplotlib (以及 imshow 来自 scikit-image )被强制显示正确(整数)像素索引?

  2. 是否可以扩展这些方法以显示像素值?


解决方案

有几种不同的方法可以解决这个问题。



你可以修补 ax.format_coord ,类似于。如果您指定 hover = True ,则只要将鼠标悬停在已启用的艺术家上,该框就会弹出。 (默认情况下,它仅在单击时弹出。)请注意 mpldatacursor 确实处理范围 origin kwargs到 imshow 正确。

  import numpy as np 
import matplotlib.pyplot as plt
import mpldatacursor

data = np.random.random((10,10))

fig,ax = plt.subplots()
ax.imshow(data,interpolation ='none')

mpldatacursor.datacursor(hover = True,bbox = dict(alpha = 1, fc ='w'))
plt.show()



另外,我忘了提及如何显示像素指数。在第一个例子中,它只假设 i,j = int(y),int(x)。如果您愿意,可以添加那些代替 x y



使用 mpldatacursor ,您可以使用自定义格式化程序指定它们。 i j 参数是正确的像素索引,无论范围原点



例如(注意图像的范围 i,j 显示坐标):

 将numpy导入nump 
导入matplotlib.pyplot为plt
import mpldatacursor

data = np.random.random((10,10))

fig,ax = plt.subplots()
ax.imshow (data,interpolation ='none',extent = [0,1.5 * np.pi,0,np.pi])

mpldatacursor.datacursor(hover = True,bbox = dict(alpha = 1) ,fc ='w'),
formatter ='i,j = {i},{j} \\\
z = {z:.02g}'。format)
plt.show()


Short version: is there a Python method for displaying an image which shows, in real time, the pixel indices and intensities? So that as I move the cursor over the image, I have a continually updated display such as pixel[103,214] = 198 (for grayscale) or pixel[103,214] = (138,24,211) for rgb?

Long version:

Suppose I open a grayscale image saved as an ndarray im and display it with imshow from matplotlib:

im = plt.imread('image.png')
plt.imshow(im,cm.gray)

What I get is the image, and in the bottom right of the window frame, an interactive display of the pixel indices. Except that they're not quite, as the values are not integers: x=134.64 y=129.169 for example.

If I set the display with correct resolution:

plt.axis('equal')

the x and y values are still not integers.

The imshow method from the spectral package does a better job:

import spectral as spc
spc.imshow(im)

Then in the bottom right I now have pixel=[103,152] for example.

However, none of these methods also shows the pixel values. So I have two questions:

  1. Can the imshow from matplotlib (and the imshow from scikit-image) be coerced into showing the correct (integer) pixel indices?
  2. Can any of these methods be extended to show the pixel values as well?

解决方案

There a couple of different ways to go about this.

You can monkey-patch ax.format_coord, similar to this official example. I'm going to use a slightly more "pythonic" approach here that doesn't rely on global variables. (Note that I'm assuming no extent kwarg was specified, similar to the matplotlib example. To be fully general, you need to do a touch more work.)

import numpy as np
import matplotlib.pyplot as plt

class Formatter(object):
    def __init__(self, im):
        self.im = im
    def __call__(self, x, y):
        z = self.im.get_array()[int(y), int(x)]
        return 'x={:.01f}, y={:.01f}, z={:.01f}'.format(x, y, z)

data = np.random.random((10,10))

fig, ax = plt.subplots()
im = ax.imshow(data, interpolation='none')
ax.format_coord = Formatter(im)
plt.show()

Alternatively, just to plug one of my own projects, you can use mpldatacursor for this. If you specify hover=True, the box will pop up whenever you hover over an enabled artist. (By default it only pops up when clicked.) Note that mpldatacursor does handle the extent and origin kwargs to imshow correctly.

import numpy as np
import matplotlib.pyplot as plt
import mpldatacursor

data = np.random.random((10,10))

fig, ax = plt.subplots()
ax.imshow(data, interpolation='none')

mpldatacursor.datacursor(hover=True, bbox=dict(alpha=1, fc='w'))
plt.show()

Also, I forgot to mention how to show the pixel indices. In the first example, it's just assuming that i, j = int(y), int(x). You can add those in place of x and y, if you'd prefer.

With mpldatacursor, you can specify them with a custom formatter. The i and j arguments are the correct pixel indices, regardless of the extent and origin of the image plotted.

For example (note the extent of the image vs. the i,j coordinates displayed):

import numpy as np
import matplotlib.pyplot as plt
import mpldatacursor

data = np.random.random((10,10))

fig, ax = plt.subplots()
ax.imshow(data, interpolation='none', extent=[0, 1.5*np.pi, 0, np.pi])

mpldatacursor.datacursor(hover=True, bbox=dict(alpha=1, fc='w'),
                         formatter='i, j = {i}, {j}\nz = {z:.02g}'.format)
plt.show()

这篇关于Python中图像的交互式像素信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆