00001 // **************************************************************************** 00002 // This file is part of the Integrating Vision Toolkit (IVT). 00003 // 00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT) 00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de). 00006 // 00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT). 00008 // All rights reserved. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are met: 00012 // 00013 // 1. Redistributions of source code must retain the above copyright 00014 // notice, this list of conditions and the following disclaimer. 00015 // 00016 // 2. Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // 3. Neither the name of the KIT nor the names of its contributors may be 00021 // used to endorse or promote products derived from this software 00022 // without specific prior written permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY 00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY 00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 // **************************************************************************** 00035 // **************************************************************************** 00036 // Filename: MeanFilter.cpp 00037 // Author: Pedram Azad 00038 // Date: 2006 00039 // **************************************************************************** 00040 00041 00042 // **************************************************************************** 00043 // Includes 00044 // **************************************************************************** 00045 00046 #include <new> // for explicitly using correct new/delete operators on VC DSPs 00047 00048 #include "MeanFilter.h" 00049 00050 #include <stdio.h> 00051 00052 00053 00054 // **************************************************************************** 00055 // Constructor / Destructor 00056 // **************************************************************************** 00057 00058 CMeanFilter::CMeanFilter(int nKernelSize) 00059 { 00060 m_pValues = 0; 00061 m_nPosition = 0; 00062 m_nElementsFilled = 0; 00063 m_nKernelSize = 0; 00064 00065 SetKernelSize(nKernelSize); 00066 } 00067 00068 CMeanFilter::~CMeanFilter() 00069 { 00070 if (m_pValues) 00071 delete [] m_pValues; 00072 } 00073 00074 00075 // **************************************************************************** 00076 // Methods 00077 // **************************************************************************** 00078 00079 void CMeanFilter::SetKernelSize(int nKernelSize) 00080 { 00081 if (nKernelSize <= 0) 00082 { 00083 printf("error: nKernelSize must be greater 0 for CMeanFilter::SetKernelSize\n"); 00084 return; 00085 } 00086 00087 m_nKernelSize = nKernelSize; 00088 00089 if (m_pValues) 00090 delete [] m_pValues; 00091 00092 m_pValues = new float[nKernelSize]; 00093 00094 Reset(); 00095 } 00096 00097 void CMeanFilter::Reset() 00098 { 00099 for (int i = 0; i < m_nKernelSize; i++) 00100 m_pValues[i] = 0.0f; // this is not really necessary... 00101 00102 m_nPosition = 0; 00103 m_nElementsFilled = 0; 00104 } 00105 00106 float CMeanFilter::Filter(float x) 00107 { 00108 if (m_nKernelSize <= 0) 00109 { 00110 printf("error: CMeanFilter::Filter called, but m_nKernelSize is invalid (%i)\n", m_nKernelSize); 00111 return x; 00112 } 00113 00114 m_pValues[m_nPosition++ % m_nKernelSize] = x; 00115 if (m_nElementsFilled < m_nKernelSize) 00116 m_nElementsFilled++; 00117 00118 float sum = 0.0f; 00119 for (int i = 0; i < m_nElementsFilled; i++) 00120 sum += m_pValues[i]; 00121 00122 return sum / m_nElementsFilled; 00123 }