如何获取特定的折线坐标?

时间:2018-08-22 10:01:50

标签: python-3.x pygame opencv-contrib

我将创建一个python脚本来检测对象并在其周围绘制线条,之后我还将在其周围绘制4条用作边框的线条。当对象线与它周围的任何4条线发生碰撞时,它将执行一个操作(临时只向左,向右,向上,向下打印)。View the sample

我现在面临的问题是,我不知道如何获取对象的线坐标,因为它是折线。对于围绕正方形的图像的4条线,实际上是矩形,就像一条线一样绘制。

这是用于初始化图像周围矩形坐标的代码。

import pygame, sys, random
import cv2
import numpy as np
from pygame.locals import *
pygame.init()
mainClock = pygame.time.Clock()

WINDOWWIDTH = 640
WINDOWHEIGHT = 480
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Collision Detection')
SIZE = 7
bouncer = {'rect':pygame.Rect(300, 100, 50, 50), 'dir':UPLEFT}
RED =   (255, 0, 0)

upLine = pygame.Rect(50, 50, 540, SIZE)
leftLine = pygame.Rect(50, 50, SIZE, 380)
rightLine = pygame.Rect(583, 50, SIZE, 380)
downLine = pygame.Rect(50, 430, 540, SIZE)

这是用于检测物体并在其周围画线的代码。

MIN_MATCH_COUNT=30

detector=cv2.xfeatures2d.SIFT_create()

FLANN_INDEX_KDITREE=0
flannParam=dict(algorithm=FLANN_INDEX_KDITREE,tree=5)
flann=cv2.FlannBasedMatcher(flannParam,{})

trainImg=cv2.imread("ObjectTest.jpg",0)
trainKP,trainDesc=detector.detectAndCompute(trainImg,None)

cam=cv2.VideoCapture(0)
# run the game loop
while True:
    ret, QueryImgBGR=cam.read()
    QueryImg=cv2.cvtColor(QueryImgBGR,cv2.COLOR_BGR2GRAY)
    queryKP,queryDesc=detector.detectAndCompute(QueryImg,None)
    matches=flann.knnMatch(queryDesc,trainDesc,k=2)

    goodMatch=[]

   # check for the QUIT event
    for event in pygame.event.get():
        if event.type == QUIT:
            cam.release()
            pygame.quit()
            sys.exit()

    windowSurface.fill(BLACK)

    for m,n in matches:
        if(m.distance<0.75*n.distance):
            goodMatch.append(m)
    if(len(goodMatch)>MIN_MATCH_COUNT):
        tp=[]
        qp=[]
        for m in goodMatch:
            tp.append(trainKP[m.trainIdx].pt)
            qp.append(queryKP[m.queryIdx].pt)
        tp,qp=np.float32((tp,qp))
        H,status=cv2.findHomography(tp,qp,cv2.RANSAC,3.0)
        h,w=trainImg.shape
        trainBorder=np.float32([[[0,0],[0,h-1],[w-1,h-1],[w-1,0]]])
        queryBorder=cv2.perspectiveTransform(trainBorder,H)
        cv2.polylines(QueryImgBGR,[np.int32(queryBorder)],True,(0,255,0),5)
    else:
        print("Not Enough match found- %d/%d"%(len(goodMatch),MIN_MATCH_COUNT))
    QueryImgBGR = cv2.cvtColor(QueryImgBGR, cv2.COLOR_BGR2RGB)
    QueryImgBGR = np.rot90(QueryImgBGR)
    QueryImgBGR = pygame.surfarray.make_surface(QueryImgBGR)
    QueryImgBGR = pygame.transform.flip(QueryImgBGR,True,False)
    windowSurface.blit(QueryImgBGR, (0,0))

这是绘制4条线以在图像旁边形成正方形的代码。

    pygame.draw.rect(windowSurface, GREEN, upLine)
    pygame.draw.rect(windowSurface, GREEN, leftLine)
    pygame.draw.rect(windowSurface, GREEN, rightLine)
    pygame.draw.rect(windowSurface, GREEN, downLine)

    print(queryBorder)
     # draw the window onto the screen
    pygame.display.update()
    mainClock.tick(40)

我不知道如何获取折线的坐标,我正在尝试使用它来获取折线:

print(queryBorder)

但是我得到4个坐标,如何只得到1个?还是其他方式做到这一点? View sample

我还参考了该网站https://inventwithpython.com/chapter18.html上的代码,以了解2个矩形之间的碰撞检测。我实际上是想在检测到的对象周围绘制一个矩形,但是我不知道如何绘制该矩形,并且不打算将矩形同时转换为我的对象。

0 个答案:

没有答案