1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
18 // Erping Pang, pang_er_ping@163.com
19 // Xiaopeng Fu, fuxiaopeng2222@163.com
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
24 // * Redistribution's of source code must retain the above copyright notice,
25 // this list of conditions and the following disclaimer.
27 // * Redistribution's in binary form must reproduce the above copyright notice,
28 // this list of conditions and the following disclaimer in the documentation
29 // and/or other oclMaterials provided with the distribution.
31 // * The name of the copyright holders may not be used to endorse or promote products
32 // derived from this software without specific prior written permission.
34 // This software is provided by the copyright holders and contributors "as is" and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
47 #include "test_precomp.hpp"
51 using namespace cvtest;
52 using namespace testing;
56 #define OCL_KMEANS_USE_INITIAL_LABELS 1
57 #define OCL_KMEANS_PP_CENTERS 2
59 PARAM_TEST_CASE(Kmeans, int, int, int)
65 ocl::oclMat d_src, d_dists;
68 ocl::oclMat d_labels, d_centers;
74 rng = TS::ptr()->get_rng();
76 // MWIDTH=256, MHEIGHT=256. defined in utility.hpp
77 cv::Size size = cv::Size(MWIDTH, MHEIGHT);
78 src.create(size, type);
80 const int max_neighbour = MHEIGHT / K - 1;
81 CV_Assert(K <= MWIDTH);
82 for(int i = 0; i < K; i++ )
84 Mat center_row_header = src.row(row_idx);
85 center_row_header.setTo(0);
86 int nchannel = center_row_header.channels();
87 for(int j = 0; j < nchannel; j++)
88 center_row_header.at<float>(0, i*nchannel+j) = 50000.0;
90 for(int j = 0; (j < max_neighbour) ||
91 (i == K-1 && j < max_neighbour + MHEIGHT%K); j ++)
93 Mat cur_row_header = src.row(row_idx + 1 + j);
94 center_row_header.copyTo(cur_row_header);
95 Mat tmpmat = randomMat(rng, cur_row_header.size(), cur_row_header.type(), -200, 200, false);
96 cur_row_header += tmpmat;
98 row_idx += 1 + max_neighbour;
104 if(flags & KMEANS_USE_INITIAL_LABELS)
106 // inital a given labels
107 labels.create(src.rows, 1, CV_32S);
108 int *label = labels.ptr<int>();
109 for(int i = 0; i < src.rows; i++)
110 label[i] = rng.uniform(0, K);
111 d_labels.upload(labels);
115 for(int j = 0; j < LOOP_TIMES; j++)
117 kmeans(src, K, labels,
118 TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 100, 0),
121 ocl::kmeans(d_src, K, d_labels,
122 TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 100, 0),
123 1, flags, d_centers);
125 Mat dd_labels(d_labels);
126 Mat dd_centers(d_centers);
127 if(flags & KMEANS_USE_INITIAL_LABELS)
129 EXPECT_MAT_NEAR(labels, dd_labels, 0);
130 EXPECT_MAT_NEAR(centers, dd_centers, 1e-3);
135 for(int i = 0; i < K; i++)
137 // verify lables with ground truth resutls
138 int label = labels.at<int>(row_idx);
139 int header_label = dd_labels.at<int>(row_idx);
140 for(int j = 0; (j < MHEIGHT/K)||(i == K-1 && j < MHEIGHT/K+MHEIGHT%K); j++)
142 ASSERT_NEAR(labels.at<int>(row_idx+j), label, 0);
143 ASSERT_NEAR(dd_labels.at<int>(row_idx+j), header_label, 0);
147 float *center = centers.ptr<float>(label);
148 float *header_center = dd_centers.ptr<float>(header_label);
149 for(int t = 0; t < centers.cols; t++)
150 ASSERT_NEAR(center[t], header_center[t], 1e-3);
152 row_idx += MHEIGHT/K;
157 INSTANTIATE_TEST_CASE_P(OCL_ML, Kmeans, Combine(
159 Values(CV_32FC1, CV_32FC2, CV_32FC4),
160 Values(OCL_KMEANS_USE_INITIAL_LABELS/*, OCL_KMEANS_PP_CENTERS*/)));