Filter Library Camera Interface Physics

ScanFilterKuwahara.cpp

00001 
00002 
00003 //#define USE_ORIG_KUWAHARA
00004 
00005 
00006 
00007 // ScanFilterKuwahara.cpp: implementation of the CScanFilterKuwahara class.
00008 //
00009 //////////////////////////////////////////////////////////////////////
00010 
00011 #include "stdafx.h"
00012 #include "resource.h"
00013 #include <Camera/InterfaceDll.h>                                // Interface Dll definitions
00014 #include "ScanBaseBuffer.h"                      
00015 #include "ScanFilter.h"                                            // Include overall FilterBaseClass
00016 #include "ScanFilterKuwahara.h"
00017 
00018 #ifdef _DEBUG
00019 #undef THIS_FILE
00020 static char THIS_FILE[]=__FILE__;
00021 #define new DEBUG_NEW
00022 #endif
00023 
00024 //////////////////////////////////////////////////////////////////////
00025 // Construction/Destruction
00026 //////////////////////////////////////////////////////////////////////
00027 
00028 CScanFilterKuwahara::CScanFilterKuwahara()
00029 {       
00030         m_csFilterName = m_lpcsFilterName;
00031 }
00032 
00033 CScanFilterKuwahara::~CScanFilterKuwahara()
00034 {
00035 
00036 }
00037 
00038 //-------------------------------------------------------------------------------//
00039 
00040 LPCTSTR CScanFilterKuwahara::m_lpcsFilterName = _T("Kuwahara");
00041                                         // this filter's short name
00042 LPCTSTR CScanFilterKuwahara::m_lpcsShortFilterName = CScanFilterKuwahara::m_lpcsFilterName;
00043 IMPLEMENT_SERIAL(CScanFilterKuwahara, CScanFilter, 0)
00044 
00045 //-------------------------------------------------------------------------------//
00046 
00047 void CScanFilterKuwahara::Serialize(CArchive& ar)
00048 {    
00049         CScanFilter::Serialize( ar );    
00050 };
00051 
00052 //-------------------------------------------------------------------------------//
00053 
00054 BOOL CScanFilterKuwahara::SetParameters( LPCTSTR lpParameters )
00055 {
00056    /*
00057     * we do not expect any parameters, so say so:
00058     */
00059    if ( Q_INVALID( NULL != lpParameters && "CScanFilterKuwahara (SetParameters): no parameters expected." ) )
00060    {
00061       return FALSE;
00062    }
00063 
00064    return TRUE;
00065 }
00066 
00067 LPCTSTR CScanFilterKuwahara::GetParameters() const
00068 {
00069         return NULL;
00070 }
00071 
00072 //-------------------------------------------------------------------------------//
00073 
00074 BOOL CScanFilterKuwahara::Apply()
00075 {
00076         ASSERT(m_lpsbIn);
00077         ASSERT(m_lpsbOut);
00078 
00079    /*
00080     * copy the filter settings (not the data):
00081     */
00082    Q_RETURN( m_lpsbOut->CreateOutputBufferFor( *m_lpsbIn ) );
00083 
00084         m_lpsbOut->m_csBufferName.Format( _T("%s - %s"),m_lpsbIn->m_csBufferName, m_lpcsShortFilterName );
00085 
00086         // Process first part (L2R)
00087         Kuwahara(0);
00088 
00089         // If we have a double buffer, L2R and R2L, process that part too.
00090         if ((m_lpsbIn->m_dwFlags & SPM_SDBF_RIGHT2LEFT) && (m_lpsbIn->m_dwFlags & SPM_SDBF_LEFT2RIGHT))
00091                 Kuwahara(m_lpsbIn->m_dwSizeX * m_lpsbIn->m_dwSizeY);
00092 
00093         return TRUE;
00094 }
00095         
00096 //-------------------------------------------------------------------------------//
00097 
00098 void CScanFilterKuwahara::Kuwahara(DWORD dwOffset)
00099 {
00100         // Check: can only handle completed frames
00101         ASSERT(m_lpsbIn->m_dwPixelOffset == 0);
00102         ASSERT(m_lpsbIn->m_dwPixelCount == m_lpsbIn->m_dwSizeX * m_lpsbIn->m_dwSizeY);
00103         
00104         DWORD ptr = dwOffset;
00105         int nStrideX = m_lpsbIn->m_dwSizeX;     
00106 
00107         for (DWORD y=0; y<m_lpsbIn->m_dwSizeY; y++)
00108                 for (DWORD x=0; x<(DWORD)nStrideX; x++)
00109                 {                               
00110                         DWORD dwCount;
00111                         
00112                         double dSum, dSum2;
00113                         double dRegionAvg[4];
00114                         double dRegionSdv[4];
00115 
00116                         // loop through 3x3 pixel area (Region 1)
00117                         dwCount = 0;
00118                         dSum = dSum2 = 0.0;
00119                         {for (int yi=max(0,(int)y-2); yi<min((int)m_lpsbIn->m_dwSizeY,(int)y+1); yi++)
00120                         {
00121                                 for (int xi=max(0,(int)x-2); xi<min((int)nStrideX,(int)x+1); xi++)
00122                                 {
00123                                         short sValue = m_lpsbIn->m_pwData[dwOffset + yi*nStrideX + xi];
00124                                         dSum += sValue;
00125                                         dSum2 += sValue * sValue;
00126                                         dwCount++;
00127                                 }
00128                         }}
00129 
00130                         if (dwCount > 1)
00131                         {
00132                                 dRegionAvg[0] = dSum / dwCount;
00133 #ifdef USE_ORIG_KUWAHARA
00134                                 dRegionSdv[0] = sqrt( dSum2 - ((dSum*dSum/dwCount) / (dwCount-1)) );
00135 #else
00136                                 dRegionSdv[0] = dwCount * dSum2 - dSum * dSum;
00137 #endif
00138                         }
00139                         else dRegionSdv[0] = 1E20;      // set big number
00140 
00141                         // loop through 3x3 pixel area (Region 2)
00142                         dwCount = 0;
00143                         dSum = dSum2 = 0.0;
00144                         {for (int yi=max(0,(int)y-2); yi<min((int)m_lpsbIn->m_dwSizeY,(int)y+1); yi++)
00145                         {
00146                                 for (int xi=max(0,(int)x); xi<min((int)nStrideX,(int)x+3); xi++)
00147                                 {
00148                                         short sValue = m_lpsbIn->m_pwData[dwOffset + yi*nStrideX + xi];
00149                                         dSum += sValue;
00150                                         dSum2 += sValue * sValue;
00151                                         dwCount++;
00152                                 }
00153                         }}
00154 
00155                         if (dwCount > 1)
00156                         {
00157                                 dRegionAvg[1] = dSum / dwCount;
00158 #ifdef USE_ORIG_KUWAHARA
00159                                 dRegionSdv[1] = sqrt( dSum2 - ((dSum*dSum/dwCount) / (dwCount-1)) );
00160 #else
00161                                 dRegionSdv[1] = dwCount * dSum2 - dSum * dSum;
00162 #endif
00163                         }
00164                         else dRegionSdv[1] = 1E20;      // set big number
00165 
00166 
00167                         // loop through 3x3 pixel area (Region 3)
00168                         dwCount = 0;
00169                         dSum = dSum2 = 0.0;
00170                         {for (int yi=max(0,(int)y); yi<min((int)m_lpsbIn->m_dwSizeY,(int)y+3); yi++)
00171                         {
00172                                 for (int xi=max(0,(int)x-2); xi<min((int)nStrideX,(int)x+1); xi++)
00173                                 {
00174                                         short sValue = m_lpsbIn->m_pwData[dwOffset + yi*nStrideX + xi];
00175                                         dSum += sValue;
00176                                         dSum2 += sValue * sValue;
00177                                         dwCount++;
00178                                 }
00179                         }}
00180 
00181                         if (dwCount > 1)
00182                         {
00183                                 dRegionAvg[2] = dSum / dwCount;
00184 #ifdef USE_ORIG_KUWAHARA
00185                                 dRegionSdv[2] = sqrt( dSum2 - ((dSum*dSum/dwCount) / (dwCount-1)) );
00186 #else
00187                                 dRegionSdv[2] = dwCount * dSum2 - dSum * dSum;
00188 #endif
00189                         }
00190                         else dRegionSdv[2] = 1E20;      // set big number
00191 
00192                         // loop through 3x3 pixel area (Region 4)
00193                         dwCount = 0;
00194                         dSum = dSum2 = 0.0;
00195                         {for (int yi=max(0,(int)y); yi<min((int)m_lpsbIn->m_dwSizeY,(int)y+3); yi++)
00196                         {
00197                                 for (int xi=max(0,(int)x); xi<min((int)nStrideX,(int)x+3); xi++)
00198                                 {
00199                                         short sValue = m_lpsbIn->m_pwData[dwOffset + yi*nStrideX + xi];
00200                                         dSum += sValue;
00201                                         dSum2 += sValue * sValue;
00202                                         dwCount++;
00203                                 }
00204                         }}
00205 
00206                         if (dwCount > 1)
00207                         {
00208                                 dRegionAvg[3] = dSum / dwCount;
00209 #ifdef USE_ORIG_KUWAHARA
00210                                 dRegionSdv[3] = sqrt( dSum2 - ((dSum*dSum/dwCount) / (dwCount-1)) );
00211 #else
00212                                 dRegionSdv[3] = dwCount * dSum2 - dSum * dSum;
00213 #endif
00214                         }
00215                         else dRegionSdv[3] = 1E20;      // set big number
00216 
00217                         // Find Region with lowest std-diviation
00218                         int iRegionSelect = 0;
00219                         double dMin = dRegionSdv[0];
00220                         for (int i=1; i<4; i++) if (dRegionSdv[i] < dMin) { dMin = dRegionSdv[i]; iRegionSelect = i; };
00221 
00222                         // Set avg of that value
00223                         m_lpsbOut->m_pwData[ptr++] = (short)dRegionAvg[iRegionSelect];
00224 
00225                 }
00226 
00227 }
00228 
00229 //-------------------------------------------------------------------------------//

Camera Filter Library documentation © 2004-2007 by Leiden Probe Microscopy