forked from aclapes/segmenthreetion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThermalFeatureExtractor.cpp
executable file
·125 lines (94 loc) · 3.8 KB
/
ThermalFeatureExtractor.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// ThermalFeatureExtractor.cpp
// Segmenthreetion
//
// Created by Albert Clapés on 24/05/13.
// Copyright (c) 2013 Albert Clapés. All rights reserved.
//
#include "FeatureExtractor.h"
#include "ThermalFeatureExtractor.h"
#include <opencv2/opencv.hpp>
#include <pcl/point_types.h>
#include <pcl/features/normal_3d.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/cloud_viewer.h>
#include <boost/thread.hpp>
#include <boost/timer.hpp>
ThermalFeatureExtractor::ThermalFeatureExtractor()
: FeatureExtractor()
{ }
ThermalFeatureExtractor::ThermalFeatureExtractor(ThermalParametrization tParam)
: FeatureExtractor(), m_ThermalParam(tParam)
{ }
void ThermalFeatureExtractor::setParam(ThermalParametrization thermalParam)
{
m_ThermalParam = thermalParam;
}
void ThermalFeatureExtractor::describe(ModalityGridData& data)
{
FeatureExtractor::describe(data);
}
void ThermalFeatureExtractor::describe(GridMat grid, GridMat gmask,
cv::Mat gvalidness, GridMat& gdescriptors)
{
cv::namedWindow("w");
for (int i = 0; i < grid.crows(); i++) for (int j = 0; j < grid.ccols(); j++)
{
cv::Mat tHist (1, m_ThermalParam.ibins + m_ThermalParam.oribins, CV_32F);
tHist.setTo(std::numeric_limits<float>::quiet_NaN());
if (gvalidness.at<unsigned char>(i,j))
{
cv::Mat& cell = grid.at(i,j);
cv::Mat& cellMask = gmask.at(i,j);
// Intensities descriptor
cv::Mat tIntensitiesHist;
describeThermalIntesities(cell, cellMask, tIntensitiesHist);
// Gradient orientation descriptor
cv::Mat tGradOrientsHist, dX, dY, p;
describeThermalGradOrients(cell, cellMask, tGradOrientsHist);
// Join both descriptors in a row
hconcat(tIntensitiesHist, tGradOrientsHist, tHist);
}
gdescriptors.at(i,j) = tHist; // row in a matrix of descriptors
}
}
void ThermalFeatureExtractor::describeThermalIntesities(cv::Mat cell, cv::Mat cellMask, cv::Mat & tIntensitiesHist)
{
int ibins = m_ThermalParam.ibins;
// Create an histogram for the cell region of blurred intensity values
int histSize[] = { (int) ibins };
int channels[] = { 0 }; // 1 channel, number 0
float tranges[] = { 0, 256 }; // thermal intensity values range: [0, 256)
const float* ranges[] = { tranges };
cv::Mat tmpHist;
calcHist(&cell, 1, channels, cellMask, tmpHist, 1, histSize, ranges, true, false);
transpose(tmpHist, tmpHist);
hypercubeNorm(tmpHist, tIntensitiesHist);
tmpHist.release();
}
void ThermalFeatureExtractor::describeThermalGradOrients(cv::Mat cell, cv::Mat cellMask, cv::Mat & tGradOrientsHist)
{
//cv::Mat cellSeg = cv::Mat::zeros(cell.rows, cell.cols, cell.type());
//cell.copyTo(cellSeg, cellMask);
// First derivatives
cv::Mat cellDervX, cellDervY;
cv::Mat aux = cell.clone();
cv::Sobel(aux, cellDervX, CV_32F, 1, 0);
cv::Sobel(aux, cellDervY, CV_32F, 0, 1);
cv::Mat cellGradOrients;
cv::phase(cellDervX, cellDervY, cellGradOrients, true);
int oribins = m_ThermalParam.oribins;
cv::Mat tmpHist = cv::Mat::zeros(1, oribins, cv::DataType<float>::type);
for (int i = 0; i < cell.rows; i++) for (int j = 0; j < cell.cols; j++)
{
if (cellMask.at<unsigned char>(i,j))
{
float g_x = cellDervX.at<float>(i,j);//unsigned short>(i,j);
float g_y = cellDervY.at<float>(i,j);
float orientation = cellGradOrients.at<float>(i,j);
int bin = ((int) floorf((orientation/360.0) * oribins));
tmpHist.at<float>(0, bin) += sqrtf(g_x * g_x + g_y * g_y);
}
}
hypercubeNorm(tmpHist, tGradOrientsHist);
}