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) 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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "precomp.hpp"
48 static void downsamplePoints( const Mat& src, Mat& dst, size_t count )
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);
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) );
59 std::cerr << "Such big matrix cann't be created." << std::endl;
61 for( int i = 0; i < dists.rows; i++ )
63 for( int j = i; j < dists.cols; j++ )
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;
72 minMaxLoc(dists, 0, &maxVal, 0, &maxLoc);
74 dst.at<Point3_<uchar> >(0) = src.at<Point3_<uchar> >(maxLoc.x);
75 dst.at<Point3_<uchar> >(1) = src.at<Point3_<uchar> >(maxLoc.y);
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;
82 for( size_t i = 2; i < count; i++ )
84 activedDists.push_back(dists.row(maxLoc.x));
85 candidatePointsMask.at<uchar>(0, maxLoc.x) = 0;
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);
94 void cv::generateColors( std::vector<Scalar>& colors, size_t count, size_t factor )
103 colors[0] = Scalar(0,0,255); // red
108 colors[0] = Scalar(0,0,255); // red
109 colors[1] = Scalar(0,255,0); // green
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 );
118 // Convert the colors set to Lab space.
119 // Distances between colors in this space correspond a human perception.
121 cvtColor( bgr, lab, COLOR_BGR2Lab);
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.
127 downsamplePoints( lab, lab_subset, count );
129 // Convert subsampled colors back to RGB
131 cvtColor( lab_subset, bgr_subset, COLOR_Lab2BGR );
133 CV_Assert( bgr_subset.total() == count );
134 for( size_t i = 0; i < count; i++ )
136 Point3_<uchar> c = bgr_subset.at<Point3_<uchar> >((int)i);
137 colors[i] = Scalar(c.x, c.y, c.z);