Python OpenCV实时检测平行四边形并获取顶点坐标
import cv2
import numpy as np
def detect_parallelograms(image):
# 预处理图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# 查找轮廓
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
parallelograms = []
for contour in contours:
# 近似轮廓
epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
# 判断是否为四边形
if len(approx) == 4:
# 计算面积
area = cv2.contourArea(approx)
if area > 1000: # 设置面积过滤阈值
parallelograms.append(approx)
return parallelograms
def get_coordinates(parallelogram):
# 获取四个顶点的坐标
vertices = []
for vertex in parallelogram:
x, y = vertex[0]
vertices.append((x, y))
# 计算中心点坐标
center_x = int((vertices[0][0] + vertices[2][0]) / 2)
center_y = int((vertices[0][1] + vertices[2][1]) / 2)
center = (center_x, center_y)
return vertices, center
def main():
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
parallelograms = detect_parallelograms(frame)
for parallelogram in parallelograms:
vertices, center = get_coordinates(parallelogram)
# 绘制四边形
cv2.drawContours(frame, [parallelogram], 0, (0, 255, 0), 2)
# 在图像上标记顶点和中心点
for vertex in vertices:
cv2.circle(frame, vertex, 4, (0, 0, 255), -1)
cv2.circle(frame, center, 4, (255, 0, 0), -1)
# 打印坐标到控制台
x1, y1 = vertices[0]
x2, y2 = vertices[1]
x3, y3 = vertices[2]
x4, y4 = vertices[3]
xc, yc = center
print('Vertex 1: ({}, {})'.format(x1, y1))
print('Vertex 2: ({}, {})'.format(x2, y2))
print('Vertex 3: ({}, {})'.format(x3, y3))
print('Vertex 4: ({}, {})'.format(x4, y4))
print('Center: ({}, {})'.format(xc, yc))
print()
# 显示图像
cv2.imshow('Parallelogram Detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
原文地址: https://www.cveoy.top/t/topic/fxH1 著作权归作者所有。请勿转载和采集!