高级数据可视化:使用Ultralytics YOLOv8 绘制热图 🚀
热图简介
生成的热图 Ultralytics YOLOv8将复杂的数据转换成生动的彩色编码矩阵。这种可视化工具采用色谱来表示不同的数据值,暖色调表示较高的强度,冷色调表示较低的值。热图在可视化复杂的数据模式、相关性和异常情况方面表现出色,为不同领域的数据解读提供了一种易于理解且引人入胜的方法。
观看: 使用热图Ultralytics YOLOv8
为什么选择热图进行数据分析?
- 直观的数据分布可视化:热图可简化对数据集中和分布的理解,将复杂的数据集转换为易于理解的可视化格式。
- 高效模式检测:通过热图格式的可视化数据,可以更容易地发现趋势、群组和异常值,从而促进更快地分析和洞察。
- 增强空间分析和决策:热图有助于说明空间关系,帮助商业智能、环境研究和城市规划等领域的决策过程。
真实世界的应用
交通运输 | 零售 |
---|---|
Ultralytics YOLOv8 交通热图 | Ultralytics YOLOv8 零售业热图 |
热图配置
heatmap_alpha
:确保该值在 (0.0 - 1.0) 范围内。decay_factor
:用于在帧中不再存在对象后删除热图,其值也应在 (0.0 - 1.0) 范围内。
热图使用Ultralytics YOLOv8 示例
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
# Init heatmap
heatmap_obj = solutions.Heatmap(
colormap=cv2.COLORMAP_PARULA,
view_img=True,
shape="circle",
names=model.names,
)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
line_points = [(20, 400), (1080, 404)] # line for object counting
# Init heatmap
heatmap_obj = solutions.Heatmap(
colormap=cv2.COLORMAP_PARULA,
view_img=True,
shape="circle",
count_reg_pts=line_points,
names=model.names,
)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
# Define polygon points
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360), (20, 400)]
# Init heatmap
heatmap_obj = solutions.Heatmap(
colormap=cv2.COLORMAP_PARULA,
view_img=True,
shape="circle",
count_reg_pts=region_points,
names=model.names,
)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
# Define region points
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]
# Init heatmap
heatmap_obj = solutions.Heatmap(
colormap=cv2.COLORMAP_PARULA,
view_img=True,
shape="circle",
count_reg_pts=region_points,
names=model.names,
)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8s.pt") # YOLOv8 custom/pretrained model
im0 = cv2.imread("path/to/image.png") # path to image file
h, w = im0.shape[:2] # image height and width
# Heatmap Init
heatmap_obj = solutions.Heatmap(
colormap=cv2.COLORMAP_PARULA,
view_img=True,
shape="circle",
names=model.names,
)
results = model.track(im0, persist=True)
im0 = heatmap_obj.generate_heatmap(im0, tracks=results)
cv2.imwrite("ultralytics_output.png", im0)
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
classes_for_heatmap = [0, 2] # classes for heatmap
# Init heatmap
heatmap_obj = solutions.Heatmap(
colormap=cv2.COLORMAP_PARULA,
view_img=True,
shape="circle",
names=model.names,
)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False, classes=classes_for_heatmap)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
论据 Heatmap()
名称 | 类型 | 默认值 | 说明 |
---|---|---|---|
names |
list |
None |
类名词典 |
imw |
int |
0 |
图像宽度。 |
imh |
int |
0 |
图像高度。 |
colormap |
int |
cv2.COLORMAP_JET |
热图使用的颜色图。 |
heatmap_alpha |
float |
0.5 |
热图叠加的 Alpha 混合值。 |
view_img |
bool |
False |
是否使用热图叠加显示图像。 |
view_in_counts |
bool |
True |
是否显示进入区域的对象计数。 |
view_out_counts |
bool |
True |
是否显示退出区域的对象计数。 |
count_reg_pts |
list 或 None |
None |
定义计数区域的点(直线或多边形)。 |
count_txt_color |
tuple |
(0, 0, 0) |
显示计数的文本颜色。 |
count_bg_color |
tuple |
(255, 255, 255) |
显示计数的背景颜色。 |
count_reg_color |
tuple |
(255, 0, 255) |
计数区域的颜色。 |
region_thickness |
int |
5 |
区域线的厚度。 |
line_dist_thresh |
int |
15 |
线性计数的距离阈值 |
line_thickness |
int |
2 |
绘画线条的粗细。 |
decay_factor |
float |
0.99 |
热图的衰减系数,用于降低随时间变化的强度。 |
shape |
str |
"circle" |
热图 Blobs 的形状("圆形 "或 "矩形")。 |
论据 model.track
论据 | 类型 | 默认值 | 说明 |
---|---|---|---|
source |
str |
None |
Specifies the source directory for images or videos. Supports file paths and URLs. |
persist |
bool |
False |
Enables persistent tracking of objects between frames, maintaining IDs across video sequences. |
tracker |
str |
botsort.yaml |
Specifies the tracking algorithm to use, e.g., bytetrack.yaml 或 botsort.yaml . |
conf |
float |
0.3 |
Sets the confidence threshold for detections; lower values allow more objects to be tracked but may include false positives. |
iou |
float |
0.5 |
Sets the Intersection over Union (IoU) threshold for filtering overlapping detections. |
classes |
list |
None |
Filters results by class index. For example, classes=[0, 2, 3] only tracks the specified classes. |
verbose |
bool |
True |
Controls the display of tracking results, providing a visual output of tracked objects. |
热图 COLORMAPs
彩色地图名称 | 说明 |
---|---|
cv::COLORMAP_AUTUMN |
秋色地图 |
cv::COLORMAP_BONE |
骨骼颜色图 |
cv::COLORMAP_JET |
喷气彩图 |
cv::COLORMAP_WINTER |
冬季彩色地图 |
cv::COLORMAP_RAINBOW |
彩虹颜色地图 |
cv::COLORMAP_OCEAN |
海洋颜色地图 |
cv::COLORMAP_SUMMER |
夏季彩色地图 |
cv::COLORMAP_SPRING |
春色地图 |
cv::COLORMAP_COOL |
酷炫的彩色地图 |
cv::COLORMAP_HSV |
HSV(色调、饱和度、值)色彩图 |
cv::COLORMAP_PINK |
粉色地图 |
cv::COLORMAP_HOT |
热门彩色地图 |
cv::COLORMAP_PARULA |
帕鲁拉彩图 |
cv::COLORMAP_MAGMA |
岩浆颜色图 |
cv::COLORMAP_INFERNO |
地狱彩色地图 |
cv::COLORMAP_PLASMA |
等离子彩图 |
cv::COLORMAP_VIRIDIS |
Viridis 彩色地图 |
cv::COLORMAP_CIVIDIS |
Cividis 彩色地图 |
cv::COLORMAP_TWILIGHT |
黄昏彩色地图 |
cv::COLORMAP_TWILIGHT_SHIFTED |
偏移的曙光色彩图 |
cv::COLORMAP_TURBO |
涡轮颜色图 |
cv::COLORMAP_DEEPGREEN |
深绿色彩图 |
这些颜色图通常用于用不同的颜色表示可视化数据。
常见问题
Ultralytics YOLOv8 如何生成热图及其优势是什么?
Ultralytics YOLOv8 通过将复杂数据转换为彩色编码矩阵,不同色调代表数据强度,从而生成热图。热图更容易直观地显示数据中的模式、相关性和异常情况。较暖的色调表示较高的数值,而较冷的色调表示较低的数值。其主要优点包括直观可视化数据分布、高效模式检测和增强决策空间分析。有关详细信息和配置选项,请参阅热图配置部分。
能否使用Ultralytics YOLOv8 同时执行对象跟踪和生成热图?
是的,Ultralytics YOLOv8 支持同时进行对象跟踪和热图生成。这可以通过其 Heatmap
解决方案集成了对象跟踪模型。为此,您需要初始化热图对象并使用YOLOv8 的跟踪功能。下面是一个简单的示例:
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
heatmap_obj = solutions.Heatmap(colormap=cv2.COLORMAP_PARULA, view_img=True, shape="circle", names=model.names)
while cap.isOpened():
success, im0 = cap.read()
if not success:
break
tracks = model.track(im0, persist=True, show=False)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
cv2.imshow("Heatmap", im0)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
如需进一步指导,请查看跟踪模式页面。
Ultralytics YOLOv8 热图与 OpenCV 或 Matplotlib 等其他数据可视化工具有何不同?
Ultralytics YOLOv8 热图专为与其对象检测和跟踪模型集成而设计,为实时数据分析提供了端到端的解决方案。与 OpenCV 或 Matplotlib 等通用可视化工具不同,YOLOv8 热图针对性能和自动处理进行了优化,支持持续跟踪、衰减系数调整和实时视频叠加等功能。有关YOLOv8 独特功能的更多信息,请访问Ultralytics YOLOv8 简介。
如何使用Ultralytics YOLOv8 在热图中只显示特定对象类别?
您可以通过在 track()
YOLO 方法。例如,如果您只想可视化汽车和人(假设它们的类指数分别为 0 和 2),您可以设置 classes
参数。
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
heatmap_obj = solutions.Heatmap(colormap=cv2.COLORMAP_PARULA, view_img=True, shape="circle", names=model.names)
classes_for_heatmap = [0, 2] # Classes to visualize
while cap.isOpened():
success, im0 = cap.read()
if not success:
break
tracks = model.track(im0, persist=True, show=False, classes=classes_for_heatmap)
im0 = heatmap_obj.generate_heatmap(im0, tracks)
cv2.imshow("Heatmap", im0)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
企业在数据分析中为什么要选择Ultralytics YOLOv8 来生成热图?
Ultralytics YOLOv8 高级对象检测和实时热图生成的无缝集成,使其成为希望更有效地实现数据可视化的企业的理想选择。它的主要优势包括直观的数据分布可视化、高效的模式检测和增强的空间分析,以便更好地做出决策。此外,YOLOv8 的尖端功能(如持续跟踪、可定制的颜色图以及对各种导出格式的支持)使其在综合数据分析方面优于TensorFlow 和 OpenCV 等其他工具。了解有关商业应用的更多信息,请访问Ultralytics Plans。