【发布时间】:2021-06-16 18:04:20
【问题描述】:
考虑下图,轮廓以绿色显示,直线以红色显示
我们如何找到直线切割轮廓的交点
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)
【问题讨论】:
标签: python opencv opencv-contour