-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_images.cpp
376 lines (336 loc) · 10.3 KB
/
ml_images.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <ml_images.h>
#include <iostream>
using namespace cv;
using namespace std;
string rootdir = "D:/opencv-4.2.0/opencv/sources/samples/data/";
void MLoperatorsDemo::kmeans_segmentation_demo(Mat &image) {
Scalar colorTab[] = {
Scalar(0, 0, 255),
Scalar(0, 255, 0),
Scalar(255, 0, 0),
Scalar(0, 255, 255),
Scalar(255, 0, 255)
};
int width = image.cols;
int height = image.rows;
int dims = image.channels();
// 初始化定义
int sampleCount = width*height;
int clusterCount = 5;
Mat labels;
Mat centers;
// RGB 数据转换到样本数据
Mat sample_data = image.reshape(3, sampleCount);
Mat data;
sample_data.convertTo(data, CV_32F);
// 运行K-Means
TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1);
kmeans(data, clusterCount, labels, criteria, clusterCount, KMEANS_PP_CENTERS, centers);
// 显示图像分割结果
int index = 0;
Mat result = Mat::zeros(image.size(), image.type());
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
index = row*width + col;
int label = labels.at<int>(index, 0);
result.at<Vec3b>(row, col)[0] = colorTab[label][0];
result.at<Vec3b>(row, col)[1] = colorTab[label][1];
result.at<Vec3b>(row, col)[2] = colorTab[label][2];
}
}
imshow("KMeans图像分割", result);
}
void MLoperatorsDemo::mainColorComponents(Mat &image) {
int width = image.cols;
int height = image.rows;
int dims = image.channels();
// 初始化定义
int sampleCount = width*height;
int clusterCount = 5;
Mat labels;
Mat centers;
// RGB 数据转换到样本数据
Mat sample_data = image.reshape(3, sampleCount);
Mat data;
sample_data.convertTo(data, CV_32F);
// 运行K-Means
TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1);
kmeans(data, clusterCount, labels, criteria, clusterCount, KMEANS_PP_CENTERS, centers);
Mat card = Mat::zeros(Size(width, 50), CV_8UC3);
vector<float> clusters(clusterCount);
// 生成色卡比率
for (int i = 0; i < labels.rows; i++) {
clusters[labels.at<int>(i, 0)]++;
}
for (int i = 0; i < clusters.size(); i++) {
clusters[i] = clusters[i] / sampleCount;
}
int x_offset = 0;
// 绘制色卡
for (int x = 0; x < clusterCount; x++) {
Rect rect;
rect.x = x_offset;
rect.y = 0;
rect.height = 50;
rect.width = round(clusters[x] * width);
x_offset += rect.width;
int b = centers.at<float>(x, 0);
int g = centers.at<float>(x, 1);
int r = centers.at<float>(x, 2);
rectangle(card, rect, Scalar(b, g, r), -1, 8, 0);
}
imshow("色卡生成", card);
}
void MLoperatorsDemo::knn_digit_train(Mat &image) {
Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);
// 分割为5000个cells
Mat images = Mat::zeros(5000, 400, CV_8UC1);
Mat labels = Mat::zeros(5000, 1, CV_8UC1);
int index = 0;
Rect roi;
roi.x = 0;
roi.height = 1;
roi.width = 400;
for (int row = 0; row < 50; row++) {
int label = row / 5;
int offsety = row * 20;
for (int col = 0; col < 100; col++) {
int offsetx = col * 20;
Mat digit = Mat::zeros(Size(20, 20), CV_8UC1);
for (int sr = 0; sr < 20; sr++) {
for (int sc = 0; sc < 20; sc++) {
digit.at<uchar>(sr, sc) = gray.at<uchar>(sr + offsety, sc + offsetx);
}
}
Mat one_row = digit.reshape(1, 1);
roi.y = index;
one_row.copyTo(images(roi));
labels.at<uchar>(index, 0) = label;
index++;
}
}
printf("load sample hand-writing data...\n");
// 转换为浮点数
images.convertTo(images, CV_32FC1);
labels.convertTo(labels, CV_32SC1);
printf("load sample hand-writing data...\n");
// 开始KNN训练
printf("Start to knn train...\n");
Ptr<ml::KNearest> knn = ml::KNearest::create();
knn->setDefaultK(5);
knn->setIsClassifier(true);
Ptr<ml::TrainData> tdata = ml::TrainData::create(images, ml::ROW_SAMPLE, labels);
knn->train(tdata);
knn->save("D:/vcworkspaces/knn_knowledge.yml");
printf("Finished KNN...\n");
}
void MLoperatorsDemo::knn_digit_test() {
// real test it
Mat t1 = imread(rootdir + "knn_01.png", IMREAD_GRAYSCALE);
Mat t2 = imread(rootdir + "knn_02.png", IMREAD_GRAYSCALE);
namedWindow("t1", WINDOW_FREERATIO);
namedWindow("t2", WINDOW_FREERATIO);
imshow("t1", t1);
imshow("t2", t2);
Mat m1, m2;
resize(t1, m1, Size(20, 20));
resize(t2, m2, Size(20, 20));
Mat testdata = Mat::zeros(2, 400, CV_8UC1);
Mat testlabels = Mat::zeros(2, 1, CV_32SC1);
Rect rect;
rect.x = 0;
rect.y = 0;
rect.height = 1;
rect.width = 400;
Mat one = m1.reshape(1, 1);
Mat two = m2.reshape(1, 1);
one.copyTo(testdata(rect));
rect.y = 1;
two.copyTo(testdata(rect));
testlabels.at<int>(0, 0) = 1;
testlabels.at<int>(1, 0) = 2;
testdata.convertTo(testdata, CV_32F);
// 加载KNN分类器
Ptr<ml::KNearest> knn = Algorithm::load<ml::KNearest>("D:/vcworkspaces/knn_knowledge.yml");
Mat result;
knn->findNearest(testdata, 5, result);
for (int i = 0; i< result.rows; i++) {
int predict = result.at<float>(i, 0);
printf("knn t%d predict : %d, actual label :%d \n", (i + 1), predict, testlabels.at<int>(i, 0));
}
}
void MLoperatorsDemo::svm_digit_train(Mat &image) {
Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);
// 分割为5000个cells
Mat images = Mat::zeros(5000, 400, CV_8UC1);
Mat labels = Mat::zeros(5000, 1, CV_8UC1);
int index = 0;
Rect roi;
roi.x = 0;
roi.height = 1;
roi.width = 400;
for (int row = 0; row < 50; row++) {
int label = row / 5;
int offsety = row * 20;
for (int col = 0; col < 100; col++) {
int offsetx = col * 20;
Mat digit = Mat::zeros(Size(20, 20), CV_8UC1);
for (int sr = 0; sr < 20; sr++) {
for (int sc = 0; sc < 20; sc++) {
digit.at<uchar>(sr, sc) = gray.at<uchar>(sr + offsety, sc + offsetx);
}
}
Mat one_row = digit.reshape(1, 1);
roi.y = index;
one_row.copyTo(images(roi));
labels.at<uchar>(index, 0) = label;
printf("index : %d, label : %d \n", index, label);
index++;
}
}
// 转换为浮点数
images.convertTo(images, CV_32FC1);
labels.convertTo(labels, CV_32SC1);
printf("load sample hand-writing data...\n");
Ptr<ml::SVM> svm = ml::SVM::create();
svm->setGamma(0.02);
svm->setC(0.5);
svm->setKernel(ml::SVM::LINEAR);
svm->setType(ml::SVM::C_SVC);
printf("Starting SVM Model Training...\n");
Ptr<ml::TrainData> tdata = ml::TrainData::create(images, ml::ROW_SAMPLE, labels);
svm->train(tdata);
svm->save("D:/vcworkspaces/svm_knowledge.yml");
printf("Finished Training SVM...\n");
}
void MLoperatorsDemo::svm_digit_test() {
// real test it
Mat t1 = imread(rootdir + "knn_01.png", IMREAD_GRAYSCALE);
Mat t2 = imread(rootdir + "knn_02.png", IMREAD_GRAYSCALE);
namedWindow("t1", WINDOW_FREERATIO);
namedWindow("t2", WINDOW_FREERATIO);
imshow("t1", t1);
imshow("t2", t2);
Mat m1, m2;
resize(t1, m1, Size(20, 20));
resize(t2, m2, Size(20, 20));
Mat testdata = Mat::zeros(2, 400, CV_8UC1);
Mat testlabels = Mat::zeros(2, 1, CV_32SC1);
Rect rect;
rect.x = 0;
rect.y = 0;
rect.height = 1;
rect.width = 400;
Mat one = m1.reshape(1, 1);
Mat two = m2.reshape(1, 1);
one.copyTo(testdata(rect));
rect.y = 1;
two.copyTo(testdata(rect));
testlabels.at<int>(0, 0) = 1;
testlabels.at<int>(1, 0) = 2;
testdata.convertTo(testdata, CV_32F);
// 加载SVM分类器
Ptr<ml::SVM> svm = ml::StatModel::load<ml::SVM>("D:/vcworkspaces/svm_knowledge.yml");
Mat result;
svm->predict(testdata, result);
for (int i = 0; i < result.rows; i++)
{
int predict = result.at<float>(i, 0);
printf("svm t%d predict : %d, actual label :%d \n", (i + 1), predict, testlabels.at<int>(i, 0));
}
}
void MLoperatorsDemo::get_hog_descriptor(Mat &image, vector<float> &desc) {
HOGDescriptor hog;
int h = image.rows;
int w = image.cols;
float rate = 64.0 / w;
Mat img, gray;
resize(image, img, Size(64, int(rate*h)));
cvtColor(img, gray, COLOR_BGR2GRAY);
Mat result = Mat::zeros(Size(64, 128), CV_8UC1);
result = Scalar(127);
Rect roi;
roi.x = 0;
roi.width = 64;
roi.y = (128 - gray.rows) / 2;
roi.height = gray.rows;
gray.copyTo(result(roi));
hog.compute(result, desc, Size(8, 8), Size(0, 0));
}
void MLoperatorsDemo::train_ele_watch(std::string positive_dir, std::string negative_dir) {
// 创建变量
Mat trainData = Mat::zeros(Size(3780, 26), CV_32FC1);
Mat labels = Mat::zeros(Size(1, 26), CV_32SC1);
vector<string> images;
glob(positive_dir, images);
int pos_num = images.size();
// 生成正负样本数据
for (int i = 0; i < images.size(); i++) {
Mat image = imread(images[i].c_str());
vector<float> fv;
get_hog_descriptor(image, fv);
printf("image path : %s, feature data length: %d \n", images[i].c_str(), fv.size());
for (int j = 0; j < fv.size(); j++) {
trainData.at<float>(i, j) = fv[j];
}
labels.at<int>(i, 0) = 1;
}
images.clear();
glob(negative_dir, images);
for (int i = 0; i < images.size(); i++) {
Mat image = imread(images[i].c_str());
vector<float> fv;
get_hog_descriptor(image, fv);
printf("image path : %s, feature data length: %d \n", images[i].c_str(), fv.size());
for (int j = 0; j < fv.size(); j++) {
trainData.at<float>(i + pos_num, j) = fv[j];
}
labels.at<int>(i + pos_num, 0) = -1;
}
// 训练SVM仪表分类器
printf("\n start SVM training... \n");
Ptr< ml::SVM > svm = ml::SVM::create();
svm->setKernel(ml::SVM::LINEAR);
svm->setC(2.0);
svm->setType(ml::SVM::C_SVC);
svm->train(trainData, ml::ROW_SAMPLE, labels);
clog << "...[done]" << endl;
// save xml
svm->save("D:/vcworkspaces/svm_hog_elec.yml");
}
void MLoperatorsDemo::hog_svm_detector_demo(Mat &image) {
// 创建HOG与加载SVM训练数据
HOGDescriptor hog;
Ptr<ml::SVM> svm = ml::SVM::load("D:/vcworkspaces/svm_hog_elec.yml");
Mat sv = svm->getSupportVectors();
Mat alpha, svidx;
double rho = svm->getDecisionFunction(0, alpha, svidx);
// 构建detector
vector<float> svmDetector;
svmDetector.clear();
svmDetector.resize(sv.cols + 1);
for (int j = 0; j < sv.cols; j++) {
svmDetector[j] = -sv.at<float>(0, j);
}
svmDetector[sv.cols] = (float)rho;
hog.setSVMDetector(svmDetector);
vector<Rect> objects;
hog.detectMultiScale(image, objects, 0.1, Size(8, 8), Size(32, 32), 1.25);
for (int i = 0; i < objects.size(); i++) {
rectangle(image, objects[i], Scalar(0, 0, 255), 2, 8, 0);
}
namedWindow("SVM+HOG对象检测演示", WINDOW_FREERATIO);
imshow("SVM+HOG对象检测演示", image);
}
int main(int argc, char** argv) {
Mat image = imread(rootdir + "/elec_watch/test/scene_08.jpg");
imshow("image", image);
MLoperatorsDemo mler;
// mler.train_ele_watch(rootdir + "/elec_watch/positive", rootdir + "/elec_watch/negative");
mler.hog_svm_detector_demo(image);
waitKey(0);
destroyAllWindows();
return 0;
}