python - 如何找到numpy数组的中心点?
问题描述
我正在使用 PIL 和 numpy 检测绿色。由于代码即使在非绿色区域也能找到大量绿色像素。因此,我找到了与我指定的值最接近的值,我得到了一个提供 (1512L, 3L) 矩阵的 3 个匹配点。代码如下。结果,我想在原始图像上绘制这个矩阵来查看位置。我怎样才能做到这一点 ?示例图像也在下面。
import numpy as np
from PIL import Image
# Open image and make RGB and HSV versions
RGBim = Image.open("AdjustedNewMaze3.jpg").convert('RGB')
HSVim = RGBim.convert('HSV')
# Make numpy versions
RGBna = np.array(RGBim)
HSVna = np.array(HSVim)
# Extract Hue
H = HSVna[:,:,0]
# Find all green pixels, i.e. where 100 < Hue < 140
lo,hi = 100,140
# Rescale to 0-255, rather than 0-360 because we are using uint8
lo = int((lo * 255) / 360)
hi = int((hi * 255) / 360)
green = np.where((H>lo) & (H<hi))
# Make all green pixels black in original image
RGBna[green] = [0,0,0]
def find_nearest(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return array[idx]
value = 120
green = find_nearest(RGBna, value)
average = np.average(green)
print(green)
count = green[0].size
print("Pixels matched: {}".format(count))
Image.fromarray(green).save('resultgreen.png')
解决方案
推荐阅读
- google-apps-script - 如何轻松发布插件以避免复制电子表格的重新授权请求?
- java - 当单元测试覆盖率低于多模块项目中的预配置限制时,maven 构建失败
- c# - 从 Windows 商店安装的 Office 365 中的 C# Excel-Dna 插件中的文件访问
- background-color - Vuetify:如何在 v-select 组件中指定所选项目的背景颜色
- node.js - 92% 在项目上运行 ng serve 时挂断
- wso2 - JWT Enrichment with external rest service
- java - Java8流:流中的通用记录器方法
- python - 创建具有相同尺寸的交集 DataFrame
- python - 根据已安装的模块编写条件语句的 Pythonic 方式是什么?
- javascript - 在 D3 Observable 中禁用缩放但保留平移