Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/features2d/src/draw.cpp
16337 views
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
const int draw_shift_bits = 4;
45
const int draw_multiplier = 1 << draw_shift_bits;
46
47
namespace cv
48
{
49
50
/*
51
* Functions to draw keypoints and matches.
52
*/
53
static inline void _drawKeypoint( InputOutputArray img, const KeyPoint& p, const Scalar& color, DrawMatchesFlags flags )
54
{
55
CV_Assert( !img.empty() );
56
Point center( cvRound(p.pt.x * draw_multiplier), cvRound(p.pt.y * draw_multiplier) );
57
58
if( !!(flags & DrawMatchesFlags::DRAW_RICH_KEYPOINTS) )
59
{
60
int radius = cvRound(p.size/2 * draw_multiplier); // KeyPoint::size is a diameter
61
62
// draw the circles around keypoints with the keypoints size
63
circle( img, center, radius, color, 1, LINE_AA, draw_shift_bits );
64
65
// draw orientation of the keypoint, if it is applicable
66
if( p.angle != -1 )
67
{
68
float srcAngleRad = p.angle*(float)CV_PI/180.f;
69
Point orient( cvRound(cos(srcAngleRad)*radius ),
70
cvRound(sin(srcAngleRad)*radius )
71
);
72
line( img, center, center+orient, color, 1, LINE_AA, draw_shift_bits );
73
}
74
#if 0
75
else
76
{
77
// draw center with R=1
78
int radius = 1 * draw_multiplier;
79
circle( img, center, radius, color, 1, LINE_AA, draw_shift_bits );
80
}
81
#endif
82
}
83
else
84
{
85
// draw center with R=3
86
int radius = 3 * draw_multiplier;
87
circle( img, center, radius, color, 1, LINE_AA, draw_shift_bits );
88
}
89
}
90
91
void drawKeypoints( InputArray image, const std::vector<KeyPoint>& keypoints, InputOutputArray outImage,
92
const Scalar& _color, DrawMatchesFlags flags )
93
{
94
CV_INSTRUMENT_REGION();
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, COLOR_GRAY2BGR );
105
}
106
else
107
{
108
CV_Error( Error::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
std::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( InputArray img1, const std::vector<KeyPoint>& keypoints1,
126
InputArray img2, const std::vector<KeyPoint>& keypoints2,
127
InputOutputArray _outImg, Mat& outImg1, Mat& outImg2,
128
const Scalar& singlePointColor, DrawMatchesFlags flags )
129
{
130
Mat outImg;
131
Size img1size = img1.size(), img2size = img2.size();
132
Size size( img1size.width + img2size.width, MAX(img1size.height, img2size.height) );
133
if( !!(flags & DrawMatchesFlags::DRAW_OVER_OUTIMG) )
134
{
135
outImg = _outImg.getMat();
136
if( size.width > outImg.cols || size.height > outImg.rows )
137
CV_Error( Error::StsBadSize, "outImg has size less than need to draw img1 and img2 together" );
138
outImg1 = outImg( Rect(0, 0, img1size.width, img1size.height) );
139
outImg2 = outImg( Rect(img1size.width, 0, img2size.width, img2size.height) );
140
}
141
else
142
{
143
_outImg.create( size, CV_MAKETYPE(img1.depth(), 3) );
144
outImg = _outImg.getMat();
145
outImg = Scalar::all(0);
146
outImg1 = outImg( Rect(0, 0, img1size.width, img1size.height) );
147
outImg2 = outImg( Rect(img1size.width, 0, img2size.width, img2size.height) );
148
149
if( img1.type() == CV_8U )
150
cvtColor( img1, outImg1, COLOR_GRAY2BGR );
151
else
152
img1.copyTo( outImg1 );
153
154
if( img2.type() == CV_8U )
155
cvtColor( img2, outImg2, COLOR_GRAY2BGR );
156
else
157
img2.copyTo( outImg2 );
158
}
159
160
// draw keypoints
161
if( !(flags & DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS) )
162
{
163
Mat _outImg1 = outImg( Rect(0, 0, img1size.width, img1size.height) );
164
drawKeypoints( _outImg1, keypoints1, _outImg1, singlePointColor, flags | DrawMatchesFlags::DRAW_OVER_OUTIMG );
165
166
Mat _outImg2 = outImg( Rect(img1size.width, 0, img2size.width, img2size.height) );
167
drawKeypoints( _outImg2, keypoints2, _outImg2, singlePointColor, flags | DrawMatchesFlags::DRAW_OVER_OUTIMG );
168
}
169
}
170
171
static inline void _drawMatch( InputOutputArray outImg, InputOutputArray outImg1, InputOutputArray outImg2 ,
172
const KeyPoint& kp1, const KeyPoint& kp2, const Scalar& matchColor, DrawMatchesFlags flags )
173
{
174
RNG& rng = theRNG();
175
bool isRandMatchColor = matchColor == Scalar::all(-1);
176
Scalar color = isRandMatchColor ? Scalar( rng(256), rng(256), rng(256) ) : matchColor;
177
178
_drawKeypoint( outImg1, kp1, color, flags );
179
_drawKeypoint( outImg2, kp2, color, flags );
180
181
Point2f pt1 = kp1.pt,
182
pt2 = kp2.pt,
183
dpt2 = Point2f( std::min(pt2.x+outImg1.size().width, float(outImg.size().width-1)), pt2.y );
184
185
line( outImg,
186
Point(cvRound(pt1.x*draw_multiplier), cvRound(pt1.y*draw_multiplier)),
187
Point(cvRound(dpt2.x*draw_multiplier), cvRound(dpt2.y*draw_multiplier)),
188
color, 1, LINE_AA, draw_shift_bits );
189
}
190
191
void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
192
InputArray img2, const std::vector<KeyPoint>& keypoints2,
193
const std::vector<DMatch>& matches1to2, InputOutputArray outImg,
194
const Scalar& matchColor, const Scalar& singlePointColor,
195
const std::vector<char>& matchesMask, DrawMatchesFlags flags )
196
{
197
if( !matchesMask.empty() && matchesMask.size() != matches1to2.size() )
198
CV_Error( Error::StsBadSize, "matchesMask must have the same size as matches1to2" );
199
200
Mat outImg1, outImg2;
201
_prepareImgAndDrawKeypoints( img1, keypoints1, img2, keypoints2,
202
outImg, outImg1, outImg2, singlePointColor, flags );
203
204
// draw matches
205
for( size_t m = 0; m < matches1to2.size(); m++ )
206
{
207
if( matchesMask.empty() || matchesMask[m] )
208
{
209
int i1 = matches1to2[m].queryIdx;
210
int i2 = matches1to2[m].trainIdx;
211
CV_Assert(i1 >= 0 && i1 < static_cast<int>(keypoints1.size()));
212
CV_Assert(i2 >= 0 && i2 < static_cast<int>(keypoints2.size()));
213
214
const KeyPoint &kp1 = keypoints1[i1], &kp2 = keypoints2[i2];
215
_drawMatch( outImg, outImg1, outImg2, kp1, kp2, matchColor, flags );
216
}
217
}
218
}
219
220
void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
221
InputArray img2, const std::vector<KeyPoint>& keypoints2,
222
const std::vector<std::vector<DMatch> >& matches1to2, InputOutputArray outImg,
223
const Scalar& matchColor, const Scalar& singlePointColor,
224
const std::vector<std::vector<char> >& matchesMask, DrawMatchesFlags flags )
225
{
226
if( !matchesMask.empty() && matchesMask.size() != matches1to2.size() )
227
CV_Error( Error::StsBadSize, "matchesMask must have the same size as matches1to2" );
228
229
Mat outImg1, outImg2;
230
_prepareImgAndDrawKeypoints( img1, keypoints1, img2, keypoints2,
231
outImg, outImg1, outImg2, singlePointColor, flags );
232
233
// draw matches
234
for( size_t i = 0; i < matches1to2.size(); i++ )
235
{
236
for( size_t j = 0; j < matches1to2[i].size(); j++ )
237
{
238
int i1 = matches1to2[i][j].queryIdx;
239
int i2 = matches1to2[i][j].trainIdx;
240
if( matchesMask.empty() || matchesMask[i][j] )
241
{
242
const KeyPoint &kp1 = keypoints1[i1], &kp2 = keypoints2[i2];
243
_drawMatch( outImg, outImg1, outImg2, kp1, kp2, matchColor, flags );
244
}
245
}
246
}
247
}
248
}
249
250