如何计算两条线之间的距离?
How to calculate the distance between two lines?
我一直在尝试计算 Python 中图像中两条线之间的距离。例如,在下面给出的图像中,我想找到黄色块两端之间的垂直距离。到目前为止,我只能推导出两个像素之间的距离。
我可以编写的代码是找出红色和蓝色像素之间的距离。我想我可以改进它,使这幅图像中的两个 points/lines 之间的距离变大,但还没有成功。
import numpy as np
from PIL import Image
import math
# Load image and ensure RGB - just in case palettised
im = Image.open("2points.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))
print(reds)
# 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))
print(blues)
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)
print(distance)
在准备这个答案时,我意识到,关于 cv2.boxPoints
was misleading. Of course, I had cv2.boundingRect
的暗示在我脑海中 – 抱歉!
不过,这里是完整的分步方法:
- 使用
cv2.inRange
屏蔽所有黄色像素。注意:您的图像有 JPG 伪像,因此您在蒙版中得到很多噪音,请参见。输出:
使用cv2.findContours
查找蒙版中的所有轮廓。由于有许多微小的人工制品,这将超过 50 个。
使用Python的max
function on the (list of) found contours using cv2.contourArea
作为key得到最大的轮廓。
最后用cv2.boundingRect
得到轮廓的外接矩形。那是一个元组 (x, y, widht, height)
。只需使用最后两个元素,您就可以获得所需的信息。
那是我的代码:
import cv2
# Read image with OpenCV
img = cv2.imread('path/to/your/image.ext')
# Mask yellow color (0, 255, 255) in image; Attention: OpenCV uses BGR ordering
yellow_mask = cv2.inRange(img, (0, 255, 255), (0, 255, 255))
# Find contours in yellow mask w.r.t the OpenCV version
cnts = cv2.findContours(yellow_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
# Get the largest contour
cnt = max(cnts, key=cv2.contourArea)
# Get width and height from bounding rectangle of largest contour
(x, y, w, h) = cv2.boundingRect(cnt)
print('Width:', w, '| Height:', h)
输出
Width: 518 | Height: 320
似乎有道理。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
OpenCV: 4.5.1
----------------------------------------
我一直在尝试计算 Python 中图像中两条线之间的距离。例如,在下面给出的图像中,我想找到黄色块两端之间的垂直距离。到目前为止,我只能推导出两个像素之间的距离。
我可以编写的代码是找出红色和蓝色像素之间的距离。我想我可以改进它,使这幅图像中的两个 points/lines 之间的距离变大,但还没有成功。
import numpy as np
from PIL import Image
import math
# Load image and ensure RGB - just in case palettised
im = Image.open("2points.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))
print(reds)
# 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))
print(blues)
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)
print(distance)
在准备这个答案时,我意识到,关于 cv2.boxPoints
was misleading. Of course, I had cv2.boundingRect
的暗示在我脑海中 – 抱歉!
不过,这里是完整的分步方法:
- 使用
cv2.inRange
屏蔽所有黄色像素。注意:您的图像有 JPG 伪像,因此您在蒙版中得到很多噪音,请参见。输出:
使用
cv2.findContours
查找蒙版中的所有轮廓。由于有许多微小的人工制品,这将超过 50 个。使用Python的
max
function on the (list of) found contours usingcv2.contourArea
作为key得到最大的轮廓。最后用
cv2.boundingRect
得到轮廓的外接矩形。那是一个元组(x, y, widht, height)
。只需使用最后两个元素,您就可以获得所需的信息。
那是我的代码:
import cv2
# Read image with OpenCV
img = cv2.imread('path/to/your/image.ext')
# Mask yellow color (0, 255, 255) in image; Attention: OpenCV uses BGR ordering
yellow_mask = cv2.inRange(img, (0, 255, 255), (0, 255, 255))
# Find contours in yellow mask w.r.t the OpenCV version
cnts = cv2.findContours(yellow_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
# Get the largest contour
cnt = max(cnts, key=cv2.contourArea)
# Get width and height from bounding rectangle of largest contour
(x, y, w, h) = cv2.boundingRect(cnt)
print('Width:', w, '| Height:', h)
输出
Width: 518 | Height: 320
似乎有道理。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
OpenCV: 4.5.1
----------------------------------------