Python 检测多边形内的矩形

Python 检测多边形内的矩形,python,opencv,machine-learning,deep-learning,computer-vision,Python,Opencv,Machine Learning,Deep Learning,Computer Vision,我有一个脚本可以检测Pedstreans和Pedstreans的危险区域,但是我需要检测Pedstreans是否在Dangerárea内部 我的危险区域是一个多基因系统,而被发现的人是一个盒子 检测多功能手机内盒子的最佳方法是什么 脚本示例: # -*- coding: utf-8 -*- import numpy as np import cv2 import time import math class DetectorAPI: cap = cv2.Vide

我有一个脚本可以检测Pedstreans和Pedstreans的危险区域,但是我需要检测Pedstreans是否在Dangerárea内部

我的危险区域是一个多基因系统,而被发现的人是一个盒子

检测多功能手机内盒子的最佳方法是什么

脚本示例:

# -*- coding: utf-8 -*-
import numpy as np
import cv2
import time
import math

class DetectorAPI:          
     cap = cv2.VideoCapture("VideoCone.MOV")
     while True:
        r, img = cap.read()
        #DEFINE A ÁREA DO VIDEO EM QUE O MODELO IRA ATUAR
        #img = img[10:1280, 230:1280]
        img = cv2.resize(img, (800, 600))
        overlay = img.copy()

        #Frame Detectação Red Zone
        vermelho_inicio = np.array([0, 9, 178])
        vermelho_fim = np.array([255, 40, 255])
        #Mascara de detecção do modelo de cor
        mask = cv2.inRange(img, vermelho_inicio, vermelho_fim)
        #Pontos e desenho do poligono (Objeto detectado no laser)
        np_points = np.transpose(np.nonzero(mask))
        points = np.fliplr(np_points) # opencv uses flipped x,y coordinates 
        approx = cv2.convexHull(points)
        DangerArea = cv2.fillPoly(img, [approx], (0,0,255))

        #Transparencia
        cv2.addWeighted(overlay,0.3,img,1-0.65,0,img);

        edges = cv2.Canny(mask,30,120)
        #DESENHO AS LINHAS NO LASER (Cone)        
        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)     

        #CAPTURO AS INFORMAÇÕES DO FRAME
        height, width, channels = img.shape
        #DIVISÃO PARA CAPTURAR O CENTRO DA IMAGEM
        upper_left = (int(width / 2), int(height / 4))
        bottom_right = (int(width * 2 / 2), int(height * 3 / 4))
        #ESCREVO O RETANGULO NO CENTRO DO VÍDEO
        cv2.rectangle(img,(100,150), (200,250),(0,152,112),1);
        cv2.rectangle(img,(500,150), (420,250),(0,100,255),1);
        #Escrevo o texto na Danger Area
        #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);
        #Imprimo no console o centro da imagem
        print('Upper_Left: '+str(upper_left)+' bottom_right: '+str(bottom_right));


        #Exibe o video 
        cv2.imshow("edges", edges)
        cv2.imshow("Detectar Pessoas", img)

        key = cv2.waitKey(1)
        if key & 0xFF == ord('q'):
            break

除了检查角点是否在多边形内,如果两个多边形的交点很好,请尝试以下方法:如何传递变量p0和p1的坐标?因为长方体有一个四值cv2.矩形(img,(长方体[1],长方体[0]),(长方体[3],长方体[2]),(255,0,0),2),我需要检查所有长方体是否都在polygone内
p0 = (10,10)
p1 = (400,400)
is_p0_ok = cv2.pointPolygonTest(approx, p0, False) < 0
is_p1_ok = cv2.pointPolygonTest(approx, p1, False) < 0
print(is_p0_ok)
>>> True
print(is_p1_ok)
>>> False