| 2696 | } |
| 2697 | |
| 2698 | double cv::kmeans( InputArray _data, int K, |
| 2699 | InputOutputArray _bestLabels, |
| 2700 | TermCriteria criteria, int attempts, |
| 2701 | int flags, OutputArray _centers ) |
| 2702 | { |
| 2703 | const int SPP_TRIALS = 3; |
| 2704 | Mat data0 = _data.getMat(); |
| 2705 | bool isrow = data0.rows == 1 && data0.channels() > 1; |
| 2706 | int N = !isrow ? data0.rows : data0.cols; |
| 2707 | int dims = (!isrow ? data0.cols : 1)*data0.channels(); |
| 2708 | int type = data0.depth(); |
| 2709 | |
| 2710 | attempts = std::max(attempts, 1); |
| 2711 | CV_Assert( data0.dims <= 2 && type == CV_32F && K > 0 ); |
| 2712 | CV_Assert( N >= K ); |
| 2713 | |
| 2714 | Mat data(N, dims, CV_32F, data0.data, isrow ? dims * sizeof(float) : static_cast<size_t>(data0.step)); |
| 2715 | |
| 2716 | _bestLabels.create(N, 1, CV_32S, -1, true); |
| 2717 | |
| 2718 | Mat _labels, best_labels = _bestLabels.getMat(); |
| 2719 | if( flags & CV_KMEANS_USE_INITIAL_LABELS ) |
| 2720 | { |
| 2721 | CV_Assert( (best_labels.cols == 1 || best_labels.rows == 1) && |
| 2722 | best_labels.cols*best_labels.rows == N && |
| 2723 | best_labels.type() == CV_32S && |
| 2724 | best_labels.isContinuous()); |
| 2725 | best_labels.copyTo(_labels); |
| 2726 | } |
| 2727 | else |
| 2728 | { |
| 2729 | if( !((best_labels.cols == 1 || best_labels.rows == 1) && |
| 2730 | best_labels.cols*best_labels.rows == N && |
| 2731 | best_labels.type() == CV_32S && |
| 2732 | best_labels.isContinuous())) |
| 2733 | best_labels.create(N, 1, CV_32S); |
| 2734 | _labels.create(best_labels.size(), best_labels.type()); |
| 2735 | } |
| 2736 | int* labels = _labels.ptr<int>(); |
| 2737 | |
| 2738 | Mat centers(K, dims, type), old_centers(K, dims, type), temp(1, dims, type); |
| 2739 | vector<int> counters(K); |
| 2740 | vector<Vec2f> _box(dims); |
| 2741 | Vec2f* box = &_box[0]; |
| 2742 | double best_compactness = DBL_MAX, compactness = 0; |
| 2743 | RNG& rng = theRNG(); |
| 2744 | int a, iter, i, j, k; |
| 2745 | |
| 2746 | if( criteria.type & TermCriteria::EPS ) |
| 2747 | criteria.epsilon = std::max(criteria.epsilon, 0.); |
| 2748 | else |
| 2749 | criteria.epsilon = FLT_EPSILON; |
| 2750 | criteria.epsilon *= criteria.epsilon; |
| 2751 | |
| 2752 | if( criteria.type & TermCriteria::COUNT ) |
| 2753 | criteria.maxCount = std::min(std::max(criteria.maxCount, 2), 100); |
| 2754 | else |
| 2755 | criteria.maxCount = 100; |
nothing calls this directly
no test coverage detected