一、目标
1、绘制雷达PPI显示器
2、添加雷达扫描线及辉亮,如下图所示。
3、添加运动目标轨迹和参数指示
二、实现方法
1、使用OpenCV绘制同心圆和线段,添加指向N和距离刻度文字。主要代码如下:
# 绘制雷达显示器界面的同心圆
cv.circle( canvas, (350, 350), 100, white, 2)
cv.circle( canvas, (350, 350), 200, white, 2)
cv.circle( canvas, (350, 350), 300, white, 2)
# 绘制十字线
cv.line( canvas, (50, 350), (650, 350), white, 2 )
cv.line( canvas, (350, 50), (350, 650), white, 2 )
start_point = (int(350-300*np.sin(0.25*np.pi)), int(350-300*np.sin(0.25*np.pi)))
end_point = (int(350+300*np.sin(0.25*np.pi)), int(350+300*np.sin(0.25*np.pi)))
cv.line( canvas, start_point, end_point, white, 1 )
start_point = (int(350-300*np.sin(0.25*np.pi)), int(350+300*np.sin(0.25*np.pi)))
end_point = (int(350+300*np.sin(0.25*np.pi)), int(350-300*np.sin(0.25*np.pi)))
cv.line( canvas, start_point, end_point, white, 1 )
# 添加正北指向和距离刻度文字
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText( canvas, "N", (340, 40), font, 1, (255, 255, 255), 1)
cv.putText( canvas, "30", (5, 360), font, 1, (255, 255, 255), 1)
cv.putText( canvas, "30", (650, 360), font, 1, (255, 255, 255), 1)
# 添加参数指示文字
cv.putText( canvas, "Speed(m/s):", (500, 15), font, 0.5, (255, 255, 255), 1)
cv.putText( canvas, "Rotation:", (530, 35), font, 0.5, (255, 255, 255), 1)
cv.putText( canvas, "Coordinate(X):", (488, 55), font, 0.5, (255, 255, 255), 1)
cv.putText( canvas, "Coordinate(Y):", (488, 75), font, 0.5, (255, 255, 255), 1)
2、扫描辉亮为扇形渐变,实现方法为:将扫描辉亮夹角等分为若干份,绘制小角度扇形,比如讲60度扫描辉亮等分为60份,每次绘制1度扇形,60个小扇形组成整个扫描辉亮。每个子扇形的颜色从白色(255, 255, 255)依次过渡到黑色(0, 0, 0)。为什么最终颜色为黑色呢?主要是为了最后将扫描辉亮与显示器混合。代码如下:
# 定义绘制扫描辉亮函数,ang为扫描线所在角度位置
def drawScanner(ang):
img = np.zeros((700, 700, 3), np.uint8)
a = 255 / 60 # 将颜色值255等分60,60为辉亮夹角
for i in range(60):
# 逐次绘制1度扇形,颜色从255到0
cv.ellipse(img, (350, 350), (300, 300), 1, ang - i, ang - i - 1, (255 - i * a, 255 - i * a, 255 - i * a), -1)
return img
3、将雷达显示器和扫描辉亮混合使用addWeighted函数,其格式为cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) 。雷达显示器为src1,alpha取1;扫描辉亮图像是src2,beta值我取的0.6,gamma值取0。
scanImg = drawScanner(i) # 绘制扫描辉亮
blend = cv.addWeighted(temp, 1.0, scanImg, 0.6, 0.0) # 将雷达显示与扫描辉亮混合
cv.imshow('My Radar', blend)
if cv.waitKey(100) == 27: # 按下ESC键退出
break
4、雷达要动态扫描,并显示运动目标信息,实现方法是使用while循环,每次循环复制雷达图像,然后在复制的图像上绘制目标点,并与扫描辉亮混合。
三、实现效果和完整代码
实现效果见下图。
完整代码如下:
import numpy as np
import cv2 as cv
# 生成一个700*700的空灰度图像
canvas = np.zeros((700, 700, 3), np.uint8)
point_size = 1
white = ( 255, 255, 255 )
red = ( 0, 0, 255 )
blue = (255, 0, 0)
yellow = (0, 255, 255)
# 绘制雷达显示器界面的同心圆
cv.circle( canvas, (350, 350), 100, white, 2)
cv.circle( canvas, (350, 350), 200, white, 2)
cv.circle( canvas, (350, 350), 300, white, 2)
# 绘制十字线
cv.line( canvas, (50, 350), (650, 350), white, 2 )
cv.line( canvas, (350, 50), (350, 650), white, 2 )
start_point = (int(350-300*np.sin(0.25*np.pi)), int(350-300*np.sin(0.25*np.pi)))
end_point = (int(350+300*np.sin(0.25*np.pi)), int(350+300*np.sin(0.25*np.pi)))
cv.line( canvas, start_point, end_point, white, 1 )
start_point = (int(350-300*np.sin(0.25*np.pi)), int(350+300*np.sin(0.25*np.pi)))
end_point = (int(350+300*np.sin(0.25*np.pi)), int(350-300*np.sin(0.25*np.pi)))
cv.line( canvas, start_point, end_point, white, 1 )
# 添加正北指向和距离刻度文字
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText( canvas, "N", (340, 40), font, 1, (255, 255, 255), 1)
cv.putText( canvas, "30", (5, 360), font, 1, (255, 255, 255), 1)
cv.putText( canvas, "30", (650, 360), font, 1, (255, 255, 255), 1)
# 添加参数指示文字
cv.putText( canvas, "Speed(m/s):", (500, 15), font, 0.5, (255, 255, 255), 1)
cv.putText( canvas, "Rotation:", (530, 35), font, 0.5, (255, 255, 255), 1)
cv.putText( canvas, "Coordinate(X):", (488, 55), font, 0.5, (255, 255, 255), 1)
cv.putText( canvas, "Coordinate(Y):", (488, 75), font, 0.5, (255, 255, 255), 1)
# 定义绘制扫描辉亮函数,ang为扫描线所在角度位置
def drawScanner(ang):
img = np.zeros((700, 700, 3), np.uint8)
a = 255 / 60 # 将颜色值255等分60,60为辉亮夹角
for i in range(60):
# 逐次绘制1度扇形,颜色从255到0
cv.ellipse(img, (350, 350), (300, 300), 1, ang - i, ang - i - 1, (255 - i * a, 255 - i * a, 255 - i * a), -1)
return img
# 运动目标初始值,beta飞行角度,speed速度
beta = 225 / 180 * np.pi # 飞行方位角
speed = 300 # 飞行速度
pointStartX = 500
pointStartY = 10
pointEndX = 500
pintEndY = 500
i = 0
delteT = 1 # 目标运动的比例值
while(1):
i += 1
pointStartX += int(speed*delteT*0.01*np.cos(beta))
pointStartY += -int(speed*delteT*0.01*np.sin(beta))
cv.circle(canvas, (pointStartX, pointStartY), point_size, yellow, 1) # 目标运动轨迹点
# 复制雷达界面,将目标运动和参数指示绘制在复制图上
temp = np.copy(canvas)
cv.circle(temp, (pointStartX, pointStartY), point_size, red, 6) # 目标点
cv.putText(temp, str(speed), (605, 15), font, 0.5, (0, 255, 0), 1)
cv.putText(temp, str(beta/np.pi*180), (605, 35), font, 0.5, (0, 255, 0), 1)
cv.putText(temp, str(pointStartX), (605, 55), font, 0.5, (0, 255, 0), 1)
cv.putText(temp, str(pointStartY), (605, 75), font, 0.5, (0, 255, 0), 1)
scanImg = drawScanner(i) # 绘制扫描辉亮
blend = cv.addWeighted(temp, 1.0, scanImg, 0.6, 0.0) # 将雷达显示与扫描辉亮混合
cv.imshow('My Radar', blend)
if cv.waitKey(100) == 27: # 按下ESC键退出
break
转载:https://blog.csdn.net/MrV2020/article/details/105464547
查看评论