-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureAnalysis.cpp
109 lines (87 loc) · 2.09 KB
/
TextureAnalysis.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
#include "TextureAnalysis.h"
#include "imageprocessing.h"
#include "opencv2/opencv.hpp"
#include <map>
using namespace cv;
TextureAnalysis::TextureAnalysis()
{
}
TextureAnalysis::~TextureAnalysis()
{
}
float TextureAnalysis::calculateEntropyOfRegion(Mat const & grayimage, int offset_x, int offset_y, int region)
{
int x, y, r2 = region >> 1;
float p, entropyVal = 0, count=0;
uint hist[255] = {0};
uint val, nMin = grayimage.cols*grayimage.rows, nMax = 0;
// get hist
for (x = offset_x - r2; x <= offset_x + r2; x++)
{
for (y = offset_y - r2; y <= offset_y + r2; y++)
{
if (x >= 0 && y >= 0 && x < grayimage.cols && y < grayimage.rows)
{
val = grayimage.at<uchar>(y, x);
hist[val]++;
nMin = MIN(val, nMin);
nMax = MAX(val, nMax);
count++;
}
}
}
// limit 0-255
nMax = MAX(0, MIN(254, nMax));
nMin = MAX(0, MIN(254, nMin));
for (int i=nMin; i<=nMax; i++)
{
if(hist[i]>0)
{
p = (float(hist[i]) / count);
entropyVal += p*log2(p);
}
}
return -entropyVal;
}
/*
float TextureAnalysis::calculateEntropyOfRegion(Mat const & grayimage, int offset_x, int offset_y, int region)
{
int x, y, r2 = region >> 1;
float p, entropyVal = 0, count=0;
MatConstIterator_<float> it;
std::map<uchar, float> hist;
uint val, nMin = grayimage.cols*grayimage.rows, nMax = 0;
// get hist
for(x=offset_x - r2; x <= offset_x + r2; x++)
for (y = offset_y - r2; y <= offset_y + r2; y++)
{
if (x >= 0 && y >= 0 && x < grayimage.cols && y < grayimage.rows)
{
val = grayimage.at<uchar>(y, x);
hist[val]++;
count++;
nMin = MIN(val, nMin);
nMax = MAX(val, nMax);
}
}
for (auto const &iterator : hist)
{
p = (iterator.second / count);
entropyVal += p*log2(p);
}
return -entropyVal;
}
*/
void TextureAnalysis::entropyFilt(Mat const & image, Mat & entropy, int region)
{
int x, y, r2 = region >> 1;
cv::Mat pad;
ImageProcessing::symmetricPadding(image, pad, r2);
for (int i = 0; i < entropy.cols; i++)
{
for (int j = 0; j < entropy.rows; j++)
{
entropy.at<float>(j, i) = calculateEntropyOfRegion(pad, i+r2, j+r2, region);
}
}
}