检测矩形是否在多边形内部

我有一个脚本可以检测行人所在的危险区域,但我需要检测行人是否在危险区域内。

我的危险区域是一个多边形,而检测到的人是用一个矩形框表示的!

检测一个矩形是否在多边形内部的最佳方法是什么?

enter image description here

脚本示例:

# -*- coding: utf-8 -*-import numpy as npimport cv2import timeimport mathclass DetectorAPI:               cap = cv2.VideoCapture("VideoCone.MOV")     while True:        r, img = cap.read()        #定义视频中模型将要作用的区域        #img = img[10:1280, 230:1280]        img = cv2.resize(img, (800, 600))        overlay = img.copy()        #帧检测红区        vermelho_inicio = np.array([0, 9, 178])        vermelho_fim = np.array([255, 40, 255])        #颜色模型的检测掩膜        mask = cv2.inRange(img, vermelho_inicio, vermelho_fim)        #点和多边形的绘制(激光检测到的对象)        np_points = np.transpose(np.nonzero(mask))        points = np.fliplr(np_points) # opencv使用翻转的x,y坐标         approx = cv2.convexHull(points)        DangerArea = cv2.fillPoly(img, [approx], (0,0,255))        #透明度        cv2.addWeighted(overlay,0.3,img,1-0.65,0,img);        edges = cv2.Canny(mask,30,120)        #在激光(锥形)上绘制线条                lines = cv2.HoughLinesP(edges, 5, np.pi/180, 30, maxLineGap=50)        a,b,c = lines.shape        if lines is not None:           for line in lines:                        x1, y1, x2, y2 = line[0]              cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 1)             #捕获帧的信息        height, width, channels = img.shape        #分割以捕获图像的中心        upper_left = (int(width / 2), int(height / 4))        bottom_right = (int(width * 2 / 2), int(height * 3 / 4))        #在视频中心绘制矩形        cv2.rectangle(img,(100,150), (200,250),(0,152,112),1);        cv2.rectangle(img,(500,150), (420,250),(0,100,255),1);        #在危险区域写入文本        #cv2.putText(DangerArea,'Danger Area',(int(width / 4),int(height * 3 / 4)),  cv2.FONT_HERSHEY_SIMPLEX, 0.5,(255,255,255),2,cv2.LINE_AA)        #cv2.addWeighted(overlay,0.3,img,1-0.4,0,img);        #在控制台打印图像的中心        print('Upper_Left: '+str(upper_left)+' bottom_right: '+str(bottom_right));        #显示视频         cv2.imshow("edges", edges)        cv2.imshow("Detectar Pessoas", img)        key = cv2.waitKey(1)        if key & 0xFF == ord('q'):            break

回答:

p0 = (10,10)p1 = (400,400)is_p0_ok = cv2.pointPolygonTest(approx, p0, False) < 0is_p1_ok = cv2.pointPolygonTest(approx, p1, False) < 0print(is_p0_ok)>>> Trueprint(is_p1_ok)>>> False

根据您的实现,您可能需要使用cv2.pointPolygonTest检查矩形的中心,或者检查它的所有角点。

编辑

如何将矩形框的坐标传递给变量p0和p1?因为矩形框有四个值cv2.rectangle(img,(box[1],box[0]),(box[3],box[2]),(255,0,0),2),我需要检查整个矩形是否在多边形内部

p0 = (box[1],box[0])p1 = (box[1], box[2])p2 = (box[3],box[2])p3 = (box[3], box[0])rect = [p0,p1,p2,p3]rect_pts_inside = [cv2.pointPolygonTest(approx, pt, False) < 0 for pt in rect]whole_rect_inside_polygon = all(rect_pts_inside)part_of_rect_inside_polygon = any(rect_pts_inside)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注