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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "precomp.hpp"
46 const int draw_shift_bits = 4;
47 const int draw_multiplier = 1 << draw_shift_bits;
53 * Functions to draw keypoints and matches.
55 static inline void _drawKeypoint( Mat& img, const KeyPoint& p, const Scalar& color, int flags )
57 CV_Assert( !img.empty() );
58 Point center( cvRound(p.pt.x * draw_multiplier), cvRound(p.pt.y * draw_multiplier) );
60 if( flags & DrawMatchesFlags::DRAW_RICH_KEYPOINTS )
62 int radius = cvRound(p.size/2 * draw_multiplier); // KeyPoint::size is a diameter
64 // draw the circles around keypoints with the keypoints size
65 circle( img, center, radius, color, 1, CV_AA, draw_shift_bits );
67 // draw orientation of the keypoint, if it is applicable
70 float srcAngleRad = p.angle*(float)CV_PI/180.f;
71 Point orient( cvRound(cos(srcAngleRad)*radius ),
72 cvRound(sin(srcAngleRad)*radius )
74 line( img, center, center+orient, color, 1, CV_AA, draw_shift_bits );
79 // draw center with R=1
80 int radius = 1 * draw_multiplier;
81 circle( img, center, radius, color, 1, CV_AA, draw_shift_bits );
87 // draw center with R=3
88 int radius = 3 * draw_multiplier;
89 circle( img, center, radius, color, 1, CV_AA, draw_shift_bits );
93 void drawKeypoints( const Mat& image, const vector<KeyPoint>& keypoints, Mat& outImage,
94 const Scalar& _color, int flags )
96 if( !(flags & DrawMatchesFlags::DRAW_OVER_OUTIMG) )
98 if( image.type() == CV_8UC3 )
100 image.copyTo( outImage );
102 else if( image.type() == CV_8UC1 )
104 cvtColor( image, outImage, CV_GRAY2BGR );
108 CV_Error( CV_StsBadArg, "Incorrect type of input image.\n" );
113 bool isRandColor = _color == Scalar::all(-1);
115 CV_Assert( !outImage.empty() );
116 vector<KeyPoint>::const_iterator it = keypoints.begin(),
117 end = keypoints.end();
118 for( ; it != end; ++it )
120 Scalar color = isRandColor ? Scalar(rng(256), rng(256), rng(256)) : _color;
121 _drawKeypoint( outImage, *it, color, flags );
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 )
130 Size size( img1.cols + img2.cols, MAX(img1.rows, img2.rows) );
131 if( flags & DrawMatchesFlags::DRAW_OVER_OUTIMG )
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) );
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) );
145 if( img1.type() == CV_8U )
146 cvtColor( img1, outImg1, CV_GRAY2BGR );
148 img1.copyTo( outImg1 );
150 if( img2.type() == CV_8U )
151 cvtColor( img2, outImg2, CV_GRAY2BGR );
153 img2.copyTo( outImg2 );
157 if( !(flags & DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS) )
159 Mat _outImg1 = outImg( Rect(0, 0, img1.cols, img1.rows) );
160 drawKeypoints( _outImg1, keypoints1, _outImg1, singlePointColor, flags + DrawMatchesFlags::DRAW_OVER_OUTIMG );
162 Mat _outImg2 = outImg( Rect(img1.cols, 0, img2.cols, img2.rows) );
163 drawKeypoints( _outImg2, keypoints2, _outImg2, singlePointColor, flags + DrawMatchesFlags::DRAW_OVER_OUTIMG );
167 static inline void _drawMatch( Mat& outImg, Mat& outImg1, Mat& outImg2 ,
168 const KeyPoint& kp1, const KeyPoint& kp2, const Scalar& matchColor, int flags )
171 bool isRandMatchColor = matchColor == Scalar::all(-1);
172 Scalar color = isRandMatchColor ? Scalar( rng(256), rng(256), rng(256) ) : matchColor;
174 _drawKeypoint( outImg1, kp1, color, flags );
175 _drawKeypoint( outImg2, kp2, color, flags );
177 Point2f pt1 = kp1.pt,
179 dpt2 = Point2f( std::min(pt2.x+outImg1.cols, float(outImg.cols-1)), pt2.y );
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 );
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 )
193 if( !matchesMask.empty() && matchesMask.size() != matches1to2.size() )
194 CV_Error( CV_StsBadSize, "matchesMask must have the same size as matches1to2" );
196 Mat outImg1, outImg2;
197 _prepareImgAndDrawKeypoints( img1, keypoints1, img2, keypoints2,
198 outImg, outImg1, outImg2, singlePointColor, flags );
201 for( size_t m = 0; m < matches1to2.size(); m++ )
203 if( matchesMask.empty() || matchesMask[m] )
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()));
210 const KeyPoint &kp1 = keypoints1[i1], &kp2 = keypoints2[i2];
211 _drawMatch( outImg, outImg1, outImg2, kp1, kp2, matchColor, flags );
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 )
222 if( !matchesMask.empty() && matchesMask.size() != matches1to2.size() )
223 CV_Error( CV_StsBadSize, "matchesMask must have the same size as matches1to2" );
225 Mat outImg1, outImg2;
226 _prepareImgAndDrawKeypoints( img1, keypoints1, img2, keypoints2,
227 outImg, outImg1, outImg2, singlePointColor, flags );
230 for( size_t i = 0; i < matches1to2.size(); i++ )
232 for( size_t j = 0; j < matches1to2[i].size(); j++ )
234 int i1 = matches1to2[i][j].queryIdx;
235 int i2 = matches1to2[i][j].trainIdx;
236 if( matchesMask.empty() || matchesMask[i][j] )
238 const KeyPoint &kp1 = keypoints1[i1], &kp2 = keypoints2[i2];
239 _drawMatch( outImg, outImg1, outImg2, kp1, kp2, matchColor, flags );