Add OpenCV source code
[platform/upstream/opencv.git] / modules / features2d / src / draw.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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43
44 using namespace std;
45
46 const int draw_shift_bits = 4;
47 const int draw_multiplier = 1 << draw_shift_bits;
48
49 namespace cv
50 {
51
52 /*
53  * Functions to draw keypoints and matches.
54  */
55 static inline void _drawKeypoint( Mat& img, const KeyPoint& p, const Scalar& color, int flags )
56 {
57     CV_Assert( !img.empty() );
58     Point center( cvRound(p.pt.x * draw_multiplier), cvRound(p.pt.y * draw_multiplier) );
59
60     if( flags & DrawMatchesFlags::DRAW_RICH_KEYPOINTS )
61     {
62         int radius = cvRound(p.size/2 * draw_multiplier); // KeyPoint::size is a diameter
63
64         // draw the circles around keypoints with the keypoints size
65         circle( img, center, radius, color, 1, CV_AA, draw_shift_bits );
66
67         // draw orientation of the keypoint, if it is applicable
68         if( p.angle != -1 )
69         {
70             float srcAngleRad = p.angle*(float)CV_PI/180.f;
71             Point orient( cvRound(cos(srcAngleRad)*radius ),
72                           cvRound(sin(srcAngleRad)*radius )
73                         );
74             line( img, center, center+orient, color, 1, CV_AA, draw_shift_bits );
75         }
76 #if 0
77         else
78         {
79             // draw center with R=1
80             int radius = 1 * draw_multiplier;
81             circle( img, center, radius, color, 1, CV_AA, draw_shift_bits );
82         }
83 #endif
84     }
85     else
86     {
87         // draw center with R=3
88         int radius = 3 * draw_multiplier;
89         circle( img, center, radius, color, 1, CV_AA, draw_shift_bits );
90     }
91 }
92
93 void drawKeypoints( const Mat& image, const vector<KeyPoint>& keypoints, Mat& outImage,
94                     const Scalar& _color, int flags )
95 {
96     if( !(flags & DrawMatchesFlags::DRAW_OVER_OUTIMG) )
97     {
98         if( image.type() == CV_8UC3 )
99         {
100             image.copyTo( outImage );
101         }
102         else if( image.type() == CV_8UC1 )
103         {
104             cvtColor( image, outImage, CV_GRAY2BGR );
105         }
106         else
107         {
108             CV_Error( CV_StsBadArg, "Incorrect type of input image.\n" );
109         }
110     }
111
112     RNG& rng=theRNG();
113     bool isRandColor = _color == Scalar::all(-1);
114
115     CV_Assert( !outImage.empty() );
116     vector<KeyPoint>::const_iterator it = keypoints.begin(),
117                                      end = keypoints.end();
118     for( ; it != end; ++it )
119     {
120         Scalar color = isRandColor ? Scalar(rng(256), rng(256), rng(256)) : _color;
121         _drawKeypoint( outImage, *it, color, flags );
122     }
123 }
124
125 static void _prepareImgAndDrawKeypoints( const Mat& img1, const vector<KeyPoint>& keypoints1,
126                                          const Mat& img2, const vector<KeyPoint>& keypoints2,
127                                          Mat& outImg, Mat& outImg1, Mat& outImg2,
128                                          const Scalar& singlePointColor, int flags )
129 {
130     Size size( img1.cols + img2.cols, MAX(img1.rows, img2.rows) );
131     if( flags & DrawMatchesFlags::DRAW_OVER_OUTIMG )
132     {
133         if( size.width > outImg.cols || size.height > outImg.rows )
134             CV_Error( CV_StsBadSize, "outImg has size less than need to draw img1 and img2 together" );
135         outImg1 = outImg( Rect(0, 0, img1.cols, img1.rows) );
136         outImg2 = outImg( Rect(img1.cols, 0, img2.cols, img2.rows) );
137     }
138     else
139     {
140         outImg.create( size, CV_MAKETYPE(img1.depth(), 3) );
141         outImg = Scalar::all(0);
142         outImg1 = outImg( Rect(0, 0, img1.cols, img1.rows) );
143         outImg2 = outImg( Rect(img1.cols, 0, img2.cols, img2.rows) );
144
145         if( img1.type() == CV_8U )
146             cvtColor( img1, outImg1, CV_GRAY2BGR );
147         else
148             img1.copyTo( outImg1 );
149
150         if( img2.type() == CV_8U )
151             cvtColor( img2, outImg2, CV_GRAY2BGR );
152         else
153             img2.copyTo( outImg2 );
154     }
155
156     // draw keypoints
157     if( !(flags & DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS) )
158     {
159         Mat _outImg1 = outImg( Rect(0, 0, img1.cols, img1.rows) );
160         drawKeypoints( _outImg1, keypoints1, _outImg1, singlePointColor, flags + DrawMatchesFlags::DRAW_OVER_OUTIMG );
161
162         Mat _outImg2 = outImg( Rect(img1.cols, 0, img2.cols, img2.rows) );
163         drawKeypoints( _outImg2, keypoints2, _outImg2, singlePointColor, flags + DrawMatchesFlags::DRAW_OVER_OUTIMG );
164     }
165 }
166
167 static inline void _drawMatch( Mat& outImg, Mat& outImg1, Mat& outImg2 ,
168                           const KeyPoint& kp1, const KeyPoint& kp2, const Scalar& matchColor, int flags )
169 {
170     RNG& rng = theRNG();
171     bool isRandMatchColor = matchColor == Scalar::all(-1);
172     Scalar color = isRandMatchColor ? Scalar( rng(256), rng(256), rng(256) ) : matchColor;
173
174     _drawKeypoint( outImg1, kp1, color, flags );
175     _drawKeypoint( outImg2, kp2, color, flags );
176
177     Point2f pt1 = kp1.pt,
178             pt2 = kp2.pt,
179             dpt2 = Point2f( std::min(pt2.x+outImg1.cols, float(outImg.cols-1)), pt2.y );
180
181     line( outImg,
182           Point(cvRound(pt1.x*draw_multiplier), cvRound(pt1.y*draw_multiplier)),
183           Point(cvRound(dpt2.x*draw_multiplier), cvRound(dpt2.y*draw_multiplier)),
184           color, 1, CV_AA, draw_shift_bits );
185 }
186
187 void drawMatches( const Mat& img1, const vector<KeyPoint>& keypoints1,
188                   const Mat& img2, const vector<KeyPoint>& keypoints2,
189                   const vector<DMatch>& matches1to2, Mat& outImg,
190                   const Scalar& matchColor, const Scalar& singlePointColor,
191                   const vector<char>& matchesMask, int flags )
192 {
193     if( !matchesMask.empty() && matchesMask.size() != matches1to2.size() )
194         CV_Error( CV_StsBadSize, "matchesMask must have the same size as matches1to2" );
195
196     Mat outImg1, outImg2;
197     _prepareImgAndDrawKeypoints( img1, keypoints1, img2, keypoints2,
198                                  outImg, outImg1, outImg2, singlePointColor, flags );
199
200     // draw matches
201     for( size_t m = 0; m < matches1to2.size(); m++ )
202     {
203         if( matchesMask.empty() || matchesMask[m] )
204         {
205             int i1 = matches1to2[m].queryIdx;
206             int i2 = matches1to2[m].trainIdx;
207             CV_Assert(i1 >= 0 && i1 < static_cast<int>(keypoints1.size()));
208             CV_Assert(i2 >= 0 && i2 < static_cast<int>(keypoints2.size()));
209
210             const KeyPoint &kp1 = keypoints1[i1], &kp2 = keypoints2[i2];
211             _drawMatch( outImg, outImg1, outImg2, kp1, kp2, matchColor, flags );
212         }
213     }
214 }
215
216 void drawMatches( const Mat& img1, const vector<KeyPoint>& keypoints1,
217                   const Mat& img2, const vector<KeyPoint>& keypoints2,
218                   const vector<vector<DMatch> >& matches1to2, Mat& outImg,
219                   const Scalar& matchColor, const Scalar& singlePointColor,
220                   const vector<vector<char> >& matchesMask, int flags )
221 {
222     if( !matchesMask.empty() && matchesMask.size() != matches1to2.size() )
223         CV_Error( CV_StsBadSize, "matchesMask must have the same size as matches1to2" );
224
225     Mat outImg1, outImg2;
226     _prepareImgAndDrawKeypoints( img1, keypoints1, img2, keypoints2,
227                                  outImg, outImg1, outImg2, singlePointColor, flags );
228
229     // draw matches
230     for( size_t i = 0; i < matches1to2.size(); i++ )
231     {
232         for( size_t j = 0; j < matches1to2[i].size(); j++ )
233         {
234             int i1 = matches1to2[i][j].queryIdx;
235             int i2 = matches1to2[i][j].trainIdx;
236             if( matchesMask.empty() || matchesMask[i][j] )
237             {
238                 const KeyPoint &kp1 = keypoints1[i1], &kp2 = keypoints2[i2];
239                 _drawMatch( outImg, outImg1, outImg2, kp1, kp2, matchColor, flags );
240             }
241         }
242     }
243 }
244 }