基于openCV实现的单目相机行人和减速带检测-代码

时间:2024-03-16 08:05:52
"""
This .py is to compute H between two planes:image plane and ground plane
(本程序实现:求解成像平面和大地平面的单应矩阵)
"""

import cv2
import numpy as np
from utils.calibrate import *
from utils.capture import Capture
from utils.opFile import writeHomographyToFile
from configs.Intrinsic_normal import cameraMatrix,distCoeff
from utils.measureDistance import getDistance

# mouse callback function
def click_corner(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        xy = "%d,%d" % (x, y)
        cv2.circle(img, (x, y), 1, (255, 0, 0), thickness = -1)
        cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
                    1.0, (0,0,0), thickness = 1)
        print(x,y)
        dist = round(getDistance(x,y),5)
        print("dist=",dist,"m")

        
if __name__ == '__main__':

    img = cv2.imread("./groundImg/61.png")
    cv2.destroyAllWindows()

    # click the corner
    cv2.namedWindow("groundBoard")
    cv2.setMouseCallback("groundBoard",click_corner)
    
    while(1):
        cv2.imshow("groundBoard", img)
        key = cv2.waitKey(1) & 0xff
        if key == ord('q') or key == ord('Q'):
            break

    

此外,项目还进行了技术创新和优化,比如加入了CBAM(Convolutional Block Attention Module)注意力机制,增强了网络对目标特征的学习能力;并且尝试了二值化处理等图像预处理手段,旨在提升模型性能。

然而,在最终的试验结果展示中,尽管已经付出了诸多努力,但目标检测在视频截图中的远程表现并不理想。作者推测这可能是由于相机本身的分辨率限制,导致在较远距离处捕捉到的物体特征模糊不清,从而影响了模型的训练效果和预测精度。这也提示我们在实际应用场景中,除了算法设计和训练策略之外,硬件设备的选择同样至关重要。