draw.cpp 9.41 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"

wester committed
44 45
using namespace std;

wester committed
46 47 48 49 50 51 52 53 54
const int draw_shift_bits = 4;
const int draw_multiplier = 1 << draw_shift_bits;

namespace cv
{

/*
 * Functions to draw keypoints and matches.
 */
wester committed
55
static inline void _drawKeypoint( Mat& img, const KeyPoint& p, const Scalar& color, int flags )
wester committed
56 57 58 59 60 61 62 63 64
{
    CV_Assert( !img.empty() );
    Point center( cvRound(p.pt.x * draw_multiplier), cvRound(p.pt.y * draw_multiplier) );

    if( flags & DrawMatchesFlags::DRAW_RICH_KEYPOINTS )
    {
        int radius = cvRound(p.size/2 * draw_multiplier); // KeyPoint::size is a diameter

        // draw the circles around keypoints with the keypoints size
wester committed
65
        circle( img, center, radius, color, 1, CV_AA, draw_shift_bits );
wester committed
66 67 68 69 70 71 72 73

        // draw orientation of the keypoint, if it is applicable
        if( p.angle != -1 )
        {
            float srcAngleRad = p.angle*(float)CV_PI/180.f;
            Point orient( cvRound(cos(srcAngleRad)*radius ),
                          cvRound(sin(srcAngleRad)*radius )
                        );
wester committed
74
            line( img, center, center+orient, color, 1, CV_AA, draw_shift_bits );
wester committed
75 76 77 78 79 80
        }
#if 0
        else
        {
            // draw center with R=1
            int radius = 1 * draw_multiplier;
wester committed
81
            circle( img, center, radius, color, 1, CV_AA, draw_shift_bits );
wester committed
82 83 84 85 86 87 88
        }
#endif
    }
    else
    {
        // draw center with R=3
        int radius = 3 * draw_multiplier;
wester committed
89
        circle( img, center, radius, color, 1, CV_AA, draw_shift_bits );
wester committed
90 91 92
    }
}

wester committed
93
void drawKeypoints( const Mat& image, const vector<KeyPoint>& keypoints, Mat& outImage,
wester committed
94 95 96 97 98 99 100 101 102 103
                    const Scalar& _color, int flags )
{
    if( !(flags & DrawMatchesFlags::DRAW_OVER_OUTIMG) )
    {
        if( image.type() == CV_8UC3 )
        {
            image.copyTo( outImage );
        }
        else if( image.type() == CV_8UC1 )
        {
wester committed
104
            cvtColor( image, outImage, CV_GRAY2BGR );
wester committed
105 106 107
        }
        else
        {
wester committed
108
            CV_Error( CV_StsBadArg, "Incorrect type of input image.\n" );
wester committed
109 110 111 112 113 114 115
        }
    }

    RNG& rng=theRNG();
    bool isRandColor = _color == Scalar::all(-1);

    CV_Assert( !outImage.empty() );
wester committed
116
    vector<KeyPoint>::const_iterator it = keypoints.begin(),
wester committed
117 118 119 120 121 122 123 124
                                     end = keypoints.end();
    for( ; it != end; ++it )
    {
        Scalar color = isRandColor ? Scalar(rng(256), rng(256), rng(256)) : _color;
        _drawKeypoint( outImage, *it, color, flags );
    }
}

wester committed
125 126 127
static void _prepareImgAndDrawKeypoints( const Mat& img1, const vector<KeyPoint>& keypoints1,
                                         const Mat& img2, const vector<KeyPoint>& keypoints2,
                                         Mat& outImg, Mat& outImg1, Mat& outImg2,
wester committed
128 129
                                         const Scalar& singlePointColor, int flags )
{
wester committed
130
    Size size( img1.cols + img2.cols, MAX(img1.rows, img2.rows) );
wester committed
131 132 133
    if( flags & DrawMatchesFlags::DRAW_OVER_OUTIMG )
    {
        if( size.width > outImg.cols || size.height > outImg.rows )
wester committed
134 135 136
            CV_Error( CV_StsBadSize, "outImg has size less than need to draw img1 and img2 together" );
        outImg1 = outImg( Rect(0, 0, img1.cols, img1.rows) );
        outImg2 = outImg( Rect(img1.cols, 0, img2.cols, img2.rows) );
wester committed
137 138 139
    }
    else
    {
wester committed
140
        outImg.create( size, CV_MAKETYPE(img1.depth(), 3) );
wester committed
141
        outImg = Scalar::all(0);
wester committed
142 143
        outImg1 = outImg( Rect(0, 0, img1.cols, img1.rows) );
        outImg2 = outImg( Rect(img1.cols, 0, img2.cols, img2.rows) );
wester committed
144 145

        if( img1.type() == CV_8U )
wester committed
146
            cvtColor( img1, outImg1, CV_GRAY2BGR );
wester committed
147 148 149 150
        else
            img1.copyTo( outImg1 );

        if( img2.type() == CV_8U )
wester committed
151
            cvtColor( img2, outImg2, CV_GRAY2BGR );
wester committed
152 153 154 155 156 157 158
        else
            img2.copyTo( outImg2 );
    }

    // draw keypoints
    if( !(flags & DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS) )
    {
wester committed
159
        Mat _outImg1 = outImg( Rect(0, 0, img1.cols, img1.rows) );
wester committed
160 161
        drawKeypoints( _outImg1, keypoints1, _outImg1, singlePointColor, flags | DrawMatchesFlags::DRAW_OVER_OUTIMG );

wester committed
162
        Mat _outImg2 = outImg( Rect(img1.cols, 0, img2.cols, img2.rows) );
wester committed
163 164 165 166
        drawKeypoints( _outImg2, keypoints2, _outImg2, singlePointColor, flags | DrawMatchesFlags::DRAW_OVER_OUTIMG );
    }
}

wester committed
167
static inline void _drawMatch( Mat& outImg, Mat& outImg1, Mat& outImg2 ,
wester committed
168 169 170 171 172 173 174 175 176 177 178
                          const KeyPoint& kp1, const KeyPoint& kp2, const Scalar& matchColor, int flags )
{
    RNG& rng = theRNG();
    bool isRandMatchColor = matchColor == Scalar::all(-1);
    Scalar color = isRandMatchColor ? Scalar( rng(256), rng(256), rng(256) ) : matchColor;

    _drawKeypoint( outImg1, kp1, color, flags );
    _drawKeypoint( outImg2, kp2, color, flags );

    Point2f pt1 = kp1.pt,
            pt2 = kp2.pt,
wester committed
179
            dpt2 = Point2f( std::min(pt2.x+outImg1.cols, float(outImg.cols-1)), pt2.y );
wester committed
180 181 182 183

    line( outImg,
          Point(cvRound(pt1.x*draw_multiplier), cvRound(pt1.y*draw_multiplier)),
          Point(cvRound(dpt2.x*draw_multiplier), cvRound(dpt2.y*draw_multiplier)),
wester committed
184
          color, 1, CV_AA, draw_shift_bits );
wester committed
185 186
}

wester committed
187 188 189
void drawMatches( const Mat& img1, const vector<KeyPoint>& keypoints1,
                  const Mat& img2, const vector<KeyPoint>& keypoints2,
                  const vector<DMatch>& matches1to2, Mat& outImg,
wester committed
190
                  const Scalar& matchColor, const Scalar& singlePointColor,
wester committed
191
                  const vector<char>& matchesMask, int flags )
wester committed
192 193
{
    if( !matchesMask.empty() && matchesMask.size() != matches1to2.size() )
wester committed
194
        CV_Error( CV_StsBadSize, "matchesMask must have the same size as matches1to2" );
wester committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

    Mat outImg1, outImg2;
    _prepareImgAndDrawKeypoints( img1, keypoints1, img2, keypoints2,
                                 outImg, outImg1, outImg2, singlePointColor, flags );

    // draw matches
    for( size_t m = 0; m < matches1to2.size(); m++ )
    {
        if( matchesMask.empty() || matchesMask[m] )
        {
            int i1 = matches1to2[m].queryIdx;
            int i2 = matches1to2[m].trainIdx;
            CV_Assert(i1 >= 0 && i1 < static_cast<int>(keypoints1.size()));
            CV_Assert(i2 >= 0 && i2 < static_cast<int>(keypoints2.size()));

            const KeyPoint &kp1 = keypoints1[i1], &kp2 = keypoints2[i2];
            _drawMatch( outImg, outImg1, outImg2, kp1, kp2, matchColor, flags );
        }
    }
}

wester committed
216 217 218
void drawMatches( const Mat& img1, const vector<KeyPoint>& keypoints1,
                  const Mat& img2, const vector<KeyPoint>& keypoints2,
                  const vector<vector<DMatch> >& matches1to2, Mat& outImg,
wester committed
219
                  const Scalar& matchColor, const Scalar& singlePointColor,
wester committed
220
                  const vector<vector<char> >& matchesMask, int flags )
wester committed
221 222
{
    if( !matchesMask.empty() && matchesMask.size() != matches1to2.size() )
wester committed
223
        CV_Error( CV_StsBadSize, "matchesMask must have the same size as matches1to2" );
wester committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

    Mat outImg1, outImg2;
    _prepareImgAndDrawKeypoints( img1, keypoints1, img2, keypoints2,
                                 outImg, outImg1, outImg2, singlePointColor, flags );

    // draw matches
    for( size_t i = 0; i < matches1to2.size(); i++ )
    {
        for( size_t j = 0; j < matches1to2[i].size(); j++ )
        {
            int i1 = matches1to2[i][j].queryIdx;
            int i2 = matches1to2[i][j].trainIdx;
            if( matchesMask.empty() || matchesMask[i][j] )
            {
                const KeyPoint &kp1 = keypoints1[i1], &kp2 = keypoints2[i2];
                _drawMatch( outImg, outImg1, outImg2, kp1, kp2, matchColor, flags );
            }
        }
    }
}
}