栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

2像素之间的距离

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2像素之间的距离

让我们从测试图像开始。它是400x300像素的灰度(192),具有:

  • 在20,10处有一个红色的3x3正方形,
  • 蓝色的3x3正方形,位于300,200

现在执行以下操作:

import numpy as npimport PILimport math# Load image and ensure RGB - just in case palettisedim=Image.open("a.png").convert("RGB")# Make numpy array from imagenpimage=np.array(im)# Describe what a single red pixel looks likered=np.array([255,0,0],dtype=np.uint8)# Find [x,y] coordinates of all red pixelsreds=np.where(np.all((npimage==red),axis=-1))

这给出:

(array([10, 10, 10, 11, 11, 11, 12, 12, 12]), array([20, 21, 22, 20, 21, 22, 20, 21, 22]))

现在让我们做蓝色像素:

# Describe what a single blue pixel looks likeblue=np.array([0,0,255],dtype=np.uint8)# Find [x,y] coordinates of all blue pixelsblues=np.where(np.all((npimage==blue),axis=-1))

这给出:

(array([200, 200, 200, 201, 201, 201, 202, 202, 202]), array([300, 301, 302, 300, 301, 302, 300, 301, 302]))

所以现在我们需要从第一个红色到第一个蓝色像素的距离

dx2 = (blues[0][0]-reds[0][0])**2          # (200-10)^2dy2 = (blues[1][0]-reds[1][0])**2          # (300-20)^2distance = math.sqrt(dx2 + dy2)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/637094.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号