如何找到线和轮廓之间的交点
How to find intersection points between lines and contours
考虑下图,其中等高线显示为绿色,直线显示为红色
如何找到直线与等高线的交点
import cv2
from matplotlib import pyplot as plt
import numpy as np
# Read image img
# Binarize
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
# Find Contours
_, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Draw Contours
blank_mask = np.zeros((thresh.shape[0],thresh.shape[1],3), np.uint8)
cv2.drawContours(blank_mask, contours, -1, (0, 255, 0), 1)
# Define lines coordinates
line1 = [x1, y1, x2, y2]
line2 = [x1, y1, x2, y2]
line3 = [x1, y1, x2, y2]
# Draw Lines over Contours
cv2.line(blank_mask, (line1[0], line1[1]), (line1[2], line1[3]), (255, 0, 0), thickness=1)
cv2.line(blank_mask, (line2[0], line2[1]), (line2[2], line2[3]), (255, 0, 0), thickness=1)
cv2.line(blank_mask, (line3[0], line3[1]), (line3[2], line3[3]), (255, 0, 0), thickness=1)
# Show Image
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(blank_mask)
您可以逐个像素地检查它们重叠的地方。在绘制线条之前,您必须保留轮廓所在像素的副本,因为这会用线条的颜色覆盖重叠像素。在此处的代码中添加几行
# Draw Contours
blank_mask = np.zeros((thresh.shape[0],thresh.shape[1],3), np.uint8)
cv2.drawContours(blank_mask, contours, -1, (0, 255, 0), 1)
contours_idx = blank_mask[...,1] == 255
# Define lines coordinates
line1 = [x1, y1, x2, y2]
line2 = [x1, y1, x2, y2]
line3 = [x1, y1, x2, y2]
# Draw Lines over Contours
cv2.line(blank_mask, (line1[0], line1[1]), (line1[2], line1[3]), (255, 0, 0), thickness=1)
cv2.line(blank_mask, (line2[0], line2[1]), (line2[2], line2[3]), (255, 0, 0), thickness=1)
cv2.line(blank_mask, (line3[0], line3[1]), (line3[2], line3[3]), (255, 0, 0), thickness=1)
lines_idx = blank_mask[...,0] == 255
然后
overlap = np.where(contours_idx * lines_idx)
这会给你类似下面的东西(我不得不猜测你的线坐标和图像调整大小形状,所以我可能有点偏离)
(array([ 90, 110, 110, 140, 140], dtype=int64), array([ 80, 40, 141, 27, 156], dtype=int64))
如果你想要它们在像素坐标的元组中
list(zip(*overlap))
Edit:对于 contours/lines 的颜色在多个平面中的更通用的解决方案,您可以在之后检查每个像素的完整颜色每次抽奖。例如
# Change these
contours_idx = blank_mask[...,1] == 255
lines_idx = blank_mask[...,0] == 255
# To this
contours_idx = np.all(blank_mask == (0, 255, 0), axis=-1)
lines_idx = np.all(blank_mask == (255, 0, 0), axis=-1)
考虑下图,其中等高线显示为绿色,直线显示为红色
如何找到直线与等高线的交点
import cv2
from matplotlib import pyplot as plt
import numpy as np
# Read image img
# Binarize
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
# Find Contours
_, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Draw Contours
blank_mask = np.zeros((thresh.shape[0],thresh.shape[1],3), np.uint8)
cv2.drawContours(blank_mask, contours, -1, (0, 255, 0), 1)
# Define lines coordinates
line1 = [x1, y1, x2, y2]
line2 = [x1, y1, x2, y2]
line3 = [x1, y1, x2, y2]
# Draw Lines over Contours
cv2.line(blank_mask, (line1[0], line1[1]), (line1[2], line1[3]), (255, 0, 0), thickness=1)
cv2.line(blank_mask, (line2[0], line2[1]), (line2[2], line2[3]), (255, 0, 0), thickness=1)
cv2.line(blank_mask, (line3[0], line3[1]), (line3[2], line3[3]), (255, 0, 0), thickness=1)
# Show Image
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(blank_mask)
您可以逐个像素地检查它们重叠的地方。在绘制线条之前,您必须保留轮廓所在像素的副本,因为这会用线条的颜色覆盖重叠像素。在此处的代码中添加几行
# Draw Contours
blank_mask = np.zeros((thresh.shape[0],thresh.shape[1],3), np.uint8)
cv2.drawContours(blank_mask, contours, -1, (0, 255, 0), 1)
contours_idx = blank_mask[...,1] == 255
# Define lines coordinates
line1 = [x1, y1, x2, y2]
line2 = [x1, y1, x2, y2]
line3 = [x1, y1, x2, y2]
# Draw Lines over Contours
cv2.line(blank_mask, (line1[0], line1[1]), (line1[2], line1[3]), (255, 0, 0), thickness=1)
cv2.line(blank_mask, (line2[0], line2[1]), (line2[2], line2[3]), (255, 0, 0), thickness=1)
cv2.line(blank_mask, (line3[0], line3[1]), (line3[2], line3[3]), (255, 0, 0), thickness=1)
lines_idx = blank_mask[...,0] == 255
然后
overlap = np.where(contours_idx * lines_idx)
这会给你类似下面的东西(我不得不猜测你的线坐标和图像调整大小形状,所以我可能有点偏离)
(array([ 90, 110, 110, 140, 140], dtype=int64), array([ 80, 40, 141, 27, 156], dtype=int64))
如果你想要它们在像素坐标的元组中
list(zip(*overlap))
Edit:对于 contours/lines 的颜色在多个平面中的更通用的解决方案,您可以在之后检查每个像素的完整颜色每次抽奖。例如
# Change these
contours_idx = blank_mask[...,1] == 255
lines_idx = blank_mask[...,0] == 255
# To this
contours_idx = np.all(blank_mask == (0, 255, 0), axis=-1)
lines_idx = np.all(blank_mask == (255, 0, 0), axis=-1)