/
thermalcam.cpp
98 lines (82 loc) · 2.3 KB
/
thermalcam.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <cstdint>
#include <algorithm>
#include <filesystem>
inline double round2(double d)
{
return round(d*100)/100;
}
inline double get_temp(int16_t pixel)
{
return round2((pixel / 64) - 273.15);
}
int main(int, char**)
{
int w, h;
int scale = 3;
cv::ColormapTypes colorMap = cv::COLORMAP_JET;
double min, max, center;
double minVal, maxVal;
cv::Point minPoint;
cv::Point maxPoint;
cv::Mat fullFrame;
cv::Mat imageData;
cv::Mat thermalData;
cv::VideoCapture cap;
for(std::filesystem::directory_entry const& dir :
std::filesystem::directory_iterator("/sys/class/video4linux"))
{
std::string dirname = dir.path().filename();
if(dirname.substr(0, 5) != "video")
{
continue;
}
std::filesystem::directory_entry uevent(dir.path() / "device" / "uevent");
std::ifstream file(uevent.path());
if(file.is_open())
{
std::string line;
while(std::getline(file, line))
{
if(line.substr(0, 16) == "PRODUCT=bda/5830")
{
// std::cout << "Trying " << "/dev/" + dirname << std::endl;
cap = cv::VideoCapture("/dev/" + dirname, cv::CAP_V4L2);
break;
}
}
}
if(cap.isOpened())
{
break;
}
}
if(!cap.isOpened())
{
std::cerr << "ERROR: Failed to open camera." << std::endl;
return 1;
}
cap.set(cv::CAP_PROP_CONVERT_RGB, false);
while(cap.read(fullFrame))
{
w = fullFrame.cols;
h = fullFrame.rows / 2;
imageData = fullFrame.rowRange(0, h);
thermalData = cv::Mat(h, w, CV_16SC1, fullFrame.row(h).data);
cv::minMaxLoc(thermalData, &minVal, &maxVal, &minPoint, &maxPoint);
min = get_temp(minVal);
max = get_temp(maxVal);
center = get_temp(thermalData.at<int16_t>(w/2, h/2));
std::cout << "minMaxIdx: " << "min: " << min << " max: " << max << " center: " << center << std::endl;
std::cout << "min pos: " << minPoint.x << "x" << minPoint.y
<< " max pos: " << maxPoint.x << "x" << maxPoint.y << std::endl;
cv::resize(imageData, imageData, {w * scale, h * scale});
cv::cvtColor(imageData, imageData, cv::COLOR_YUV2BGR_YUYV);
cv::applyColorMap(imageData, imageData, colorMap);
cv::imwrite("out.png", imageData);
break;
}
return 0;
}