字符分割源代码(效果还可以)

  1. void FFTGabor::GaborTransform(int * lpDIBBits, LONG lWidth, LONG lHeight, int Orientation, int Frequency, FFTGaborResult * result)
  2. {
  3.         LONG        i;
  4.         LONG        j;

  5.         complex<double> *TD = new complex<double>[complexWidth * complexHeight];
  6.         complex<double> *FD = new complex<double>[complexWidth * complexHeight];
  7.         
  8.         // 行
  9.         for(i = 0; i < lHeight; i++) 
  10.         {
  11.                 // 列
  12.                 for(j = 0; j < lWidth; j++)
  13.                 {
  14.                         // 给时域赋值
  15.                         TD[j + complexWidth * i] = complex<double>(lpDIBBits[j+lWidth * i], 0);
  16.                 }
  17.         }
  18.         ::FFT2(TD,complexWidth , complexHeight);
  19.         
  20.         //::IFFT2(TD,256,256);
  21.         //在频域执行卷积
  22.         double scale = 1.0 / (complexWidth * complexHeight); 
  23.         
  24.         for( i=0; i<complexWidth * complexHeight; i++)
  25.                 FD[i] = TD[i] * KernelFFT2[Orientation * 4 + Frequency][i] ; // * scale; 
  26.         
  27.         IFFT2(FD,complexWidth , complexHeight);
  28.         
  29.         //计算均值、方差及结果
  30.         //计算均值并找出tmpMag中的最大最小值,以便调整到0~255,用于显示输出。
  31.         double min, max;
  32.         double Sum=0, Avg=0, Deta=0;
  33.         double tmpModulus=0;
  34.         double * tmpMag = new double[lWidth*lHeight] ;// [128][128] ;
  35.         int x ,y;
  36.         tmpModulus = sqrt(FD[(GaborHeight/2)*complexWidth+(GaborWidth/2)].real() *
  37.                                                                 FD[(GaborHeight/2)*complexWidth+(GaborWidth/2)].real() +
  38.                                                                 FD[(GaborHeight/2)*complexWidth+(GaborWidth/2)].imag() *
  39.                                                                 FD[(GaborHeight/2)*complexWidth+(GaborWidth/2)].imag());
  40.         min = max = tmpModulus;
  41.         for( y=(GaborHeight/2); y<(GaborHeight/2)+lHeight; y++){
  42.                 for( x=(GaborWidth/2); x<(GaborWidth/2)+lWidth; x++)
  43.                 {
  44.                         tmpModulus = sqrt(FD[y*complexWidth+x].real() * FD[y*complexWidth+x].real() +
  45.                                 FD[y*complexWidth+x].imag() * FD[y*complexWidth+x].imag());
  46.                         
  47.                         if(min>tmpModulus)
  48.                                 min = tmpModulus;
  49.                         if(max<tmpModulus)
  50.                                 max = tmpModulus;        
  51.                         
  52.                         Sum += tmpModulus;
  53.                         tmpMag[(y-(GaborHeight/2))* lWidth + (x-(GaborWidth/2))] = tmpModulus;
  54.                 }
  55.         }
  56.         Avg = Sum / (lHeight * lWidth);
  57.         //计算方差
  58.         for( y=0; y < lHeight; y++){
  59.                 for( x=0; x < lWidth; x++)
  60.                         Deta += (tmpMag[y* lWidth + x] - Avg)*(tmpMag[y* lWidth + x] - Avg);
  61.         }
  62.         Deta = Deta / (lHeight * lWidth);
  63.         
  64.         //计算magShow以及均值
  65.         scale = 255.0/(max-min);
  66.         
  67.         for( y=0; y < lHeight; y++){
  68.                 for( x=0; x < lWidth; x++)
  69.                 {
  70.                         result->magShow[y * lWidth + x] = (int)(scale*(tmpMag[y* lWidth + x]-min));
  71.                 }
  72.         }
  73.         result->Avg = Avg;
  74.         result->Deta = Deta;
  75.         
  76.         delete[] tmpMag;

  77.         delete[] FD;
  78.         delete[] TD;
  79. }

  80. void FFTGabor::intTocomplex(complex<double> * TD,int * lpDIBBits,LONG lWidth, LONG lHeight)
  81. {
  82.         LONG        i;
  83.         LONG        j;

  84. //        complex<double> *TD = new complex<double>[complexWidth * complexHeight];
  85.         
  86.         // 行
  87.         for(i = 0; i < lHeight; i++) 
  88.         {
  89.                 // 列
  90.                 for(j = 0; j < lWidth; j++)
  91.                 {
  92.                         // 给时域赋值
  93.                         TD[j + complexWidth * i] = complex<double>(lpDIBBits[j+lWidth * i], 0);
  94.                 }
  95.         }
  96.         ::FFT2(TD,complexWidth , complexHeight);

  97. }
  98. void FFTGabor::GaborTransform(complex<double> *TD, LONG lWidth, LONG lHeight, double * TransformResult)
  99. {
  100.         // 循环变量
  101.         LONG        i;
  102. //        LONG        j;
  103.         int Orientation, Frequency ;
  104.         // 分配内存
  105.         complex<double> *FD = new complex<double>[complexWidth * complexHeight];
  106.         
  107.         double scale = 1.0 / (complexWidth * complexHeight); 
  108.         double Sum=0, Avg=0, Deta=0;
  109.         double tmpModulus=0;
  110.         double * tmpMag = new double[lWidth*lHeight] ;// [128][128] ;
  111.         int x ,y;
  112.         
  113.         for( Orientation=0; Orientation<8; Orientation++){
  114.                 for( Frequency=0; Frequency<4; Frequency++)
  115.                 {
  116.                         //在频域执行卷积
  117.                         for( i=0; i<complexWidth * complexHeight; i++)
  118.                                 FD[i] = TD[i] * KernelFFT2[Orientation * 4 + Frequency][i] ; // * scale; 
  119.                         
  120.                         IFFT2(FD,complexWidth , complexHeight);
  121.                         
  122.                         //计算均值、方差及结果
  123.                         Sum=0, Avg=0, Deta=0;
  124.                         
  125.                         for( y=(GaborHeight/2); y<(GaborHeight/2)+lHeight; y++){
  126.                                 for( x=(GaborWidth/2); x<(GaborWidth/2)+lWidth; x++)
  127.                                 {
  128.                                         tmpModulus = sqrt(FD[y*complexWidth+x].real() * FD[y*complexWidth+x].real() +
  129.                                                 FD[y*complexWidth+x].imag() * FD[y*complexWidth+x].imag());
  130.                                         
  131.                                         Sum += tmpModulus;
  132.                                         tmpMag[(y-(GaborHeight/2)) * lWidth + (x-(GaborWidth/2))] = tmpModulus;
  133.                                 }
  134.                         }
  135.                         Avg = Sum / (lHeight * lWidth);
  136.                         //计算方差
  137.                         for( y=0; y < lHeight; y++){
  138.                                 for( x=0; x < lWidth; x++)
  139.                                         Deta += (tmpMag[y* lWidth + x] - Avg)*(tmpMag[y* lWidth + x] - Avg);
  140.                         }
  141.                         Deta = Deta / (lHeight * lWidth);
  142.                         

  143.                         TransformResult[(Orientation * 4 + Frequency)*2] = Avg;
  144.                         TransformResult[(Orientation * 4 + Frequency)*2+1] = Deta;
  145.                 }
  146.         }
  147.         
  148.         delete[] tmpMag;
  149.         delete[] FD;
  150. }

  151. void FFTGabor::GaborTransform(complex<double> *TD, LONG lWidth, LONG lHeight, int Orientation, int Frequency, FFTGaborResult * result)
  152. {
  153.         LONG        i;
  154. //        LONG        j;

  155.         complex<double> *FD = new complex<double>[complexWidth * complexHeight];
  156.         

  157.         //在频域执行卷积
  158.         double scale = 1.0 / (complexWidth * complexHeight); 
  159.         
  160.         for( i=0; i<complexWidth * complexHeight; i++)
  161.                 FD[i] = TD[i] * KernelFFT2[Orientation * 4 + Frequency][i] ; // * scale; 
  162.         
  163.         IFFT2(FD,complexWidth , complexHeight);
  164.         
  165.         //计算均值、方差及结果
  166.         //计算均值并找出tmpMag中的最大最小值,以便调整到0~255,用于显示输出。
  167.         double min, max;
  168.         double Sum=0, Avg=0, Deta=0;
  169.         double tmpModulus=0;
  170.         double * tmpMag = new double[lWidth*lHeight] ;// [128][128] ;
  171.         int x ,y;
  172.         tmpModulus = sqrt(FD[(GaborHeight/2)*complexWidth+(GaborWidth/2)].real() *
  173.                                                                 FD[(GaborHeight/2)*complexWidth+(GaborWidth/2)].real() +
  174.                                                                 FD[(GaborHeight/2)*complexWidth+(GaborWidth/2)].imag() *
  175.                                                                 FD[(GaborHeight/2)*complexWidth+(GaborWidth/2)].imag());
  176.         min = max = tmpModulus;
  177.         for( y=(GaborHeight/2); y<(GaborHeight/2)+lHeight; y++){
  178.                 for( x=(GaborWidth/2); x<(GaborWidth/2)+lWidth; x++)
  179.                 {
  180.                         tmpModulus = sqrt(FD[y*complexWidth+x].real() * FD[y*complexWidth+x].real() +
  181.                                 FD[y*complexWidth+x].imag() * FD[y*complexWidth+x].imag());
  182.                         
  183.                         if(min>tmpModulus)
  184.                                 min = tmpModulus;
  185.                         if(max<tmpModulus)
  186.                                 max = tmpModulus;        
  187.                         
  188.                         Sum += tmpModulus;
  189.                         tmpMag[(y-(GaborHeight/2))* lWidth + (x-(GaborWidth/2))] = tmpModulus;
  190.                 }
  191.         }
  192.         Avg = Sum / (lHeight * lWidth);
  193.         //计算方差
  194.         for( y=0; y < lHeight; y++){
  195.                 for( x=0; x < lWidth; x++)
  196.                         Deta += (tmpMag[y* lWidth + x] - Avg)*(tmpMag[y* lWidth + x] - Avg);
  197.         }
  198.         Deta = Deta / (lHeight * lWidth);
  199.         
  200.         //计算magShow以及均值
  201.         scale = 255.0/(max-min);
  202.         
  203.         for( y=0; y < lHeight; y++){
  204.                 for( x=0; x < lWidth; x++)
  205.                 {
  206.                         result->magShow[y * lWidth + x] = (int)(scale*(tmpMag[y* lWidth + x]-min));
  207.                 }
  208.         }
  209.         result->Avg = Avg;
  210.         result->Deta = Deta;
  211.         
  212.         delete[] tmpMag;

  213.         delete[] FD;
  214. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值