源码下载:http://download.csdn.net/detail/renshengrumenglibing/3875522
//请先仔细研读Hough变化的算法问题
void CBMPViewerDoc::OnMenuitem32796() //Hough变化
{//现在只能处理二值图像// TODO: Add your command handler code here
int linewidth;
linewidth=(pbi->bmiHeader.biWidth*pbi->bmiHeader.biBitCount+31)/32*4;
HLOCAL hTemp;
hTemp = LocalAlloc(LHND ,linewidth * bi.biHeight );
LPSTR lpTemp;
lpTemp = (char*)LocalLock(hTemp);
unsigned char *lpScr;
unsigned char * lpDest;
HLOCAL hTransArea;
hTransArea = LocalAlloc(LHND, bi.biWidth * bi.biHeight *sizeof(int));
LPSTR lpTransArea;
lpTransArea = (char *)(LocalLock(hTransArea));
//变换域尺寸
int iMaxAngleNumber;
int iMaxDist;
unsigned char pixel;
int iDest;
int iAngleNumber;
//变换域两个最大值
int MaxValue1 , MaxValue2;
//最大距离
iMaxDist = (int)(sqrt(bi.biHeight *bi.biHeight + bi.biWidth + bi.biWidth));
//每格两度
iMaxAngleNumber = 180;
//为内存赋初值
int Dist;//存放变换域中最大那个数的位置
int AngleNumber;
memset(lpTransArea , 0 , bi.biWidth * bi.biHeight *sizeof(int));
// TODO: Add your command handler code here
for(int i = 0 ; i< bi.biHeight ; i++){
for(int j = 0 ; j< bi.biWidth ; j++){
lpScr = (unsigned char *)lpBuf+linewidth*(bi.biHeight - i -1) + j;
pixel = *lpScr;
if(pixel ==0){
for(iAngleNumber = 0 ; iAngleNumber < iMaxAngleNumber; iAngleNumber++){
iDest = (int)(fabs(i*cos(iAngleNumber*2*PI/180.0)) + j*(sin(iAngleNumber*2 *PI/180.0)));
*(lpTransArea+iDest*iMaxAngleNumber + iAngleNumber) =*(lpTransArea+iDest*iMaxAngleNumber + iAngleNumber)+1;
}
}
}
}
//找变换域中的两个最大点
MaxValue1 = 0 ;
for( iDest = 0 ; iDest < iMaxDist ; iDest++){
for(iAngleNumber = 0 ; iAngleNumber < iMaxAngleNumber ; iAngleNumber++){
lpDest =(unsigned char*)(lpTransArea + iDest*iMaxAngleNumber + iAngleNumber);
if(*lpDest > MaxValue1){
MaxValue1 = *lpDest;
Dist = iDest;
AngleNumber = iAngleNumber;
}
}
}
//重绘该直线
for(int m = 0 ;m< bi.biHeight; m++){
for(int n = 0 ; n< bi.biWidth; n++){
lpDest = (unsigned char *)lpTemp + linewidth * (bi.biHeight-m -1) + n;
iDest = (int)(fabs(m*cos(AngleNumber*2*PI/180.0)) + n*(sin(AngleNumber*2 *PI/180.0)));
if((iDest -Dist) < 2 &&(iDest - Dist) > -2){
*lpDest = 0 ;
}
else{
*lpDest = 255;
}
}
}
memcpy(lpBuf, lpTemp, linewidth * bi.biHeight);
// Invalidata(TRUE);
UpdateAllViews(NULL,0,NULL);
}