/
ScalarColorFeature.cpp
149 lines (104 loc) · 3.6 KB
/
ScalarColorFeature.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "ScalarColorFeature.h"
#include "Image_proc_functions.h"
/**********************************************************************************************************************************************************/
/**
* @function ScalarColorFeature
* @param input vector<Mat>& image = Vector of Mat object ==> different channels as different Mat Object
* @brief This Function is used to calculate scalar color feature based on first principal component;
* All pixel values are projevted on 1st principal component, where all pixels are considered for computing PCA
* @output vector<float> data => Scalar data of size same as number of pixels in the given image
*/
vector<float> IITkgp_functions::ScalarColorFeature(vector<Mat>& image)
{
int nEigens = image.size();
int sp,i,j,k,l;
double temp_max_val;
PCA ImagePCA;
Mat ImageDataPCA (image[0].rows*image[0].cols,image.size(), CV_32FC1);
for (i = 0; i < image[0].rows; i++)
{
for (j = 0; j < image[0].cols; j++)
{
for(l=0;l<image.size();l++)
{
ImageDataPCA.at<float>((i*image[0].cols+j),l) = image[l].at<uchar>(i,j) * 1.0;
}
}
}
ImagePCA(ImageDataPCA,Mat(),CV_PCA_DATA_AS_ROW,nEigens);
float *ClrFeature;
ClrFeature = (float *)malloc(image.size()*sizeof(float));
for(l=0;l<image.size();l++)
{
ClrFeature[l] = ImagePCA.eigenvectors.at<float>(0,l);
}
vector<float> ScalarColorData;
for (i = 0; i < image[0].rows; i++)
{
for (j = 0; j < image[0].cols; j++)
{
float pdata = 0.0;
for(l=0;l<image.size();l++)
{
pdata = pdata + image[l].at<uchar>(i,j) * ClrFeature[l];
}
ScalarColorData.push_back(pdata);
}
}
return(ScalarColorData);
}
/**
* @function ScalarColorFeatureMasked
* @param input vector<Mat>& image = Vector of Mat object ==> different channels as different Mat Object;
* Mat MaskedImage = Mat object of 1 channel, and masked pixels have value '0' ;
* @brief This Function is used to calculate scalar color feature based on first principal component;
* All pixel values are projevted on 1st principal component, where masked pixels are considered for computing PCA
* @output vector<float> data => Scalar data of size same as number of masked pixels in the given image
*/
vector<float> IITkgp_functions::ScalarColorFeatureMasked(vector<Mat>& image, Mat MaskedImage)
{
int nEigens = image.size();
int sp,i,j,k,l;
int no_of_foregrnd_pix = NumberofForegroundPixel(MaskedImage);
PCA ImagePCA;
Mat ImageDataPCA (no_of_foregrnd_pix,image.size(), CV_32FC1);
k = 0;
for (i = 0; i < image[0].rows; i++)
{
for (j = 0; j < image[0].cols; j++)
{
if(MaskedImage.at<uchar>(i,j) == 0)
{
for(l=0;l<image.size();l++)
{
ImageDataPCA.at<float>(k,l) = image[l].at<uchar>(i,j) * 1.0;
}
k++;
}
}
}
ImagePCA(ImageDataPCA,Mat(),CV_PCA_DATA_AS_ROW,nEigens);
float *ClrFeature;
ClrFeature = (float *)malloc(image.size()*sizeof(float));
for(l=0;l<image.size();l++)
{
ClrFeature[l] = ImagePCA.eigenvectors.at<float>(0,l);
}
vector<float> ScalarColorData;
for (i = 0; i < image[0].rows; i++)
{
for (j = 0; j < image[0].cols; j++)
{
if(MaskedImage.at<uchar>(i,j) == 0)
{
float pdata = 0.0;
for(l=0;l<image.size();l++)
{
pdata = pdata + image[l].at<uchar>(i,j) * ClrFeature[l];
}
ScalarColorData.push_back(pdata);
}
}
}
return(ScalarColorData);
}