OverloadHotspotDetectionAlgorithm.cpp
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "OverloadHotspotDetectionAlgorithm.hpp"
#include <algorithm>
#include <opencv2/imgproc.hpp>
void Cpu::OverloadHotspotDetectionAlgorithm::setup(const cv::Size &frameSize,
const CVMatLoader &loader) {
currentFrameSize = frameSize;
hostFov = loader.asMat("/scene_model/FOV", CV_8UC1);
PfcSegmenter segmenter(loader, frameSize);
hostPfc = segmenter.segment();
cv::Mat zerosMask;
cv::compare(hostPfc, 0, zerosMask, cv::CMP_EQ);
hostPfc.setTo(MAX_TEMPERATURE, zerosMask);
cv::Mat model;
cv::cvtColor(loader.asMat("/scene_model/CAD", CV_8UC1), model,
cv::COLOR_GRAY2BGR);
std::vector<Contours> pfcContours;
segmenter.contour(pfcContours, hostPfcMask);
surfaceMap = std::make_unique<const SurfaceMap>(frameSize);
}
void Cpu::OverloadHotspotDetectionAlgorithm::handleFrame(
const cv::Mat &sourceFrame, unsigned long timestamp) {
hostFrame = sourceFrame;
/* Apply field-of-view mask */
cv::bitwise_and(hostFrame, hostFrame, hostInput, hostFov);
/* Convert Kelvin to Celsius */
cv::subtract(hostInput, KELVINS, hostTemperature);
/* Apply median filter */
medianBlur(hostTemperature, hostInput, 3);
/* Threshold overheating pixels */
cv::compare(hostInput, hostPfc, overheating, cv::CMP_GT);
currentHotspots.clear();
identifyHotspots(timestamp);
}
void Cpu::OverloadHotspotDetectionAlgorithm::identifyHotspots(
unsigned long timestamp) {
/* Analyse topologial structure */
Contours hotspotContours;
cv::findContours(overheating, hotspotContours, cv::RETR_TREE,
cv::CHAIN_APPROX_NONE);
for (size_t i = 0, size = hotspotContours.size();
i < size && i < blobsLimit; ++i) {
const auto &contour = hotspotContours[i];
/* Discard blobs below minimum area */
if (contour.size() >= pixelsThreshold) {
const int component = hostPfcMask.at<uchar>(contour[0]);
assert(component > 0);
Cpu::Hotspot hotspot(hostTemperature, timestamp, component, contour,
surfaceMap->at(contour));
/* Find corresponding blobs */
matchHotspot(hotspot);
currentHotspots.emplace_back(std::move(hotspot));
}
}
}
void Cpu::OverloadHotspotDetectionAlgorithm::matchHotspot(
const Cpu::Hotspot &hotspot) {
for (auto &persistentHotspot : uniqueHotspots) {
if (clusterCorrespondence.corresponds(persistentHotspot.mask,
hotspot.mask)) {
/* Merge matching blobs */
persistentHotspot.merge(hotspot);
return;
}
}
uniqueHotspots.emplace_back(hotspot);
}