You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于OpenCV的射击靶位弹孔检测计分系统技术咨询

射击俱乐部靶位弹孔检测、测量及计分系统开发咨询

项目概述

我正在为射击俱乐部开发一套靶位弹孔检测、测量及计分系统,核心目标是自动识别靶上弹孔并计算对应分数。

实现思路

  • 设置感兴趣区域(ROI),将计算范围聚焦到靶位区域
  • 对相机流应用滤镜处理,提取黑色靶心的清晰边界
  • 预先定义已知的靶心直径,作为尺寸换算的参考基准
  • 识别靶心边界的中心位置,保存为计分的参考原点
  • 检测图像中的弹孔位置,计算弹孔相对参考原点的径向距离,进而转换为对应分值
  • 在实时画面中用圆圈标注最新弹孔,并同步显示该弹孔的分值

当前开发进度

已完成边缘检测与靶心中心点定位功能;径向距离计算逻辑已明确;预期界面效果已确定。

现有实现代码

import cv2
import numpy as np
import imutils

# 声明变量
framewidth = 1920
frameheight = 1080
RTSP_URL = 'rtsp://xxxxxx:xxxxxxxx@192.168.1.64: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)

# 空回调函数
def empty(a):
    pass

# 创建参数调节滑块窗口
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)

# 图像堆叠函数
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)
        # 计算轮廓中心
        M = cv2.moments(cnt)
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])
        # 在图像上绘制轮廓和中心

        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

# 释放资源
cap.release()
cv2.destroyAllWindows()

咨询内容

  1. 请提供该项目开发的最佳实践建议
  2. 由于黑色靶区的弹孔检测存在一定难度,是否应该选用Oak-D这类具备深度识别能力的双目相机?

内容的提问来源于stack exchange,提问作者Tissi_2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:01:05