void Threshold(LPBYTE lpDIBBits,LONG LWidth,LONG LHeight,CRect rc,int thresh)                        
{ LPBYTE    lpArray = new BYTE[LWidth*LHeight];
LPBYTE    lpbinArray = new BYTE[LWidth*LHeight];
memcpy(lpArray,lpDIBBits,LWidth*LHeight);
memset(lpbinArray,0,LWidth*LHeight); DWORD lSrcWidth  = LWidth;
DWORD lSrcHeight  = LHeight;;
DWORD lSrcRowBytes = LWidth;
LPBYTE lpOldBits  = lpArray;
LPBYTE lpData   = lpbinArray;
DWORD i, j, h, offset;

int NearPoint[23];
int OffSetX[23] = {0,-1, 0, 1,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2, -1, 0, 1,0};
int OffSetY[23] = {-3,-2,-2,-2,-1,-1,-1,-1,-1,0, 0, 0, 0, 0,1, 1, 1, 1, 1, 2, 2, 2,3};
 
      for(i=rc.top;i<=rc.bottom;i++)
   for(j=rc.left;j<=rc.right;j++)
   {
//统计邻域内相似的点的个数
same = 0;              
for(h=0;h<23;h++)
                           //我想把这一句简化,请教高手怎么做,才能加快速度
 NearPoint[h] = (int)(*(lpOldBits+lSrcRowBytes*i+lSrcRowBytes*OffSetY[h]+j+OffSetX[h]));

for(h=0;h<23;h++)
 if(((int)abs(NearPoint[h]-NearPoint[18])) <= thresh) 
 same ++;
    
if(same > 18) *(lpData+lSrcRowBytes*i+j) = 0;
else *(lpData+lSrcRowBytes*i+j) = 255;
   }
memcpy(lpDIBBits,lpbinArray,LWidth*LHeight);
    
delete lpArray;
delete lpbinArray;
}

解决方案 »

  1.   

    int OffSetX[23] = {         0, 
    -1, 0, 1, 
     -2,-1, 0, 1, 2, 
     -2,-1, 0, 1, 2, 
                     -2,-1, 0, 1, 2, 
                        -1, 0, 1, 
                        0      
    };
    int OffSetY[23] = {       
             -3,
          -2,-2,-2,
       -1,-1,-1,-1,-1,
    0, 0, 0, 0, 0, 
    1, 1, 1, 1, 1, 
       2, 2, 2, 
          3       
    };
    这样写模板,可能便于理解一些