Show
Ignore:
Timestamp:
10/07/08 14:10:50 (3 months ago)
Author:
schima
Message:

Niblack, Sauvolaの手法を3,4倍ほど高速化。

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/cpluspluscli/OpenCvSharp/trunk/OpenCvSharp/Extras.cpp

    r20858 r20894  
    4040                } 
    4141 
    42                 dst->Zero(); 
     42                N::IplImage^ gray = src->Clone(); 
     43                gray->EqualizeHist(); 
     44 
    4345                Byte* p = dst->ImageDataPtr; 
    44                 for (int y = 0; y < src->Height; y++) { 
    45                         for (int x = 0; x < src->Width; x++) { 
    46                                 double m = Mean(src, x, y, size); 
    47                                 double s = StdDev(src, x, y, size, m); 
    48                                 if(Math::Abs(s) > 0.1){ 
    49                                         s = s; 
    50                                 } 
     46                double m, s; 
     47 
     48                for (int y = 0; y < gray->Height; y++) { 
     49                        for (int x = 0; x < gray->Width; x++) { 
     50                                MeanStddev(gray, x, y, size, &m, &s); 
    5151                                int offset = (dst->WidthStep * y) + x; 
    5252                                p[offset] = (Byte)(m + k*s); 
     
    8585                } 
    8686 
    87                 dst->Zero(); 
     87                N::IplImage^ gray = src->Clone(); 
     88                gray->EqualizeHist(); 
     89 
    8890                Byte* p = dst->ImageDataPtr; 
    89                 for (int y = 0; y < src->Height; y++) { 
    90                         for (int x = 0; x < src->Width; x++) { 
    91                                 double m = Mean(src, x, y, size); 
    92                                 double s = StdDev(src, x, y, size, m); 
     91                double m, s; 
     92                int offset; 
    9393 
    94                                 int offset = (dst->WidthStep * y) + x; 
     94                for (int y = 0; y < gray->Height; y++) { 
     95                        for (int x = 0; x < gray->Width; x++) { 
     96                                MeanStddev(gray, x, y, size, &m, &s); 
     97                                offset = (dst->WidthStep * y) + x; 
    9598                                p[offset] = (Byte)(m + (1 + k*(s/r - 1))); 
    9699                        } 
     
    99102 
    100103        /// <summary> 
    101         /// ���ډ��̎�f�̕��ϒl��߂� /// </summary> 
    102         Double Extras::Mean(N::IplImage^ img, Int32 x, Int32 y, Int32 size) 
     104        /// ���ډ��̎�f�̕��ϒl�ƕW���΍���߂� /// </summary> 
     105        /// <param name="r">�W��</param> 
     106        void Extras::MeanStddev(N::IplImage^ img, Int32 x, Int32 y, Int32 size, Double* mean, Double* stddev) 
    103107        { 
    104108                int count = 0; 
    105                 int init_x = x - size + (size/2+1); 
    106                 int init_y = y - size + (size/2+1); 
    107109                int sum = 0; 
     110                int sum2 = 0; 
    108111                Byte* p = img->ImageDataPtr; 
     112                int xxx, yyy; 
     113                int size2 = size / 2; 
     114                int widthStep = img->Width; 
     115                Byte v; 
    109116 
    110117                for(int xx = 0; xx<size; xx++){ 
    111118                        for(int yy = 0; yy<size; yy++){ 
    112                                 int xxx = x + xx - size/2; 
    113                                 int yyy = y + yy - size/2; 
     119                                xxx = x + xx - size2; 
     120                                yyy = y + yy - size2; 
    114121                                if(xxx < 0 || xxx >= img->Width || yyy < 0 || yyy >= img->Height){ 
    115122                                        continue; 
    116123                                } 
    117                                 sum += p[img->WidthStep * yyy + xxx]; 
     124                                v = p[widthStep * yyy + xxx]; 
     125                                sum += v; 
     126                                sum2 += v * v; 
    118127                                count++; 
    119128                        } 
    120129                } 
    121  
    122                 return (double)sum / count; 
    123         } 
    124  
    125         /// <summary> 
    126         /// ���ډ��̎�f�̕W���΍���߂�       /// </summary> 
    127         Double Extras::StdDev(N::IplImage^ img, Int32 x, Int32 y, Int32 size, Double mean) 
    128         { 
    129                 int count = 0; 
    130                 int init_x = x - size + (size/2+1); 
    131                 int init_y = y - size + (size/2+1); 
    132                 double sum = 0; 
    133                 Byte* p = img->ImageDataPtr; 
    134  
    135                 for(int xx = 0; xx<size; xx++){ 
    136                         for(int yy = 0; yy<size; yy++){ 
    137                                 int xxx = x + xx - size/2; 
    138                                 int yyy = y + yy - size/2; 
    139                                 if(xxx < 0 || xxx >= img->Width || yyy < 0 || yyy >= img->Height){ 
    140                                         continue; 
    141                                 } 
    142                                 Byte v = p[img->WidthStep * yyy + xxx]; 
    143                                 sum += Math::Pow(v - mean, 2); 
    144                                 count++; 
    145                         } 
    146                 } 
    147                 double variance = sum / count; 
    148                 return Math::Sqrt(variance); 
     130                *mean = (double)sum / count; 
     131                double var = (double)sum2 / count - (*mean)*(*mean); 
     132                if(var < 0.0) var = 0.0; 
     133                *stddev = sqrt(var); 
    149134        } 
    150135