-1

大家好,我需要帮助来查找图像中两个像素(带坐标)之间的像素数距离,提前谢谢

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)``
4

2 回答 2

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
于 2020-05-04T22:19:44.580 回答
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)
于 2020-05-05T11:04:40.313 回答