hough变换提取直线
hough变换检测直线,怎么样得到所检测到的直线的斜率啊?下面是我的程序,请高手赐教!
float HoughDIB(HGLOBAL hDIB)
{
#define pi 3.1415926
int Anglenumber;
typedef struct {
int Value;
int AngleNumber;
int Dist;
}MaxValue;
LONG lWidth,lHeight;
CDib ODIB;
LPSTR lpDIB;
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB);
LPSTR lpDIBBits;
lpDIBBits =ODIB.GetBits(lpDIB);
lWidth = ODIB.GetWidth(lpDIB);
lHeight =ODIB.GetHeight(lpDIB);
LPSTR lpSrc;
LPSTR lpDst;
LPSTR lpTrans;
LONG lLineBytes;
LPSTR lpNewDIBBits;
HLOCAL hNewDIBBits;
LPSTR lpTransArea;
HLOCAL hTransArea;
int iMaxDist;
int iMaxAngleNumber;
int iDist;
int iAngleNumber;
long i;
long j;
unsigned char pixel;
MaxValue MaxValue1;
MaxValue MaxValue2;
// 暂时分配内存,以保存新图像
hNewDIBBits = LocalAlloc(LHND, lWidth * lHeight);
if (hNewDIBBits == NULL)
{
// 分配内存失败
return FALSE;
}
// 锁定内存
lpNewDIBBits = (char * )LocalLock(hNewDIBBits);
// 初始化新分配的内存,设定初始值为255
lpDst = (char *)lpNewDIBBits;
memset(lpDst, (BYTE)255, lWidth * lHeight);
//计算变换域的尺寸
//最大距离
iMaxDist = (int) sqrt(lWidth*lWidth + lHeight*lHeight);
//角度从0-180,每格2度
iMaxAngleNumber = 180;
//为变换域分配内存
hTransArea = LocalAlloc(LHND, lWidth * lHeight * sizeof(int));
if (hNewDIBBits == NULL)
{
// 分配内存失败
return FALSE;
}
// 锁定内存
lpTransArea = (char * )LocalLock(hTransArea);
// 初始化新分配的内存,设定初始值为0
lpTrans = (char *)lpTransArea;
memset(lpTrans, 0, lWidth * lHeight * sizeof(int));
// 计算图像每行的字节数
lLineBytes =ODIB.GetReqByteWidth(lWidth * 8);
for(j = 0; j <lHeight; j++)
{
for(i = 0;i <lWidth; i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (char *)lpDIBBits + lLineBytes * j + i;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = (unsigned char)*lpSrc;
//目标图像中含有0和255外的其它灰度值
if(pixel != 255 && *lpSrc != 0)
return FALSE;
//如果是黑点,则在变换域的对应各点上加1
if(pixel == 0)
{
//注意步长是2度
for(iAngleNumber=0; iAngleNumber<iMaxAngleNumber; iAngleNumber++)
{
iDist = (int) fabs(i*cos(iAngleNumber*pi/180.0) + \
j*sin(iAngleNumber*pi/180.0));
//变换域的对应点上加1
*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber) = \
*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber) +1;
}
}
}
}
//找到变换域中的两个最大值点
MaxValue1.Value=0;
MaxValue2.Value=0;
//找到第一个最大值点
for (iDist=0; iDist<iMaxDist;iDist++)
{
for(iAngleNumber=0; iAngleNumber<iMaxAngleNumber; iAngleNumber++)
{
if((int)*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber)>MaxValue1.Value)
{
MaxValue1.Value = (int)*(lpTransArea+iDist*iMaxAngleNumber+iAngleNumber);
MaxValue1.Dist = iDist;
MaxValue1.AngleNumber = iAngleNumber;
}
}
}
//将第一个最大值点附近清零
for (iDist = -9;iDist < 10;iDist++)
{
for(iAngleNumber=-1; iAngleNumber<2; iAngleNumber++)
{
if(iDist+MaxValue1.Dist>=0 && iDist+MaxValue1.Dist<iMaxDist \
&& iAngleNumber+MaxValue1.AngleNumber>=0 && iAngleNumber+MaxValue1.AngleNumber<=iMaxAngleNumber)
{
*(lpTransArea+(iDist+MaxValue1.Dist)*iMaxAngleNumber+\
(iAngleNumber+MaxValue1.AngleNumber))=0;
}
}
}
Anglenumber=MaxValue1.AngleNumber;
// 释放内存
LocalUnlock(hNewDIBBits);
LocalFree(hNewDIBBits);
// 释放内存
LocalUnlock(hTransArea);
LocalFree(hTransArea);
::GlobalLock((HGLOBAL)hDIB);
if(Anglenumber>0)
{
// 返回
//MessageBox("ok",0,0);
return 180-Anglenumber;
}
else
{
return 180+abs(Anglenumber);
}
}