电脑视觉距离btw像素

时间:2020-05-04 21:57:25

标签: python opencv

大家好,我需要帮助,以找到图像中两个像素(带坐标)之间的像素数距离 预先感谢

import math
import numpy as np
import matplotlib.pyplot as plt
from math import sqrt
from PIL import Image, ImageOps
%matplotlib inline``
``
img = cv.imread('building.jpg',-1)
cv.imshow('image',img)
 # to display image until you press any key
cv.waitKey(0)
 # to destroy all windows
cv.destroyAllWindows()
pixels = np.array(img)
width, height, channels = pixels.shape
print(width)
print (height)
P=img[200,510]
print (P)
Q=img[100,410]
print (Q)``

2 个答案:

答案 0 :(得分:1)

我已使用此函数计算距离:

def distance(x1, y1, x2, y2):
    return ((x2-x1)**2+(y2-y1)**2)**(1/2)

point1 = (200, 510)
point2 = (100, 410)

distance(point1[0], point1[1],
         point2[0], point2[1])

输出:

141.4213562373095

答案 1 :(得分:0)

或者,您可以将scipy用于此任务。

from scipy.spatial.distance import euclidean
pt1 = (100, 100)
pt2 = (200, 200)
dist = euclidean (pt1, pt2)
# if want in int then use 
#dist = int(dist)