forked from oreillymedia/Learning-OpenCV-3_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_20-02.cpp
93 lines (77 loc) · 3.12 KB
/
example_20-02.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
//Example 20-02. Using the Mahalanobis distance for classification
#include <opencv2/opencv.hpp>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
const int CLUSTER_COUNT = 4;
const int SAMPLE_COUNT = 500;
const cv::Scalar colorTab[] = {
cv::Scalar( 0, 0, 255 ),
cv::Scalar( 0, 255, 0 ),
cv::Scalar( 255, 0, 0 ),
cv::Scalar( 255, 0, 255 ),
cv::Scalar( 0, 255, 255 )
};
static void help(char* argv[]) {
cout << "\nThis program demonstrates using the Mahalanobis distance for classification.\n"
" It generates an image with random points, uses kmeans clustering.\n"
" And then uses the Mahalanobis distance for classification of new points (colors) .\n"
"Usage:\n"
<< argv[0] << "\n\n"
<< "ESC to quit\n\n"<< endl;
}
int main(int argc, char** argv) {
cv::Mat img(500, 500, CV_8UC3, cv::Scalar::all(0));
cv::Mat points(SAMPLE_COUNT, 1, CV_32FC2);
cv::RNG rng(time(NULL));
help(argv);
rng.fill(points, cv::RNG::UNIFORM, cv::Scalar(0, 0), cv::Scalar(img.cols, img.rows));
cv::Mat labels;
kmeans(points, CLUSTER_COUNT, labels,
cv::TermCriteria(cv::TermCriteria::EPS | cv::TermCriteria::COUNT,
10, 1.0),
3,
cv::KMEANS_PP_CENTERS
);
vector<cv::Mat> clusters(CLUSTER_COUNT);
for(int i = 0; i < SAMPLE_COUNT; i++) {
int clusterIdx = labels.at<int>(i);
cv::Point ipt = points.at<cv::Point2f>(i);
cv::Mat sample(1, 2, CV_32FC1);
sample.at<float>(0, 0) = ipt.x;
sample.at<float>(0, 1) = ipt.y;
clusters[clusterIdx].push_back(sample);
cv::circle(img, ipt, 2, colorTab[clusterIdx], cv::FILLED, cv::LINE_AA);
}
cv::namedWindow("Example 20-02");
cv::imshow("Example 20-02", img);
vector<cv::Mat> covarMats(CLUSTER_COUNT);
vector<cv::Mat> means(CLUSTER_COUNT);
for(int i = 0; i < CLUSTER_COUNT; i++) {
cv::calcCovarMatrix(clusters[i], covarMats[i], means[i],
CV_COVAR_NORMAL | CV_COVAR_ROWS, 5);
}
cout << "Press any button to classify the next point!\n"
<< "Press ESC to exit." << endl;
for(;;) {
char key = (char)cv::waitKey();
if( key == 27 ) break;
cv::Mat newPoint(1, 2, CV_32FC1);
newPoint.at<float>(0, 0) = rng.uniform(0, img.cols);
newPoint.at<float>(0, 1) = rng.uniform(0, img.rows);
vector<float> mahalanobisDistance(CLUSTER_COUNT);
for(int i = 0; i < CLUSTER_COUNT; i++) {
mahalanobisDistance[i] = cv::Mahalanobis(newPoint, means[i],
covarMats[i]);
}
int clusterIdx = std::distance( mahalanobisDistance.begin(),
min_element(mahalanobisDistance.begin(),
mahalanobisDistance.end()));
cv::circle(img, newPoint.at<cv::Point2f>(0), 5, colorTab[clusterIdx],
cv::FILLED, cv::LINE_AA);
cv::imshow("Example 20-02", img);
}
cv::destroyAllWindows();
return 0;
}