【问题标题】:Using PIL (Python) to find the given value of RGB in jpeg image?使用 PIL (Python) 在 jpeg 图像中查找 RGB 的给定值?
【发布时间】:2017-09-17 02:25:48
【问题描述】:

现在我使用 PIL 读取 jpeg 图像,获取 RGB 的值。

虽然我可以组合所有的 RGB,然后找到等于给定 rgb 值的宽度和高度。

有没有更有效的方法或功能可以达到这个目的?

我的最终目标是获取这张图片上dBZ的数据,包括经纬度信息。

所以第一步,我需要在图像中获取等于给定 RGB 的坐标。

【问题讨论】:

  • 什么意思?你有一个 rgb 值,你想在图像中找到匹配的像素坐标?
  • 你的问题不清楚
  • @Dashadower 是的,这就是我的目标。

标签: python numpy python-imaging-library scikit-image


【解决方案1】:

使用 NumPy 的 argwhere 是实现您想要的直接方法。例如,您可以像这样获取具有 RGB 值[105, 171, 192] 的像素的空间坐标:

In [118]: from skimage import io

In [119]: import numpy as np

In [120]: img = io.imread('https://i.stack.imgur.com/EuHas.png')

In [121]: ref = [105, 171, 192] 

In [122]: indices = np.argwhere(np.all(img == ref, axis=-1))

In [123]: indices
Out[123]: 
array([[ 71, 577],
       [ 79, 376],
       [ 79, 386],
       [ 95, 404]], dtype=int64)

下面的sn-p可以看出上面的结果是正确的:

import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2)

ax1.imshow(img)
ax1.set_title('Original map')
ax1.set_axis_off()

ax2.imshow(img)
ax2.set_title('Pixels with RGB = ' + str(ref))
for y, x in indices:
    ax2.add_artist(plt.Circle((x, y), 8, color='r'))

【讨论】:

  • 很好,谢谢。那么有没有办法从图像中获取地球坐标呢?
  • 是的,当然。您需要对图像进行地理配准。查看this thread,了解如何将图像坐标转换为经纬度。
  • 对不起。我只是解决了坐标问题而忘记了标记。-_-
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
  • 2020-01-03
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多