-
Notifications
You must be signed in to change notification settings - Fork 1
/
ColorAnalysis.cpp
279 lines (218 loc) · 6.28 KB
/
ColorAnalysis.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "D:\Solve-Puzzle-Not-Puzzled\main.h"
#include "ColorInfo.h"
vector<ColorInfo> ImgColorDescriptor(Mat img, int row, int col, int colorNumber);
vector <vector<float>> ColorSort(vector<ColorInfo> origin, ColorInfo piece);
ColorInfo AnalysisColor(Mat img,int);
ColorInfo AnalysisColorPNG(Mat img,int);
vector <vector<float>> ColorAnalysis(Mat, Mat, int, int,int);
vector<ColorInfo> OriginColorInfo;
void ShowImageColorInfo(Mat img,vector<ColorInfo> color,int row,int col){
int i=0;
Mat part;
vector<ColorInfo>::iterator it;
for (it = color.begin(); it != color.end(); ++it)
{
//printf("%d-%d\n", i / col + 1, i%col+1);
cout << i << " " << endl;
it->ShowColorInfo();
//part = img(Range((int)((i / col)*(float)img.rows / row), (int)((float)img.rows / row*((i / col) + 1))), Range((int)((i%col)*(float)img.cols / col), (int)((float)img.cols / col*((i%col) + 1)))); //·|¶V¨Ó¶V¤p
//namedWindow("part", CV_WINDOW_AUTOSIZE);
//imshow("part", part);
//printf("\n\n");
i++;
//waitKey(1);
}
}
ColorInfo AnalysisColorPNG(Mat img, int colorNumber){
int i, j, m;
int N = colorNumber; /*class the color into N colors*/
int h,size=0;
vector<float> colorArray(N, 0);
map<int, float> colorPrortion;
for (i = 0; i < img.rows; ++i){
Vec3b *p = img.ptr<Vec3b>(i);
for (j = 0; j < img.cols; ++j){
if (p[j][1] < COLOR_RANGE / 6){
if (p[j][2] < COLOR_RANGE / 3){ //black
h = N-1;
}
else if (p[j][2] > 2 * COLOR_RANGE / 3){ //white
h = N-3;
}
else{ //gray
h = N-2;
}
}
else{
h = (float)p[j][0] / COLOR_RANGE * N;
}
if (p[j][1] == 0 && p[j][2] == 0){
//cout << "none";
}
else{
colorArray[h]++;
size++;
}
}
}
for (i = 0; i < colorArray.size(); i++){
colorArray[i] /= size;
}
ColorInfo colorInfo(colorArray);
colorInfo.ShowColorInfo();
return colorInfo;
}
ColorInfo AnalysisColor(Mat img, int colorNumber){
int i, j,m;
int N = colorNumber; /*class the color into N colors*/
int h;
vector<float> colorArray(N,0);
map<int, float> colorPrortion;
for (i = 0; i < img.rows; ++i){
Vec3b *p = img.ptr<Vec3b>(i);
for (j = 0; j < img.cols; ++j){
if (p[j][1] < COLOR_RANGE / 6){
if (p[j][2] < COLOR_RANGE / 3){ //black
h = N-1;
}
else if (p[j][2] > 2 * COLOR_RANGE / 3){ //white
h = N-3;
}
else{ //gray
h = N-2;
}
}
else{
h = (float)p[j][0] / COLOR_RANGE * N;
}
colorArray[h]++;
}
}
for (i = 0; i < colorArray.size(); i++){
colorArray[i] /= (img.rows*img.cols);
}
ColorInfo colorInfo(colorArray);
return colorInfo;
}
void PrintColorImage(Mat img, int channel){
int i, j;
int h;
for (i = 0; i < img.rows; ++i){
Vec3b *p = img.ptr<Vec3b>(i);
for (j = 0; j < img.cols; ++j){
h = (float)p[j][channel]/255*6;
printf("%d ", h);
}
printf("\n");
}
}
void PrintNonColorImage(Mat img){
int i, j;
for (i = 0; i < img.rows; ++i){
uchar *p = img.ptr<uchar>(i);
for (j = 0; j < img.cols; ++j){
printf("%3d ", p[j]);
}
printf("\n");
}
}
float ColorCompare(ColorInfo c1, ColorInfo c2,int colorNumber){
int i;
int size = colorNumber;
float compare = 0.0,a;
for (i = 0; i < size; i++){
a = abs(c1.get_colorProportion(i) - c2.get_colorProportion(i));
compare = compare + a;
}
return compare;
}
ColorInfo PieceColorDescriptor(Mat img,int colorNumber){
Mat hsv;
cvtColor(img, hsv, CV_BGR2HSV_FULL);
ColorInfo pieceColor = AnalysisColor(hsv, colorNumber);
pieceColor.ShowColorInfo();
return pieceColor;
}
vector<ColorInfo> ImgColorDescriptor(Mat img, int row, int col,int colorNumber){
Mat hsv;
vector<Mat> hsv_split, part_split;
Mat part,part2;
int i, j;
int width, height;
width = hsv.cols / col;
height = hsv.rows / row;
cvtColor(img, hsv, CV_BGR2HSV_FULL);
OriginColorInfo.clear();
//cout << hsv.type() << endl;
for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
part = hsv(Range(hsv.rows / row*i, hsv.rows / row*(i + 1)), Range(hsv.cols / col*j, hsv.cols / col*(j + 1)));
part2 = img(Range(img.rows / row*i, img.rows / row*(i + 1)), Range(img.cols / col*j, img.cols / col*(j + 1)));
OriginColorInfo.push_back(AnalysisColor(part, colorNumber));
/*namedWindow("part", CV_WINDOW_AUTOSIZE);
imshow("part", part2);
waitKey(1);*/
}
}
// ShowImageColorInfo(img, OriginColorInfo, row, col);
/*split(hsv, hsv_split);
split(part, part_split);
namedWindow("ì¹Ï", CV_WINDOW_AUTOSIZE);
namedWindow("HSV", CV_WINDOW_AUTOSIZE);
namedWindow("part", CV_WINDOW_AUTOSIZE);
imshow("ì¹Ï",img);
imshow("HSV",hsv);
imshow("part", part);
namedWindow("hue", CV_WINDOW_AUTOSIZE);
namedWindow("saturation", CV_WINDOW_AUTOSIZE);
namedWindow("value", CV_WINDOW_AUTOSIZE);
imshow("hue", hsv_split.at(0));
imshow("saturation", hsv_split.at(1));
imshow("value", hsv_split.at(2));
*/
//PrintColorImage(part, 0);
return OriginColorInfo;
}
vector <vector<float>> ColorCompareArray(vector<ColorInfo> origin, ColorInfo piece,int colorNumber){
vector <vector<float>> color_array;
vector<float> colorInfo;
int i;
float compare, max = 0.0, min = 100.0;
for (i = 0; i < origin.size(); i++)
{
compare = ColorCompare(origin[i], piece, colorNumber);
colorInfo.push_back((float)i);
colorInfo.push_back(compare);
color_array.push_back(colorInfo);
colorInfo.clear();
if (compare > max)
max = compare;
if (compare < min)
min = compare;
//cout <<"color " <<color_array[i][0] << " " << color_array[i][1] << endl;
}
for (i = 0; i < color_array.size(); i++)
{
color_array[i][1] = (color_array[i][1] - min)*0.99 / (max - min) + 0.01;
}
return color_array;
}
vector <vector<float>> ColorSort(vector <vector<float>> color_sort_array){
int i;
sort(color_sort_array.begin(), color_sort_array.end(), sortCompare);
for (i = 0; i < color_sort_array.size(); i++)
{
cout <<i+1<<" color sort " <<color_sort_array[i][0] << " : " << color_sort_array[i][1] << endl;
}
return color_sort_array;
}
vector <vector<float>> ColorAnalysis(Mat original, Mat piece, int row, int col,int colorNumber){
vector <vector<float>> colorInfo;
Mat piece_hsv;
ColorInfo pieceColor;
cvtColor(piece, piece_hsv, CV_BGR2HSV_FULL);
pieceColor = AnalysisColorPNG(piece_hsv, colorNumber);
colorInfo = ColorCompareArray(OriginColorInfo, pieceColor, colorNumber);
ColorSort(colorInfo);
return colorInfo;
}