Renesas GR-PEACH OpenCV Development / gr-peach-opencv-project-sd-card_update

Fork of gr-peach-opencv-project-sd-card by the do

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gabor.cpp Source File

gabor.cpp

00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009-2012, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other materials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #include "precomp.hpp"
00044 
00045 /*
00046  Gabor filters and such. To be greatly extended to have full texture analysis.
00047  For the formulas and the explanation of the parameters see:
00048  http://en.wikipedia.org/wiki/Gabor_filter
00049 */
00050 
00051 cv::Mat cv::getGaborKernel( Size ksize, double sigma, double theta,
00052                             double lambd, double gamma, double psi, int ktype )
00053 {
00054     double sigma_x = sigma;
00055     double sigma_y = sigma/gamma;
00056     int nstds = 3;
00057     int xmin, xmax, ymin, ymax;
00058     double c = cos(theta), s = sin(theta);
00059 
00060     if( ksize.width > 0 )
00061         xmax = ksize.width/2;
00062     else
00063         xmax = cvRound(std::max(fabs(nstds*sigma_x*c), fabs(nstds*sigma_y*s)));
00064 
00065     if( ksize.height > 0 )
00066         ymax = ksize.height/2;
00067     else
00068         ymax = cvRound(std::max(fabs(nstds*sigma_x*s), fabs(nstds*sigma_y*c)));
00069 
00070     xmin = -xmax;
00071     ymin = -ymax;
00072 
00073     CV_Assert( ktype == CV_32F || ktype == CV_64F );
00074 
00075     Mat kernel(ymax - ymin + 1, xmax - xmin + 1, ktype);
00076     double scale = 1;
00077     double ex = -0.5/(sigma_x*sigma_x);
00078     double ey = -0.5/(sigma_y*sigma_y);
00079     double cscale = CV_PI*2/lambd;
00080 
00081     for( int y = ymin; y <= ymax; y++ )
00082         for( int x = xmin; x <= xmax; x++ )
00083         {
00084             double xr = x*c + y*s;
00085             double yr = -x*s + y*c;
00086 
00087             double v = scale*std::exp(ex*xr*xr + ey*yr*yr)*cos(cscale*xr + psi);
00088             if( ktype == CV_32F )
00089                 kernel.at<float>(ymax - y, xmax - x) = (float)v;
00090             else
00091                 kernel.at<double>(ymax - y, xmax - x) = v;
00092         }
00093 
00094     return kernel;
00095 }
00096 
00097 
00098 /* End of file. */
00099