Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2像素之间的距离_Python_Numpy_Opencv_Image Processing_Computer Vision - Fatal编程技术网

Python 2像素之间的距离

Python 2像素之间的距离,python,numpy,opencv,image-processing,computer-vision,Python,Numpy,Opencv,Image Processing,Computer Vision,来自软件开发,我是图像处理新手。 我试图得到一个图像中两个像素之间的距离,这是一个形状为1001003的numpy数组 例如,我想找到图像中蓝色像素0,0,255和红色像素255,0,0之间的距离,我尝试使用For循环或np。其中。。。但是没有成功。 距离可能是图像中两个索引之间的某种差异,也可能是这些颜色的像素更多,因此至少在图像中第一次相遇 你知道怎么做吗 编辑: 我正在捕获屏幕的一部分,如下所示: screen = np.array(pyautogui.screenshot(region=

来自软件开发,我是图像处理新手。 我试图得到一个图像中两个像素之间的距离,这是一个形状为1001003的numpy数组

例如,我想找到图像中蓝色像素0,0,255和红色像素255,0,0之间的距离,我尝试使用For循环或np。其中。。。但是没有成功。 距离可能是图像中两个索引之间的某种差异,也可能是这些颜色的像素更多,因此至少在图像中第一次相遇

你知道怎么做吗

编辑: 我正在捕获屏幕的一部分,如下所示:

screen = np.array(pyautogui.screenshot(region=(80,120,100,100)))

现在我想找到图像中蓝色和红色的像素,以及它们之间的距离,让我们从测试图像开始。它是400x300像素的灰度192,具有:

20,10的红色3x3正方形, 300200的蓝色3x3正方形 现在,请执行以下操作:

import numpy as np
import PIL
import math

# Load image and ensure RGB - just in case palettised
im=Image.open("a.png").convert("RGB")

# Make numpy array from image
npimage=np.array(im)

# Describe what a single red pixel looks like
red=np.array([255,0,0],dtype=np.uint8)

# Find [x,y] coordinates of all red pixels
reds=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]))
(array([200, 200, 200, 201, 201, 201, 202, 202, 202]),
 array([300, 301, 302, 300, 301, 302, 300, 301, 302]))
现在让我们来做蓝色像素:

# Describe what a single blue pixel looks like
blue=np.array([0,0,255],dtype=np.uint8)

# Find [x,y] coordinates of all blue pixels
blues=np.where(np.all((npimage==blue),axis=-1))
这使得:

(array([10, 10, 10, 11, 11, 11, 12, 12, 12]),
 array([20, 21, 22, 20, 21, 22, 20, 21, 22]))
(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)^2
dy2 = (blues[1][0]-reds[1][0])**2          # (300-20)^2
distance = math.sqrt(dx2 + dy2)

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

20,10的红色3x3正方形, 300200的蓝色3x3正方形 现在,请执行以下操作:

import numpy as np
import PIL
import math

# Load image and ensure RGB - just in case palettised
im=Image.open("a.png").convert("RGB")

# Make numpy array from image
npimage=np.array(im)

# Describe what a single red pixel looks like
red=np.array([255,0,0],dtype=np.uint8)

# Find [x,y] coordinates of all red pixels
reds=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]))
(array([200, 200, 200, 201, 201, 201, 202, 202, 202]),
 array([300, 301, 302, 300, 301, 302, 300, 301, 302]))
现在让我们来做蓝色像素:

# Describe what a single blue pixel looks like
blue=np.array([0,0,255],dtype=np.uint8)

# Find [x,y] coordinates of all blue pixels
blues=np.where(np.all((npimage==blue),axis=-1))
这使得:

(array([10, 10, 10, 11, 11, 11, 12, 12, 12]),
 array([20, 21, 22, 20, 21, 22, 20, 21, 22]))
(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)^2
dy2 = (blues[1][0]-reds[1][0])**2          # (300-20)^2
distance = math.sqrt(dx2 + dy2)

尝试将ndarray的形状改为10000、3,然后应用scipy package中的pdist。颜色不重要,请使用位置。在找到这些像素之间的距离之前,是否先查找或查找蓝色像素和红色像素,找不到蓝色和红色像素?你的意思是像测量一张纸上两点之间的距离?看看欧几里德向量空间中的二维距离:sqrta.x-b.x^2+a.y-b.y^2请给我们看看不起作用的代码。。。也许这有助于理解你想要实现的目标。你需要什么样的距离?我们谈论的是空间距离还是颜色距离?尝试将ndarray的形状改为10000、3,然后应用scipy package中的pdist。颜色不重要,请使用位置。在找到这些像素之间的距离之前,是否先查找或查找蓝色像素和红色像素,找不到蓝色和红色像素?你的意思是像测量一张纸上两点之间的距离?看看欧几里德向量空间中的二维距离:sqrta.x-b.x^2+a.y-b.y^2请给我们看看不起作用的代码。。。也许这有助于理解你想要实现的目标。你需要什么样的距离?我们谈论的是空间距离还是颜色距离?