draw.cpp 6.94 KB
Newer Older
biagio montesano's avatar
biagio montesano 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
/*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.
 //
 //
 //                           License Agreement
 //                For Open Source Computer Vision Library
 //
 // Copyright (C) 2014, Biagio Montesano, 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 the copyright holders 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*/

42 43 44 45
#include "precomp.hpp"

namespace cv
{
46 47
namespace line_descriptor
{
48 49 50
/* draw matches between two images */
void drawLineMatches( const Mat& img1, const std::vector<KeyLine>& keylines1, const Mat& img2, const std::vector<KeyLine>& keylines2,
                      const std::vector<DMatch>& matches1to2, Mat& outImg, const Scalar& matchColor, const Scalar& singleLineColor,
51
                      const std::vector<char>& matchesMask, int flags )
52 53
{

54 55 56 57 58 59
  if(img1.type() != img2.type())
  {
    std::cout << "Input images have different types" << std::endl;
    CV_Assert(img1.type() == img2.type());
  }

60 61 62 63 64 65 66 67 68 69 70 71
  /* initialize output matrix (if necessary) */
  if( flags == DrawLinesMatchesFlags::DEFAULT )
  {
    /* check how many rows are necessary for output matrix */
    int totalRows = img1.rows >= img2.rows ? img1.rows : img2.rows;

    /* initialize output matrix */
    outImg = Mat::zeros( totalRows, img1.cols + img2.cols, img1.type() );

  }

  /* initialize random seed: */
72
  srand( (unsigned int) time( NULL ) );
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

  Scalar singleLineColorRGB;
  if( singleLineColor == Scalar::all( -1 ) )
  {
    int R = ( rand() % (int) ( 255 + 1 ) );
    int G = ( rand() % (int) ( 255 + 1 ) );
    int B = ( rand() % (int) ( 255 + 1 ) );

    singleLineColorRGB = Scalar( R, G, B );
  }

  else
    singleLineColorRGB = singleLineColor;

  /* copy input images to output images */
  Mat roi_left( outImg, Rect( 0, 0, img1.cols, img1.rows ) );
  Mat roi_right( outImg, Rect( img1.cols, 0, img2.cols, img2.rows ) );
  img1.copyTo( roi_left );
  img2.copyTo( roi_right );

  /* get columns offset */
  int offset = img1.cols;

  /* if requested, draw lines from both images */
  if( flags != DrawLinesMatchesFlags::NOT_DRAW_SINGLE_LINES )
  {
    for ( size_t i = 0; i < keylines1.size(); i++ )
    {
      KeyLine k1 = keylines1[i];
102 103 104
      //line( outImg, Point2f( k1.startPointX, k1.startPointY ), Point2f( k1.endPointX, k1.endPointY ), singleLineColorRGB, 2 );
      line( outImg, Point2f( k1.sPointInOctaveX, k1.sPointInOctaveY ), Point2f( k1.ePointInOctaveX, k1.ePointInOctaveY ), singleLineColorRGB, 2 );

105 106 107 108 109
    }

    for ( size_t j = 0; j < keylines2.size(); j++ )
    {
      KeyLine k2 = keylines2[j];
110
      line( outImg, Point2f( k2.sPointInOctaveX + offset, k2.sPointInOctaveY ), Point2f( k2.ePointInOctaveX + offset, k2.ePointInOctaveY ), singleLineColorRGB, 2 );
111 112 113 114 115 116 117
    }
  }

  /* draw matches */
  for ( size_t counter = 0; counter < matches1to2.size(); counter++ )
  {
    if( matchesMask[counter] != 0 )
118
    {
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
      DMatch dm = matches1to2[counter];
      KeyLine left = keylines1[dm.queryIdx];
      KeyLine right = keylines2[dm.trainIdx];

      Scalar matchColorRGB;
      if( matchColor == Scalar::all( -1 ) )
      {
        int R = ( rand() % (int) ( 255 + 1 ) );
        int G = ( rand() % (int) ( 255 + 1 ) );
        int B = ( rand() % (int) ( 255 + 1 ) );

        matchColorRGB = Scalar( R, G, B );

        if( singleLineColor == Scalar::all( -1 ) )
          singleLineColorRGB = matchColorRGB;
      }

      else
        matchColorRGB = matchColor;

      /* draw lines if necessary */
140 141 142 143 144 145 146 147 148
//      line( outImg, Point2f( left.startPointX, left.startPointY ), Point2f( left.endPointX, left.endPointY ), singleLineColorRGB, 2 );
//
//      line( outImg, Point2f( right.startPointX + offset, right.startPointY ), Point2f( right.endPointX + offset, right.endPointY ), singleLineColorRGB,
//            2 );
//
//      /* link correspondent lines */
//      line( outImg, Point2f( left.startPointX, left.startPointY ), Point2f( right.startPointX + offset, right.startPointY ), matchColorRGB, 1 );

      line( outImg, Point2f( left.sPointInOctaveX, left.sPointInOctaveY ), Point2f( left.ePointInOctaveX, left.ePointInOctaveY ), singleLineColorRGB, 2 );
149

150 151
        line( outImg, Point2f( right.sPointInOctaveX + offset, right.sPointInOctaveY ), Point2f( right.ePointInOctaveX + offset, right.ePointInOctaveY ), singleLineColorRGB,
              2 );
152

153 154
        /* link correspondent lines */
        line( outImg, Point2f( left.sPointInOctaveX, left.sPointInOctaveY ), Point2f( right.sPointInOctaveX + offset, right.sPointInOctaveY ), matchColorRGB, 1 );
155
    }
156 157
  }
}
158

159 160 161 162 163 164 165 166 167 168
/* draw extracted lines on original image */
void drawKeylines( const Mat& image, const std::vector<KeyLine>& keylines, Mat& outImage, const Scalar& color, int flags )
{
  if( flags == DrawLinesMatchesFlags::DEFAULT )
    outImage = image.clone();

  for ( size_t i = 0; i < keylines.size(); i++ )
  {
    /* decide lines' color  */
    Scalar lineColor;
169
    if( color == Scalar::all( -1 ) )
170
    {
171 172 173 174 175
      int R = ( rand() % (int) ( 255 + 1 ) );
      int G = ( rand() % (int) ( 255 + 1 ) );
      int B = ( rand() % (int) ( 255 + 1 ) );

      lineColor = Scalar( R, G, B );
176 177
    }

178 179 180 181 182 183 184
    else
      lineColor = color;

    /* get line */
    KeyLine k = keylines[i];

    /* draw line */
185
    line( outImage, Point2f( k.startPointX, k.startPointY ), Point2f( k.endPointX, k.endPointY ), lineColor, 1 );
186 187 188
  }
}

189
}
190
}