24796ec03d6e946ee17ca235c8b9c245191259be
[platform/upstream/opencv.git] / modules / contrib / src / gencolors.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 #include "precomp.hpp"
43
44 #include <iostream>
45
46 using namespace cv;
47
48 static void downsamplePoints( const Mat& src, Mat& dst, size_t count )
49 {
50     CV_Assert( count >= 2 );
51     CV_Assert( src.cols == 1 || src.rows == 1 );
52     CV_Assert( src.total() >= count );
53     CV_Assert( src.type() == CV_8UC3);
54
55     dst.create( 1, (int)count, CV_8UC3 );
56     //TODO: optimize by exploiting symmetry in the distance matrix
57     Mat dists( (int)src.total(), (int)src.total(), CV_32FC1, Scalar(0) );
58     if( dists.empty() )
59         std::cerr << "Such big matrix cann't be created." << std::endl;
60
61     for( int i = 0; i < dists.rows; i++ )
62     {
63         for( int j = i; j < dists.cols; j++ )
64         {
65             float dist = (float)norm(src.at<Point3_<uchar> >(i) - src.at<Point3_<uchar> >(j));
66             dists.at<float>(j, i) = dists.at<float>(i, j) = dist;
67         }
68     }
69
70     double maxVal;
71     Point maxLoc;
72     minMaxLoc(dists, 0, &maxVal, 0, &maxLoc);
73
74     dst.at<Point3_<uchar> >(0) = src.at<Point3_<uchar> >(maxLoc.x);
75     dst.at<Point3_<uchar> >(1) = src.at<Point3_<uchar> >(maxLoc.y);
76
77     Mat activedDists( 0, dists.cols, dists.type() );
78     Mat candidatePointsMask( 1, dists.cols, CV_8UC1, Scalar(255) );
79     activedDists.push_back( dists.row(maxLoc.y) );
80     candidatePointsMask.at<uchar>(0, maxLoc.y) = 0;
81
82     for( size_t i = 2; i < count; i++ )
83     {
84         activedDists.push_back(dists.row(maxLoc.x));
85         candidatePointsMask.at<uchar>(0, maxLoc.x) = 0;
86
87         Mat minDists;
88         reduce( activedDists, minDists, 0, REDUCE_MIN );
89         minMaxLoc( minDists, 0, &maxVal, 0, &maxLoc, candidatePointsMask );
90         dst.at<Point3_<uchar> >((int)i) = src.at<Point3_<uchar> >(maxLoc.x);
91     }
92 }
93
94 void cv::generateColors( std::vector<Scalar>& colors, size_t count, size_t factor )
95 {
96     if( count < 1 )
97         return;
98
99     colors.resize(count);
100
101     if( count == 1 )
102     {
103         colors[0] = Scalar(0,0,255); // red
104         return;
105     }
106     if( count == 2 )
107     {
108         colors[0] = Scalar(0,0,255); // red
109         colors[1] = Scalar(0,255,0); // green
110         return;
111     }
112
113     // Generate a set of colors in RGB space. A size of the set is severel times (=factor) larger then
114     // the needed count of colors.
115     Mat bgr( 1, (int)(count*factor), CV_8UC3 );
116     randu( bgr, 0, 256 );
117
118     // Convert the colors set to Lab space.
119     // Distances between colors in this space correspond a human perception.
120     Mat lab;
121     cvtColor( bgr, lab, COLOR_BGR2Lab);
122
123     // Subsample colors from the generated set so that
124     // to maximize the minimum distances between each other.
125     // Douglas-Peucker algorithm is used for this.
126     Mat lab_subset;
127     downsamplePoints( lab, lab_subset, count );
128
129     // Convert subsampled colors back to RGB
130     Mat bgr_subset;
131     cvtColor( lab_subset, bgr_subset, COLOR_Lab2BGR );
132
133     CV_Assert( bgr_subset.total() == count );
134     for( size_t i = 0; i < count; i++ )
135     {
136         Point3_<uchar> c = bgr_subset.at<Point3_<uchar> >((int)i);
137         colors[i] = Scalar(c.x, c.y, c.z);
138     }
139 }