staticSaliencySpectralResidual.cpp 4.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*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
 //
jaco's avatar
jaco committed
13
 // Copyright (C) 2014, OpenCV Foundation, all rights reserved.
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 44 45
 // 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*/

#include "precomp.hpp"

namespace cv
{
jaco's avatar
jaco committed
46 47
namespace saliency
{
48 49 50 51 52

/**
 * SaliencySpectralResidual
 */

53 54

StaticSaliencySpectralResidual::StaticSaliencySpectralResidual()
55 56
{
  className = "SPECTRAL_RESIDUAL";
jaco's avatar
jaco committed
57 58
  resImWidth = 64;
  resImHeight = 64;
59 60 61 62 63 64 65
}

StaticSaliencySpectralResidual::~StaticSaliencySpectralResidual()
{

}

jaco's avatar
jaco committed
66
void StaticSaliencySpectralResidual::read( const cv::FileNode& /*fn*/)
67
{
68
  //params.read( fn );
69 70
}

jaco's avatar
jaco committed
71
void StaticSaliencySpectralResidual::write( cv::FileStorage& /*fs*/) const
72
{
73
  //params.write( fs );
74 75
}

76
bool StaticSaliencySpectralResidual::computeSaliencyImpl( InputArray image, OutputArray saliencyMap )
77 78 79
{
  Mat grayTemp, grayDown;
  std::vector<Mat> mv;
jaco's avatar
jaco committed
80
  Size resizedImageSize( resImWidth, resImHeight );
81 82 83

  Mat realImage( resizedImageSize, CV_64F );
  Mat imaginaryImage( resizedImageSize, CV_64F );
84
  imaginaryImage.setTo( 0 );
85
  Mat combinedImage( resizedImageSize, CV_64FC2 );
86 87
  Mat imageDFT;
  Mat logAmplitude;
88 89
  Mat angle( resizedImageSize, CV_64F );
  Mat magnitude( resizedImageSize, CV_64F );
90 91 92 93 94
  Mat logAmplitude_blur, imageGR;

  if( image.channels() == 3 )
  {
    cvtColor( image, imageGR, COLOR_BGR2GRAY );
95
    resize( imageGR, grayDown, resizedImageSize, 0, 0, INTER_LINEAR );
96 97 98
  }
  else
  {
99
    resize( image, grayDown, resizedImageSize, 0, 0, INTER_LINEAR );
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  }

  grayDown.convertTo( realImage, CV_64F );

  mv.push_back( realImage );
  mv.push_back( imaginaryImage );
  merge( mv, combinedImage );
  dft( combinedImage, imageDFT );
  split( imageDFT, mv );

  //-- Get magnitude and phase of frequency spectrum --//
  cartToPolar( mv.at( 0 ), mv.at( 1 ), magnitude, angle, false );
  log( magnitude, logAmplitude );
  //-- Blur log amplitude with averaging filter --//
  blur( logAmplitude, logAmplitude_blur, Size( 3, 3 ), Point( -1, -1 ), BORDER_DEFAULT );

  exp( logAmplitude - logAmplitude_blur, magnitude );
  //-- Back to cartesian frequency domain --//
  polarToCart( magnitude, angle, mv.at( 0 ), mv.at( 1 ), false );
  merge( mv, imageDFT );
  dft( imageDFT, combinedImage, DFT_INVERSE );
  split( combinedImage, mv );

  cartToPolar( mv.at( 0 ), mv.at( 1 ), magnitude, angle, false );
  GaussianBlur( magnitude, magnitude, Size( 5, 5 ), 8, 0, BORDER_DEFAULT );
  magnitude = magnitude.mul( magnitude );

  double minVal, maxVal;
  minMaxLoc( magnitude, &minVal, &maxVal );

  magnitude = magnitude / maxVal;
  magnitude.convertTo( magnitude, CV_32F );

  resize( magnitude, saliencyMap, image.size(), 0, 0, INTER_LINEAR );

jaco's avatar
jaco committed
135
#ifdef SALIENCY_DEBUG
136 137
  // visualize saliency map
  imshow( "Saliency Map Interna", saliencyMap );
jaco's avatar
jaco committed
138
#endif
139 140 141 142 143

  return true;

}

jaco's avatar
jaco committed
144
} /* namespace saliency */
145
}/* namespace cv */