OpenCV python - 从相机流中检测和测量对目标的射击

问题描述 投票:0回答:1

我想实现一个枪支俱乐部的项目。目标是检测和测量目标上的射击并计算分数。我对这个项目的想法如下:

  1. 应用感兴趣区域来聚焦于目标
  2. 在相机流上应用滤镜,以获得黑色中心周围的清晰边界
  3. 定义直径,因为它是已知的
  4. 获取边界中心并将其存储为参考点
  5. 检测镜头并获取相对于直径和参考点的径向和距离,从而得出镜头的值
  6. 在屏幕上用圆圈显示最后的镜头及其值

到目前为止我得到的是:

edge detection and center point

径向和距离说明:

get the radial and distance

带有圆圈和值的屏幕:

goal

import cv2
import numpy as np
import imutils

# declare variables

framewidth = 1920
frameheight = 1080
RTSP_URL = 'rtsp://xxxxxx:[email protected]:554/Streaming/channels/1'
cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG)
cap.set(3, framewidth)
cap.set(4, frameheight)
if not cap.isOpened():
    print('Cannot open RTSP stream')
    exit(-1)

# pseudo function

def empty(a):
    pass

# slider

cv2.namedWindow("Parameters")
cv2.resizeWindow("Parameters", 640,240)
cv2.createTrackbar("Threshold1","Parameters",16,255,empty)
cv2.createTrackbar("Threshold2","Parameters",192,255,empty)
cv2.createTrackbar("Threshold3","Parameters",243,255,empty)
cv2.createTrackbar("Threshold4","Parameters",255,255,empty)

# imagestack

def stackImages(scale,imgArray):
    rows = len(imgArray)
    cols = len(imgArray[0])
    rowsAvailable = isinstance(imgArray[0], list)
    width = imgArray[0][0].shape[1]
    height = imgArray[0][0].shape[0]
    if rowsAvailable:
        for x in range ( 0, rows):
            for y in range(0, cols):
                if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
                else:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
                if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
        imageBlank = np.zeros((height, width, 3), np.uint8)
        hor = [imageBlank]*rows
        hor_con = [imageBlank]*rows
        for x in range(0, rows):
            hor[x] = np.hstack(imgArray[x])
        ver = np.vstack(hor)
    else:
        for x in range(0, rows):
            if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
                imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
            else:
                imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
            if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
        hor= np.hstack(imgArray)
        ver = hor
    return ver



def getContours(imgDil,imgContour):

    contours, hierarchy = cv2.findContours(imgDil, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
        
    for cnt in contours:
        area = cv2.contourArea(cnt)
        # compute the center of the contour
        M = cv2.moments(cnt)
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])
        # draw the contour and center of the shape on the image

        if area > 5000:
            cv2.drawContours(imgContour, cnt, -1, (255, 0 ,255),3)
            cv2.circle(imgContour, (cX, cY), 7, (255, 0, 255), -1)
            cv2.putText(imgContour, "center", (cX - 20, cY - 20),
        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 255), 2)


while(True):
    success, img = cap.read()
    imgContour = img.copy()
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    threshold1 = cv2.getTrackbarPos("Threshold1", "Parameters")
    threshold2 = cv2.getTrackbarPos("Threshold2", "Parameters")
    threshold3 = cv2.getTrackbarPos("Threshold3", "Parameters")
    threshold4 = cv2.getTrackbarPos("Threshold4", "Parameters")
    ret, thresh = cv2.threshold(imgGray,threshold1,threshold2,1)
    imgCanny = cv2.Canny(imgGray,threshold3,threshold4)
    kernel = np.ones((3,3))
    imgDil = cv2.dilate(thresh, kernel, iterations=1)
    getContours(imgDil,imgContour)

    imgStack = stackImages(0.4,([img,imgGray,thresh],[imgCanny,img,imgContour]))

    cv2.imshow('Result',imgStack)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

我真的很感谢任何有关最佳实践的建议,当然还有任何帮助。我是否最好选择像 Oak-D 这样具有深度识别功能的立体相机,因为我认为检测黑色目标区域中的镜头可能具有挑战性。

python opencv camera
1个回答
0
投票

射击后移开目标并将其放在彩光前。

买一个非常便宜的灯台/追踪灯箱/灯垫,在上面贴上彩色粘合箔

对我来说非常有用

© www.soinside.com 2019 - 2024. All rights reserved.